Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

WTSOpenServer (wtsapi32)
 
.
Summary
TODO - a short description

C# Signature:

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

VB Signature:

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

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions