WTSEnumerateProcesses

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe Green

    #1

    WTSEnumerateProcesses

    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_INF O. 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 WTSEnumeratePro cesses Lib
    "wtsapi32.d ll" ( _
    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.d ll" (ByVal
    pMemory As IntPtr)

    Private Structure WTS_PROCESS_INF O
    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 strucProcessInf o As WTS_PROCESS_INF O


    intReturn =
    WTSEnumeratePro cesses(WTS_CURR ENT_SERVER_HAND LE, 0, 1, ptrProcessInfo,
    intProcessCount )

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

    For intCount = 0 To intProcessCount - 1
    strucProcessInf o = Marshal.PtrToSt ructure(New
    IntPtr(lngPtrPo s), strucProcessInf o.GetType)

    Console.WriteLi ne("Process Name: " &
    strucProcessInf o.ProcessName.T oString)
    Console.WriteLi ne("Process ID: " &
    strucProcessInf o.ProcessID.ToS tring)
    Console.WriteLi ne("Session ID: " &
    strucProcessInf o.SessionID.ToS tring)

    lngPtrPos = lngPtrPos + Len(strucProces sInfo)
    Next

    Call WTSFreeMemory(p trProcessInfo)

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

    Any suggestion son this one...?!

  • Mattias Sjögren

    #2
    Re: WTSEnumeratePro cesses

    > Private Declare Auto Function WTSEnumeratePro cesses Lib[color=blue]
    >"wtsapi32.dl l" ( _
    > ByVal hServer As Int32, ByVal Reserved As Int32, _
    > ByVal Version As Int32, ByRef ppProcessInfo As IntPtr, _
    > ByRef pCount As Int32) As Int32[/color]

    Since you use Auto on the function ...

    [color=blue]
    > Private Structure WTS_PROCESS_INF O
    > Dim SessionID As Integer
    > Dim ProcessID As Integer
    > Dim ProcessName As String
    > Dim UserSid As Integer
    > End Structure[/color]

    .... you should use it on the structure as well. Add the
    <StructLayout(L ayoutKind.Seque ntial, CharSet:=CharSe t.Auto)>
    attribute.


    Mattias

    --
    Mattias Sjögren [C# MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    Working...