Can anyone covert a VB to VB.Net program for me?

C

CarlEOgden

Hi,

I've found this example but when I try to rework it for VB.Net I fail
with the API calls and the CopyMemory procedure and to be truthful,
it's far over my skill level with the getting information from buffers
etc, all I've ever done with these is hack code around to get them
working!!!

Many thanks with any and all help given.

Cheers,
Carl.

Code Start =============================================================
Hi,

I've found this from this group but it's in VB, can anyone convert it
to VB.Net for me? I've tried and failed! Mainly on the copymemory. But
I'm not too hot in .Net to begin with!!!

Many thanks,
Carl.

From: Alick [MS] ([email protected])
Subject: RE: WTSQuerySessionInformation
View: Complete Thread (3 articles)
Original Format
Newsgroups: microsoft.public.vb.winapi
Date: 2002-11-24 22:19:25 PST


Here is a sample using the WTSQuerySessionInformation function to
return
information about the specified session on the specified Terminal
Server.

Option Explicit
Private Declare Function LoadLibrary Lib "KERNEL32" Alias
"LoadLibraryA"
(ByVal
lpLibFileName As String) As Long
Private Declare Function FreeLibrary& Lib "KERNEL32" (ByVal hLibModule
As
Long)

Private Declare Function WTSQuerySessionInformation Lib "wtsapi32"
Alias
"WTSQuerySessionInformationA" _
(ByVal hServer As Long, ByVal SessionID As Long, ByVal WTSInfoClass
As
Long,
_
ByRef ppBuffer As Long, ByRef lLen As Long) As Long

Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (
_
hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Private Enum WTS_CONNECTSTATE_CLASS
WTSActive
WTSConnected
WTSConnectQuery
WTSShadow
WTSDisconnected
WTSIdle
WTSListen
WTSReset
WTSDown
WTSInit
End Enum

Private Type WTS_CLIENT_ADDRESS
AddressFamily As Long
Address(20) As Byte
End Type

Private Type WTS_CLIENT_DISPLAY
HorizontalResolution As Long
VerticalResolution As Long
ColorDepth As Long
End Type

Private Enum WTS_INFO_CLASS
WTSInitialProgram
WTSApplicationName
WTSWorkingDirectory
WTSOEMId
WTSSessionId
WTSUserName
WTSWinStationName
WTSDomainName
WTSConnectState
WTSClientBuildNumber
WTSClientName
WTSClientDirectory
WTSClientProductId
WTSClientHardwareId
WTSClientAddress
WTSClientDisplay
End Enum

Private Const WTS_CURRENT_SERVER_HANDLE As Long = 0
Private Const WTS_CURRENT_SESSION As Long = -1

Private varWTS_CONNECTSTATE_CLASS As WTS_CONNECTSTATE_CLASS

Private Sub Form_Load()
Dim lRet As Long
Dim lLib As Long
Dim sVal As String
Dim sOut As String
Dim i As Long

On Error GoTo ErrHandler

lLib = LoadLibrary("Wtsapi32.dll")
With lblNotFound
If lLib < 1 Then
.Caption = "DLL Not Found"
.ForeColor = &HFF&
Exit Sub
Else
.Caption = "DLL Found"
.ForeColor = &H8000&
End If
End With

MsgBox "About to call API"
For i = 0 To 15
msGetTSEValue (i)
Next i
Exit Sub

lRet = FreeLibrary(lLib)
Exit Sub

ErrHandler:
MsgBox Error(Err)
End Sub

Private Sub msGetTSEValue(eWTSType As WTS_INFO_CLASS)
Dim sVal As String
Dim lVal As Long
Dim intVal As Integer
Dim lRet As Long
Dim lLen As Long
Dim lErr As Long
Dim lBufferAddress As Long
Dim varWTS_CLIENT_ADDRESS As WTS_CLIENT_ADDRESS
Dim varWTS_CLIENT_DISPLAY As WTS_CLIENT_DISPLAY

lRet = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, _
WTS_CURRENT_SESSION, _
eWTSType, _
lBufferAddress, _
lLen)

' copying the buffer to a VB string
Select Case eWTSType
Case 0
MsgBox "WTSInitialProgram"
Case 1
MsgBox "WTSApplicationName"
Case 2
MsgBox "WTSWorkingDirectory"
Case 3
MsgBox "WTSOEMId"
Case 4
MsgBox "WTSSessionId"
Case 5
MsgBox "WTSUserName"
Case 6
MsgBox "WTSWinStationName"
Case 7
MsgBox "WTSDomainName"
Case 8
MsgBox "WTSConnectState"
Case 9
MsgBox "WTSClientBuildNumber"
Case 10
MsgBox "WTSClientName"
Case 11
MsgBox "WTSClientDirectory"
Case 12
MsgBox "WTSClientProductId"
Case 13
MsgBox "WTSClientHardwareId"
Case 14
MsgBox "WTSClientAddress"
Case 15
MsgBox "WTSClientDisplay"
End Select

If lLen > 0 Then
Select Case eWTSType
Case 4
lVal = 0
CopyMemory lVal, ByVal lBufferAddress, lLen
MsgBox "WTSSessionId " & lVal
Case 8
lVal = 0
CopyMemory lVal, ByVal lBufferAddress, lLen
MsgBox "WTSConnectState " & lVal
Case 9
intVal = 0
CopyMemory intVal, ByVal lBufferAddress, lLen
MsgBox "WTSClientBuildNumber " & intVal
Case 12
intVal = 0
CopyMemory intVal, ByVal lBufferAddress, lLen
MsgBox "WTSClientProductId " & intVal
Case 13
intVal = 0
CopyMemory intVal, ByVal lBufferAddress, lLen
MsgBox "WTSClientHardwareId " & intVal
Case 14
CopyMemory varWTS_CLIENT_ADDRESS, ByVal lBufferAddress,
lLen
MsgBox "varWTS_CLIENT_ADDRESS " &
varWTS_CLIENT_ADDRESS.AddressFamily
Case 15
CopyMemory varWTS_CLIENT_DISPLAY, ByVal lBufferAddress,
lLen
MsgBox "varWTS_CLIENT_DISPLAY.ColorDepth " &
varWTS_CLIENT_DISPLAY.ColorDepth
MsgBox "varWTS_CLIENT_DISPLAY.HorizontalResolution " &
varWTS_CLIENT_DISPLAY.HorizontalResolution
MsgBox "varWTS_CLIENT_DISPLAY.VerticalResolution " &
varWTS_CLIENT_DISPLAY.VerticalResolution
Case Else
sVal = Space(lLen) ' allocating memory to the VB string to
be
able
to store the buffer
CopyMemory ByVal sVal, ByVal lBufferAddress, lLen
MsgBox "sval " & sVal
End Select
End If

If lRet = 0 Then
lErr = Err.LastDllError
MsgBox "Error code reported from LastDllError: " & lErr
End If

End Function

Hope it helps.




Sincerely,

Alick Ye, MCSD
Product Support Services
Microsoft Corporation
 
K

Ken Tucker [MVP]

Hi,

First off the vb6 long is the same as the vb.net integer.
Instead of using copymemory take a look at marshal.ptrtostringauto and
marshal.ptrtostructure methods.
Untested but you can probably use this for the api declare and get the
structure and string back without having to jump through hoops.

Private Declare Function WTSQuerySessionInformation Lib "wtsapi32"
Alias
"WTSQuerySessionInformationA" _
(ByVal hServer As integer, ByVal SessionID As integer, ByVal WTSInfoClass
As wts_info_class,
_
ByRef ppBuffer As string, ByRef lLen As integer) As integer

Ken
------------------------
Hi,

I've found this example but when I try to rework it for VB.Net I fail
with the API calls and the CopyMemory procedure and to be truthful,
it's far over my skill level with the getting information from buffers
etc, all I've ever done with these is hack code around to get them
working!!!

Many thanks with any and all help given.

Cheers,
Carl.

Code Start =============================================================
Hi,

I've found this from this group but it's in VB, can anyone convert it
to VB.Net for me? I've tried and failed! Mainly on the copymemory. But
I'm not too hot in .Net to begin with!!!

Many thanks,
Carl.

From: Alick [MS] ([email protected])
Subject: RE: WTSQuerySessionInformation
View: Complete Thread (3 articles)
Original Format
Newsgroups: microsoft.public.vb.winapi
Date: 2002-11-24 22:19:25 PST


Here is a sample using the WTSQuerySessionInformation function to
return
information about the specified session on the specified Terminal
Server.

Option Explicit
Private Declare Function LoadLibrary Lib "KERNEL32" Alias
"LoadLibraryA"
(ByVal
lpLibFileName As String) As Long
Private Declare Function FreeLibrary& Lib "KERNEL32" (ByVal hLibModule
As
Long)

Private Declare Function WTSQuerySessionInformation Lib "wtsapi32"
Alias
"WTSQuerySessionInformationA" _
(ByVal hServer As Long, ByVal SessionID As Long, ByVal WTSInfoClass
As
Long,
_
ByRef ppBuffer As Long, ByRef lLen As Long) As Long

Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (
_
hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Private Enum WTS_CONNECTSTATE_CLASS
WTSActive
WTSConnected
WTSConnectQuery
WTSShadow
WTSDisconnected
WTSIdle
WTSListen
WTSReset
WTSDown
WTSInit
End Enum

Private Type WTS_CLIENT_ADDRESS
AddressFamily As Long
Address(20) As Byte
End Type

Private Type WTS_CLIENT_DISPLAY
HorizontalResolution As Long
VerticalResolution As Long
ColorDepth As Long
End Type

Private Enum WTS_INFO_CLASS
WTSInitialProgram
WTSApplicationName
WTSWorkingDirectory
WTSOEMId
WTSSessionId
WTSUserName
WTSWinStationName
WTSDomainName
WTSConnectState
WTSClientBuildNumber
WTSClientName
WTSClientDirectory
WTSClientProductId
WTSClientHardwareId
WTSClientAddress
WTSClientDisplay
End Enum

Private Const WTS_CURRENT_SERVER_HANDLE As Long = 0
Private Const WTS_CURRENT_SESSION As Long = -1

Private varWTS_CONNECTSTATE_CLASS As WTS_CONNECTSTATE_CLASS

Private Sub Form_Load()
Dim lRet As Long
Dim lLib As Long
Dim sVal As String
Dim sOut As String
Dim i As Long

On Error GoTo ErrHandler

lLib = LoadLibrary("Wtsapi32.dll")
With lblNotFound
If lLib < 1 Then
.Caption = "DLL Not Found"
.ForeColor = &HFF&
Exit Sub
Else
.Caption = "DLL Found"
.ForeColor = &H8000&
End If
End With

MsgBox "About to call API"
For i = 0 To 15
msGetTSEValue (i)
Next i
Exit Sub

lRet = FreeLibrary(lLib)
Exit Sub

ErrHandler:
MsgBox Error(Err)
End Sub

Private Sub msGetTSEValue(eWTSType As WTS_INFO_CLASS)
Dim sVal As String
Dim lVal As Long
Dim intVal As Integer
Dim lRet As Long
Dim lLen As Long
Dim lErr As Long
Dim lBufferAddress As Long
Dim varWTS_CLIENT_ADDRESS As WTS_CLIENT_ADDRESS
Dim varWTS_CLIENT_DISPLAY As WTS_CLIENT_DISPLAY

lRet = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, _
WTS_CURRENT_SESSION, _
eWTSType, _
lBufferAddress, _
lLen)

' copying the buffer to a VB string
Select Case eWTSType
Case 0
MsgBox "WTSInitialProgram"
Case 1
MsgBox "WTSApplicationName"
Case 2
MsgBox "WTSWorkingDirectory"
Case 3
MsgBox "WTSOEMId"
Case 4
MsgBox "WTSSessionId"
Case 5
MsgBox "WTSUserName"
Case 6
MsgBox "WTSWinStationName"
Case 7
MsgBox "WTSDomainName"
Case 8
MsgBox "WTSConnectState"
Case 9
MsgBox "WTSClientBuildNumber"
Case 10
MsgBox "WTSClientName"
Case 11
MsgBox "WTSClientDirectory"
Case 12
MsgBox "WTSClientProductId"
Case 13
MsgBox "WTSClientHardwareId"
Case 14
MsgBox "WTSClientAddress"
Case 15
MsgBox "WTSClientDisplay"
End Select

If lLen > 0 Then
Select Case eWTSType
Case 4
lVal = 0
CopyMemory lVal, ByVal lBufferAddress, lLen
MsgBox "WTSSessionId " & lVal
Case 8
lVal = 0
CopyMemory lVal, ByVal lBufferAddress, lLen
MsgBox "WTSConnectState " & lVal
Case 9
intVal = 0
CopyMemory intVal, ByVal lBufferAddress, lLen
MsgBox "WTSClientBuildNumber " & intVal
Case 12
intVal = 0
CopyMemory intVal, ByVal lBufferAddress, lLen
MsgBox "WTSClientProductId " & intVal
Case 13
intVal = 0
CopyMemory intVal, ByVal lBufferAddress, lLen
MsgBox "WTSClientHardwareId " & intVal
Case 14
CopyMemory varWTS_CLIENT_ADDRESS, ByVal lBufferAddress,
lLen
MsgBox "varWTS_CLIENT_ADDRESS " &
varWTS_CLIENT_ADDRESS.AddressFamily
Case 15
CopyMemory varWTS_CLIENT_DISPLAY, ByVal lBufferAddress,
lLen
MsgBox "varWTS_CLIENT_DISPLAY.ColorDepth " &
varWTS_CLIENT_DISPLAY.ColorDepth
MsgBox "varWTS_CLIENT_DISPLAY.HorizontalResolution " &
varWTS_CLIENT_DISPLAY.HorizontalResolution
MsgBox "varWTS_CLIENT_DISPLAY.VerticalResolution " &
varWTS_CLIENT_DISPLAY.VerticalResolution
Case Else
sVal = Space(lLen) ' allocating memory to the VB string to
be
able
to store the buffer
CopyMemory ByVal sVal, ByVal lBufferAddress, lLen
MsgBox "sval " & sVal
End Select
End If

If lRet = 0 Then
lErr = Err.LastDllError
MsgBox "Error code reported from LastDllError: " & lErr
End If

End Function

Hope it helps.




Sincerely,

Alick Ye, MCSD
Product Support Services
Microsoft Corporation
 
C

CarlEOgden

Ken Tucker said:
Hi,

First off the vb6 long is the same as the vb.net integer.
Instead of using copymemory take a look at marshal.ptrtostringauto and
marshal.ptrtostructure methods.
Untested but you can probably use this for the api declare and get the
structure and string back without having to jump through hoops.

Private Declare Function WTSQuerySessionInformation Lib "wtsapi32"
Alias
"WTSQuerySessionInformationA" _
(ByVal hServer As integer, ByVal SessionID As integer, ByVal WTSInfoClass
As wts_info_class,
_
ByRef ppBuffer As string, ByRef lLen As integer) As integer

Ken

Hi,

My issue is that you have to use the copymemory api to get the info I
require out of the buffers but that doesn't work in VB.Net and I don't
know how to get round it!

Thanks for your info,
Carl.
 
C

CarlEOgden

Hi,

I posted this a while ago! To no avail!

What if I offered to pay a nominal amount via PayPal?

We'll work the finer details out later!

Cheers,
Carl.





Hi,

I've found this example but when I try to rework it for VB.Net I fail
with the API calls and the CopyMemory procedure and to be truthful,
it's far over my skill level with the getting information from buffers
etc, all I've ever done with these is hack code around to get them
working!!!

Many thanks with any and all help given.

Cheers,
Carl.

Code Start =============================================================
Hi,

I've found this from this group but it's in VB, can anyone convert it
to VB.Net for me? I've tried and failed! Mainly on the copymemory. But
I'm not too hot in .Net to begin with!!!

Many thanks,
Carl.

From: Alick [MS] ([email protected])
Subject: RE: WTSQuerySessionInformation
View: Complete Thread (3 articles)
Original Format
Newsgroups: microsoft.public.vb.winapi
Date: 2002-11-24 22:19:25 PST


Here is a sample using the WTSQuerySessionInformation function to
return
information about the specified session on the specified Terminal
Server.

Option Explicit
Private Declare Function LoadLibrary Lib "KERNEL32" Alias
"LoadLibraryA"
(ByVal
lpLibFileName As String) As Long
Private Declare Function FreeLibrary& Lib "KERNEL32" (ByVal hLibModule
As
Long)

Private Declare Function WTSQuerySessionInformation Lib "wtsapi32"
Alias
"WTSQuerySessionInformationA" _
(ByVal hServer As Long, ByVal SessionID As Long, ByVal WTSInfoClass
As
Long,
_
ByRef ppBuffer As Long, ByRef lLen As Long) As Long

Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (
_
hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Private Enum WTS_CONNECTSTATE_CLASS
WTSActive
WTSConnected
WTSConnectQuery
WTSShadow
WTSDisconnected
WTSIdle
WTSListen
WTSReset
WTSDown
WTSInit
End Enum

Private Type WTS_CLIENT_ADDRESS
AddressFamily As Long
Address(20) As Byte
End Type

Private Type WTS_CLIENT_DISPLAY
HorizontalResolution As Long
VerticalResolution As Long
ColorDepth As Long
End Type

Private Enum WTS_INFO_CLASS
WTSInitialProgram
WTSApplicationName
WTSWorkingDirectory
WTSOEMId
WTSSessionId
WTSUserName
WTSWinStationName
WTSDomainName
WTSConnectState
WTSClientBuildNumber
WTSClientName
WTSClientDirectory
WTSClientProductId
WTSClientHardwareId
WTSClientAddress
WTSClientDisplay
End Enum

Private Const WTS_CURRENT_SERVER_HANDLE As Long = 0
Private Const WTS_CURRENT_SESSION As Long = -1

Private varWTS_CONNECTSTATE_CLASS As WTS_CONNECTSTATE_CLASS

Private Sub Form_Load()
Dim lRet As Long
Dim lLib As Long
Dim sVal As String
Dim sOut As String
Dim i As Long

On Error GoTo ErrHandler

lLib = LoadLibrary("Wtsapi32.dll")
With lblNotFound
If lLib < 1 Then
.Caption = "DLL Not Found"
.ForeColor = &HFF&
Exit Sub
Else
.Caption = "DLL Found"
.ForeColor = &H8000&
End If
End With

MsgBox "About to call API"
For i = 0 To 15
msGetTSEValue (i)
Next i
Exit Sub

lRet = FreeLibrary(lLib)
Exit Sub

ErrHandler:
MsgBox Error(Err)
End Sub

Private Sub msGetTSEValue(eWTSType As WTS_INFO_CLASS)
Dim sVal As String
Dim lVal As Long
Dim intVal As Integer
Dim lRet As Long
Dim lLen As Long
Dim lErr As Long
Dim lBufferAddress As Long
Dim varWTS_CLIENT_ADDRESS As WTS_CLIENT_ADDRESS
Dim varWTS_CLIENT_DISPLAY As WTS_CLIENT_DISPLAY

lRet = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, _
WTS_CURRENT_SESSION, _
eWTSType, _
lBufferAddress, _
lLen)

' copying the buffer to a VB string
Select Case eWTSType
Case 0
MsgBox "WTSInitialProgram"
Case 1
MsgBox "WTSApplicationName"
Case 2
MsgBox "WTSWorkingDirectory"
Case 3
MsgBox "WTSOEMId"
Case 4
MsgBox "WTSSessionId"
Case 5
MsgBox "WTSUserName"
Case 6
MsgBox "WTSWinStationName"
Case 7
MsgBox "WTSDomainName"
Case 8
MsgBox "WTSConnectState"
Case 9
MsgBox "WTSClientBuildNumber"
Case 10
MsgBox "WTSClientName"
Case 11
MsgBox "WTSClientDirectory"
Case 12
MsgBox "WTSClientProductId"
Case 13
MsgBox "WTSClientHardwareId"
Case 14
MsgBox "WTSClientAddress"
Case 15
MsgBox "WTSClientDisplay"
End Select

If lLen > 0 Then
Select Case eWTSType
Case 4
lVal = 0
CopyMemory lVal, ByVal lBufferAddress, lLen
MsgBox "WTSSessionId " & lVal
Case 8
lVal = 0
CopyMemory lVal, ByVal lBufferAddress, lLen
MsgBox "WTSConnectState " & lVal
Case 9
intVal = 0
CopyMemory intVal, ByVal lBufferAddress, lLen
MsgBox "WTSClientBuildNumber " & intVal
Case 12
intVal = 0
CopyMemory intVal, ByVal lBufferAddress, lLen
MsgBox "WTSClientProductId " & intVal
Case 13
intVal = 0
CopyMemory intVal, ByVal lBufferAddress, lLen
MsgBox "WTSClientHardwareId " & intVal
Case 14
CopyMemory varWTS_CLIENT_ADDRESS, ByVal lBufferAddress,
lLen
MsgBox "varWTS_CLIENT_ADDRESS " &
varWTS_CLIENT_ADDRESS.AddressFamily
Case 15
CopyMemory varWTS_CLIENT_DISPLAY, ByVal lBufferAddress,
lLen
MsgBox "varWTS_CLIENT_DISPLAY.ColorDepth " &
varWTS_CLIENT_DISPLAY.ColorDepth
MsgBox "varWTS_CLIENT_DISPLAY.HorizontalResolution " &
varWTS_CLIENT_DISPLAY.HorizontalResolution
MsgBox "varWTS_CLIENT_DISPLAY.VerticalResolution " &
varWTS_CLIENT_DISPLAY.VerticalResolution
Case Else
sVal = Space(lLen) ' allocating memory to the VB string to
be
able
to store the buffer
CopyMemory ByVal sVal, ByVal lBufferAddress, lLen
MsgBox "sval " & sVal
End Select
End If

If lRet = 0 Then
lErr = Err.LastDllError
MsgBox "Error code reported from LastDllError: " & lErr
End If

End Function

Hope it helps.




Sincerely,

Alick Ye, MCSD
Product Support Services
Microsoft Corporation
 
C

Cor Ligthert

Carl,

The least you can do is make your code copying by breaking the lines in
smaller parts, than maybe somebody can help you, now it is a lot of work to
paste it right in the IDE when you use a standard newsreader.

Cor
 
C

CarlEOgden

Hi,

Sorry, I only use this through Google groups as I don't have a news
reader! And read all the posts using IE.

But as it's a fully working example I presumed that it would be easier
to have the full code! I thought I'd removed most of the other
information other than the program I wanted convert! Once again,
Sorry!

Carl.
 
C

Cor Ligthert

Carl,

No that is correct.

I mean make the lines in your code something shorter using the _ now when
you copy it, it gives all kind of strange cariage returns and nobody likes
to correct that.

When I was you I would repeat the question than again in a new message,
maybe somebody will help you than. (The only one who can reply with keep in
the origial thread is me, and I do not)

:)

Cor
,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top