WTSEnumerateSessions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mészáros Tamás

    WTSEnumerateSessions

    Hi!

    I want to get information about the state of TS-Sessions, and I've tried
    it the following way:

    ------------------------------------------------------
    enum WTS_CONNECTSTAT E_CLASS
    {
    WTSActive,
    WTSConnected,
    TSConnectQuery,
    WTSShadow,
    WTSDisconnected ,
    WTSIdle,
    WTSListen,
    WTSReset,
    WTSDown,
    WTSInit
    };

    struct WTS_SESSION_INF O
    {
    public int SessionId;
    public string pWinStationName ;
    public WTS_CONNECTSTAT E_CLASS State;
    }

    [DllImport("wtsa pi32")]
    private static extern bool WTSEnumerateSes sions(int hServer, int
    reserved, int version, ref WTS_SESSION_INF O[] ppSessionInfo, ref int Count);

    ....

    WTS_SESSION_INF O[] ppSessionInfo = new WTS_SESSION_INF O[20];
    /*I must initialize it, because I can't pass it to a function
    uninitialized*/
    int Count = 0;
    WTS_SESSION_INF O wts;

    WTSEnumerateSes sions(0, 0, 1, ref ppSessionInfo, ref Count );

    ....
    -----------------------------------------------------------------------
    After the WTSEnumerateSes sions function returns, I can find the correct
    number in the Count variable, but the ppSessionInfo allways has only one
    element and it doesn't contain any information about the other sessions.

    Could someone correct this code-part or send me a working example?

    Thanks in advance,

    Tamas Meszaros
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: WTSEnumerateSes sions

    Mészáros,

    The reason for this is because the runtime doesn't know how many
    elements to marshal back from the unmanaged realm.

    Also, a few of your definitions are off. Here are the definitions you
    should have:

    enum WTS_CONNECTSTAT E_CLASS
    {
    WTSActive,
    WTSConnected,
    WTSConnectQuery , // This was misspelled.
    WTSShadow,
    WTSDisconnected ,
    WTSIdle,
    WTSListen,
    WTSReset,
    WTSDown,
    WTSInit
    };

    [StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Auto)]
    struct WTS_SESSION_INF O
    {
    public int SessionId;
    public string pWinStationName ;
    public WTS_CONNECTSTAT E_CLASS State;
    }

    You need to define the function differently as well as make some
    corrections.

    [DllImport("wtsa pi32.dll", CharSet=CharSet .Auto)]
    private static extern bool WTSEnumerateSes sions(
    // Always use IntPtr for handles, and then something derived from
    SafeHandle in .NET 2.0
    IntPtr hServer,
    // Not required, but a good practice.
    [MarshalAs(Unman agedType.U4)]
    int Reserved,
    [MarshalAs(Unman agedType.U4)]
    int Version,
    // You are going to create the memory block yourself.
    ref IntPtr ppSessionInfo,
    [MarshalAs(Unman agedType.U4)]
    ref int pCount);

    You also need the following definition:

    [DllImport("wtsa pi32.dll")]
    private static extern void WTSFreeMemory(I ntPtr pMemory);

    Now, to make the call, you do this:

    // Create the pointer that will get the buffer.
    IntPtr buffer = IntPtr.Zero;

    // The count.
    int count = 0;

    // Make the call.
    if (WTSEnumerateSe ssions(IntPtr.Z ero, 0, 1, ref buffer, ref count))
    {
    // Marshal to a structure array here. Create the array first.
    WTS_SESSION_INF O[] sessionInfo = new WTS_SESSION_INF O[count];

    // Cycle through and copy the array over.
    for (int index = 0; index < count; index++)
    // Marshal the value over.
    sessionInfo[index] = Marshal.PtrToSt ructure(buffer +
    (sizeof(WTS_SES SION_INFO) * index), typeof(WTS_SESS ION_INFO));

    // Work with the array here.
    }

    // Close the buffer.
    WTSFreeMemory(b uffer);

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Mészáros Tamás" <mesztam@sch.bm e.hu> wrote in message
    news:uC6hXeKoEH A.536@TK2MSFTNG P11.phx.gbl...[color=blue]
    > Hi!
    >
    > I want to get information about the state of TS-Sessions, and I've tried
    > it the following way:
    >
    > ------------------------------------------------------
    > enum WTS_CONNECTSTAT E_CLASS
    > {
    > WTSActive,
    > WTSConnected,
    > TSConnectQuery,
    > WTSShadow,
    > WTSDisconnected ,
    > WTSIdle,
    > WTSListen,
    > WTSReset,
    > WTSDown,
    > WTSInit
    > };
    >
    > struct WTS_SESSION_INF O
    > {
    > public int SessionId;
    > public string pWinStationName ;
    > public WTS_CONNECTSTAT E_CLASS State;
    > }
    >
    > [DllImport("wtsa pi32")]
    > private static extern bool WTSEnumerateSes sions(int hServer, int reserved,
    > int version, ref WTS_SESSION_INF O[] ppSessionInfo, ref int Count);
    >
    > ...
    >
    > WTS_SESSION_INF O[] ppSessionInfo = new WTS_SESSION_INF O[20]; /*I must
    > initialize it, because I can't pass it to a function uninitialized*/
    > int Count = 0;
    > WTS_SESSION_INF O wts;
    >
    > WTSEnumerateSes sions(0, 0, 1, ref ppSessionInfo, ref Count );
    >
    > ...
    > -----------------------------------------------------------------------
    > After the WTSEnumerateSes sions function returns, I can find the correct
    > number in the Count variable, but the ppSessionInfo allways has only one
    > element and it doesn't contain any information about the other sessions.
    >
    > Could someone correct this code-part or send me a working example?
    >
    > Thanks in advance,
    >
    > Tamas Meszaros[/color]


    Comment

    • Mészáros Tamás

      #3
      Re: WTSEnumerateSes sions

      with small modifications it has worked fine, thanks a lot.

      Tamas Meszaros

      Nicholas Paldino [.NET/C# MVP] wrote:[color=blue]
      > Mészáros,
      >
      > The reason for this is because the runtime doesn't know how many
      > elements to marshal back from the unmanaged realm.
      >
      > Also, a few of your definitions are off. Here are the definitions you
      > should have:
      >
      > enum WTS_CONNECTSTAT E_CLASS
      > {
      > WTSActive,
      > WTSConnected,
      > WTSConnectQuery , // This was misspelled.
      > WTSShadow,
      > WTSDisconnected ,
      > WTSIdle,
      > WTSListen,
      > WTSReset,
      > WTSDown,
      > WTSInit
      > };
      >
      > [StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Auto)]
      > struct WTS_SESSION_INF O
      > {
      > public int SessionId;
      > public string pWinStationName ;
      > public WTS_CONNECTSTAT E_CLASS State;
      > }
      >
      > You need to define the function differently as well as make some
      > corrections.
      >
      > [DllImport("wtsa pi32.dll", CharSet=CharSet .Auto)]
      > private static extern bool WTSEnumerateSes sions(
      > // Always use IntPtr for handles, and then something derived from
      > SafeHandle in .NET 2.0
      > IntPtr hServer,
      > // Not required, but a good practice.
      > [MarshalAs(Unman agedType.U4)]
      > int Reserved,
      > [MarshalAs(Unman agedType.U4)]
      > int Version,
      > // You are going to create the memory block yourself.
      > ref IntPtr ppSessionInfo,
      > [MarshalAs(Unman agedType.U4)]
      > ref int pCount);
      >
      > You also need the following definition:
      >
      > [DllImport("wtsa pi32.dll")]
      > private static extern void WTSFreeMemory(I ntPtr pMemory);
      >
      > Now, to make the call, you do this:
      >
      > // Create the pointer that will get the buffer.
      > IntPtr buffer = IntPtr.Zero;
      >
      > // The count.
      > int count = 0;
      >
      > // Make the call.
      > if (WTSEnumerateSe ssions(IntPtr.Z ero, 0, 1, ref buffer, ref count))
      > {
      > // Marshal to a structure array here. Create the array first.
      > WTS_SESSION_INF O[] sessionInfo = new WTS_SESSION_INF O[count];
      >
      > // Cycle through and copy the array over.
      > for (int index = 0; index < count; index++)
      > // Marshal the value over.
      > sessionInfo[index] = Marshal.PtrToSt ructure(buffer +
      > (sizeof(WTS_SES SION_INFO) * index), typeof(WTS_SESS ION_INFO));
      >
      > // Work with the array here.
      > }
      >
      > // Close the buffer.
      > WTSFreeMemory(b uffer);
      >
      > Hope this helps.
      >
      >[/color]

      Comment

      Working...