WTSOpenServer (wtsapi32)
Last changed: -165.86.81.72

.
Summary
TODO - a short description

C# Signature:

[DllImport("wtsapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
static extern IntPtr WTSOpenServer(string pServerName);

VB Signature:

    <DllImport("wtsapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
     Private Shared Function WTSOpenServer(ByVal pServerName As String) As IntPtr
    End Function

User-Defined Types:

None.

Notes:

As always, only do SetLastError=true if you actually intend to call GetLastError.

Tips & Tricks:

Please add some!

Sample Code:

[DllImport(@"wtsapi32.dll", SetLastError=true)]

static extern IntPtr WTSOpenServer(string serverName);

[DllImport(@"wtsapi32.dll", SetLastError=true)]

static extern bool WTSEnumerateSessions(IntPtr hServer, int Reserved, int Version, out IntPtr SessionInfo, ref int Count);

[StructLayout (LayoutKind.Sequential, CharSet=CharSet.Ansi)]

public struct WTS_SESSION_INFO

{

    public int SessionId;
    public string pWinStationName;  
    public WTS_CONNECTSTATE_CLASS State;

}

public enum WTS_CONNECTSTATE_CLASS

{

    /// <summary>
    /// user is logged on to the WinStation.
    /// </summary>
    WTSActiveWTSActiveA,

    /// <summary>
    /// The WinStation is connected to the client.
    /// </summary>
    WTSConnectedWTSConnected,

    /// <summary>
    /// The WinStation is in the process of connecting to the client.
    /// </summary>
    WTSConnectQueryWTSConnectQuery,

    /// <summary>
    /// The WinStation is shadowing another WinStation.
    /// </summary>
    WTSShadowWTSShadow,

    /// <summary>
    /// The WinStation is active but the client is disconnected.
    /// </summary>
    WTSDisconnectedWTSDisconnected,

    /// <summary>
    /// The Win Station is waiting for a client to connect.
    /// </summary>
    WTSIdleWTSIdle,

    /// <summary>
    /// The WinStation is listening for a connection. A listener session waits for requests for new client connections.
    /// No user is logged on a listener session. A listener session cannot be reset, shadowed, or changed to a regular client session.
    /// </summary>
    WTSListenWTSListen,

    /// <summary>
    /// The WinStation is being reset.
    /// </summary>
    WTSResetWTSReset,

    /// <summary>
    /// The WinStation is down due to an error.
    /// </summary>
    WTSDownWTSDown,

    /// <summary>
    /// The WinStation is initializing.
    /// </summary>
        WTSInitWTSInit

}

public WTS_SESSION_INFO[] GetSessions(string serveName)

    {
        WTS_SESSION_INFO[] sessionInfos;
        int Count=0;
        IntPtr hServer = new IntPtr(0);
        hServer = WTSOpenServer(serveName);

        if(hServer == IntPtr.Zero)
        {
             int ret = Marshal.GetLastWin32Error();
            string msg = string.Format("\nError: [{0}] {1}\n", ret, GetErrorMessage(ret));
            throw new ApplicationException(msg);
        }

        IntPtr ppSessionInfos = new IntPtr(0);
        int sizeOfSessionInfo = Marshal.SizeOf(typeof(WTS_SESSION_INFO));
        WTSEnumerateSessions(hServer, 0, 1,out ppSessionInfos, ref Count);
        sessionInfos = new WTS_SESSION_INFO[Count];

        for(int i=0; i < Count; i++)
        {
            sessionInfos[i] = (WTS_SESSION_INFO)Marshal.PtrToStructure(ppSessionInfos, typeof(WTS_SESSION_INFO));        
        }

        return sessionInfos;
    }

Alternative Managed API:

Do you know one? Please contribute it!

Documentation