Hi,
Can anyone convert the following from VB to VB.NET. It's beyond my programming knowledge!
Cheers,
Carl.
Step-by-Step Example
Create a new Standard EXE project in Visual Basic. Form1 is created by default.
Add a CommandButton control (Command1) to Form1.
Paste the following code into the code window of Form1:Option Explicit
Private Const WTS_CURRENT_SERVER_HANDLE = 0&
Private Enum WTS_CONNECTSTATE_CLASS
WTSActive
WTSConnected
WTSConnectQuery
WTSShadow
WTSDisconnected
WTSIdle
WTSListen
WTSReset
WTSDown
WTSInit
End Enum
Private Type WTS_SESSION_INFO
SessionID As Long
pWinStationName As Long
state As WTS_CONNECTSTATE_CLASS
End Type
Private Declare Function WTSEnumerateSessions _
Lib "wtsapi32.dll" Alias "WTSEnumerateSessionsA" ( _
ByVal hServer As Long, ByVal Reserved As Long, _
ByVal Version As Long, ByRef ppSessionInfo As Long, _
ByRef pCount As Long _
) As Long
Private Declare Sub WTSFreeMemory Lib "wtsapi32.dll" ( _
ByVal pMemory As Long)
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
Destination As Any, Source As Any, ByVal length As Long)
Private Declare Function lstrlenA Lib "kernel32" ( _
ByVal lpString As String) As Long
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" ( _
ByVal lpString1 As String, ByVal lpString2 As Long) As Long
Private arrWTSSessions() As WTS_SESSION_INFO
Private Function GetWTSSessions() As WTS_SESSION_INFO()
Dim RetVal As Long
Dim lpBuffer As Long
Dim Count As Long
Dim p As Long
Dim arrSessionInfo() As WTS_SESSION_INFO
RetVal = WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, _
0&, _
1, _
lpBuffer, _
Count)
If RetVal Then
' WTSEnumerateProcesses was successful.
p = lpBuffer
ReDim arrSessionInfo(Count - 1)
CopyMemory arrSessionInfo(0), ByVal p, _
Count * LenB(arrSessionInfo(0))
' Free the memory buffer.
WTSFreeMemory lpBuffer
Else
' Error occurred calling WTSEnumerateProcesses.
' Check Err.LastDllError for error code.
MsgBox "An error occurred calling WTSEnumerateProcesses. " & _
"Check the Platform SDK error codes in the MSDN Documentation " & _
"for more information.", vbCritical, "ERROR " & Err.LastDllError
End If
GetWTSSessions = arrSessionInfo
End Function
Private Sub Command1_Click()
Dim i As Integer
arrWTSSessions = GetWTSSessions
For i = LBound(arrWTSSessions) To UBound(arrWTSSessions)
Debug.Print "Session ID: " & arrWTSSessions(i).SessionID
Debug.Print "Machine Name: " & _
PointerToStringA(arrWTSSessions(i).pWinStationName)
Debug.Print "Connect State: " & arrWTSSessions(i).state
Debug.Print "***********"
Next i
End Sub
Public Function PointerToStringA(ByVal lpStringA As Long) As String
Dim nLen As Long
Dim sTemp As String
If lpStringA Then
nLen = lstrlenA(ByVal lpStringA)
If nLen Then
sTemp = String(nLen, vbNullChar)
lstrcpy sTemp, ByVal lpStringA
PointerToStringA = sTemp
End If
End If
End Function
Press the F5 key to run the project.
Make sure that the Immediate window is open. If not, press CTRL+G to open it. Click Command1. The session information is displayed in the Immediate window.
Can anyone convert the following from VB to VB.NET. It's beyond my programming knowledge!
Cheers,
Carl.
Step-by-Step Example
Create a new Standard EXE project in Visual Basic. Form1 is created by default.
Add a CommandButton control (Command1) to Form1.
Paste the following code into the code window of Form1:Option Explicit
Private Const WTS_CURRENT_SERVER_HANDLE = 0&
Private Enum WTS_CONNECTSTATE_CLASS
WTSActive
WTSConnected
WTSConnectQuery
WTSShadow
WTSDisconnected
WTSIdle
WTSListen
WTSReset
WTSDown
WTSInit
End Enum
Private Type WTS_SESSION_INFO
SessionID As Long
pWinStationName As Long
state As WTS_CONNECTSTATE_CLASS
End Type
Private Declare Function WTSEnumerateSessions _
Lib "wtsapi32.dll" Alias "WTSEnumerateSessionsA" ( _
ByVal hServer As Long, ByVal Reserved As Long, _
ByVal Version As Long, ByRef ppSessionInfo As Long, _
ByRef pCount As Long _
) As Long
Private Declare Sub WTSFreeMemory Lib "wtsapi32.dll" ( _
ByVal pMemory As Long)
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
Destination As Any, Source As Any, ByVal length As Long)
Private Declare Function lstrlenA Lib "kernel32" ( _
ByVal lpString As String) As Long
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" ( _
ByVal lpString1 As String, ByVal lpString2 As Long) As Long
Private arrWTSSessions() As WTS_SESSION_INFO
Private Function GetWTSSessions() As WTS_SESSION_INFO()
Dim RetVal As Long
Dim lpBuffer As Long
Dim Count As Long
Dim p As Long
Dim arrSessionInfo() As WTS_SESSION_INFO
RetVal = WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, _
0&, _
1, _
lpBuffer, _
Count)
If RetVal Then
' WTSEnumerateProcesses was successful.
p = lpBuffer
ReDim arrSessionInfo(Count - 1)
CopyMemory arrSessionInfo(0), ByVal p, _
Count * LenB(arrSessionInfo(0))
' Free the memory buffer.
WTSFreeMemory lpBuffer
Else
' Error occurred calling WTSEnumerateProcesses.
' Check Err.LastDllError for error code.
MsgBox "An error occurred calling WTSEnumerateProcesses. " & _
"Check the Platform SDK error codes in the MSDN Documentation " & _
"for more information.", vbCritical, "ERROR " & Err.LastDllError
End If
GetWTSSessions = arrSessionInfo
End Function
Private Sub Command1_Click()
Dim i As Integer
arrWTSSessions = GetWTSSessions
For i = LBound(arrWTSSessions) To UBound(arrWTSSessions)
Debug.Print "Session ID: " & arrWTSSessions(i).SessionID
Debug.Print "Machine Name: " & _
PointerToStringA(arrWTSSessions(i).pWinStationName)
Debug.Print "Connect State: " & arrWTSSessions(i).state
Debug.Print "***********"
Next i
End Sub
Public Function PointerToStringA(ByVal lpStringA As Long) As String
Dim nLen As Long
Dim sTemp As String
If lpStringA Then
nLen = lstrlenA(ByVal lpStringA)
If nLen Then
sTemp = String(nLen, vbNullChar)
lstrcpy sTemp, ByVal lpStringA
PointerToStringA = sTemp
End If
End If
End Function
Press the F5 key to run the project.
Make sure that the Immediate window is open. If not, press CTRL+G to open it. Click Command1. The session information is displayed in the Immediate window.