WTSEnumerateSessions

  • Thread starter =?ISO-8859-1?Q?M=E9sz=E1ros_Tam=E1s?=
  • Start date
?

=?ISO-8859-1?Q?M=E9sz=E1ros_Tam=E1s?=

Hi!

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

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

struct WTS_SESSION_INFO
{
public int SessionId;
public string pWinStationName;
public WTS_CONNECTSTATE_CLASS State;
}

[DllImport("wtsapi32")]
private static extern bool WTSEnumerateSessions(int hServer, int
reserved, int version, ref WTS_SESSION_INFO[] ppSessionInfo, ref int Count);

....

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

WTSEnumerateSessions(0, 0, 1, ref ppSessionInfo, ref Count );

....
-----------------------------------------------------------------------
After the WTSEnumerateSessions 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
 
N

Nicholas Paldino [.NET/C# MVP]

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_CONNECTSTATE_CLASS
{
WTSActive,
WTSConnected,
WTSConnectQuery, // This was misspelled.
WTSShadow,
WTSDisconnected,
WTSIdle,
WTSListen,
WTSReset,
WTSDown,
WTSInit
};

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
struct WTS_SESSION_INFO
{
public int SessionId;
public string pWinStationName;
public WTS_CONNECTSTATE_CLASS State;
}

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

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

You also need the following definition:

[DllImport("wtsapi32.dll")]
private static extern void WTSFreeMemory(IntPtr 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 (WTSEnumerateSessions(IntPtr.Zero, 0, 1, ref buffer, ref count))
{
// Marshal to a structure array here. Create the array first.
WTS_SESSION_INFO[] sessionInfo = new WTS_SESSION_INFO[count];

// Cycle through and copy the array over.
for (int index = 0; index < count; index++)
// Marshal the value over.
sessionInfo[index] = Marshal.PtrToStructure(buffer +
(sizeof(WTS_SESSION_INFO) * index), typeof(WTS_SESSION_INFO));

// Work with the array here.
}

// Close the buffer.
WTSFreeMemory(buffer);

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mészáros Tamás said:
Hi!

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

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

struct WTS_SESSION_INFO
{
public int SessionId;
public string pWinStationName;
public WTS_CONNECTSTATE_CLASS State;
}

[DllImport("wtsapi32")]
private static extern bool WTSEnumerateSessions(int hServer, int reserved,
int version, ref WTS_SESSION_INFO[] ppSessionInfo, ref int Count);

...

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

WTSEnumerateSessions(0, 0, 1, ref ppSessionInfo, ref Count );

...
-----------------------------------------------------------------------
After the WTSEnumerateSessions 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
 
?

=?ISO-8859-1?Q?M=E9sz=E1ros_Tam=E1s?=

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

Tamas Meszaros
 

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

Similar Threads

VB to C# 3
Problem with WinAPI 1
Marshaling 3
Can anyone covert a VB to VB.Net program for me? 6

Top