WTSEnumerateProcesses

  • Thread starter Thread starter Joe Green
  • Start date Start date
J

Joe Green

Hi guys,

I'm trying to get a list of processes for the current Terminal Server
session in VB.NET. It's all fine apart from I can't get the
ProcessName out of the structure WTS_PROCESS_INFO. I basically get the
first character in the WTS_PROCESSINFO.ProcessName. I know it's
something to do with the fact that it's a pointer to a string, but I
can't work out what to do about it!

The relevant code is as follows:

===============================================================

Private Declare Auto Function WTSEnumerateProcesses Lib
"wtsapi32.dll" ( _
ByVal hServer As Int32, ByVal Reserved As Int32, _
ByVal Version As Int32, ByRef ppProcessInfo As IntPtr, _
ByRef pCount As Int32) As Int32

Private Declare Auto Sub WTSFreeMemory Lib "wtsapi32.dll" (ByVal
pMemory As IntPtr)

Private Structure WTS_PROCESS_INFO
Dim SessionID As Integer
Dim ProcessID As Integer
Dim ProcessName As String
Dim UserSid As Integer
End Structure


Dim ptrProcessInfo As IntPtr
Dim lngPtrPos As Long
Dim intReturn As Integer
Dim intCount As Integer
Dim intProcessCount As Integer
Dim strucProcessInfo As WTS_PROCESS_INFO


intReturn =
WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0, 1, ptrProcessInfo,
intProcessCount)

'Get the length in bytes of each structure...
lngPtrPos = ptrProcessInfo.ToInt32()

For intCount = 0 To intProcessCount - 1
strucProcessInfo = Marshal.PtrToStructure(New
IntPtr(lngPtrPos), strucProcessInfo.GetType)

Console.WriteLine("Process Name: " &
strucProcessInfo.ProcessName.ToString)
Console.WriteLine("Process ID: " &
strucProcessInfo.ProcessID.ToString)
Console.WriteLine("Session ID: " &
strucProcessInfo.SessionID.ToString)

lngPtrPos = lngPtrPos + Len(strucProcessInfo)
Next

Call WTSFreeMemory(ptrProcessInfo)

===============================================================

Any suggestion son this one...?!
 
Private Declare Auto Function WTSEnumerateProcesses Lib
"wtsapi32.dll" ( _
ByVal hServer As Int32, ByVal Reserved As Int32, _
ByVal Version As Int32, ByRef ppProcessInfo As IntPtr, _
ByRef pCount As Int32) As Int32

Since you use Auto on the function ...

Private Structure WTS_PROCESS_INFO
Dim SessionID As Integer
Dim ProcessID As Integer
Dim ProcessName As String
Dim UserSid As Integer
End Structure

.... you should use it on the structure as well. Add the
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)>
attribute.


Mattias
 
Back
Top