Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than wtsapi32, prefix the name with the module name and a period.
WTSQuerySessionInformation (wtsapi32)
.
C# Signature:
/// <summary>
/// The WTSQuerySessionInformation function retrieves session information for the specified
/// session on the specified terminal server.
/// It can be used to query session information on local and remote terminal servers.
/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/termserv/termserv/wtsquerysessioninformation.asp
/// </summary>
/// <param name="hServer">Handle to a terminal server. Specify a handle opened by the WTSOpenServer function,
/// or specify <see cref="WTS_CURRENT_SERVER_HANDLE"/> to indicate the terminal server on which your application is running.</param>
/// <param name="sessionId">A Terminal Services session identifier. To indicate the session in which the calling application is running
/// (or the current session) specify <see cref="WTS_CURRENT_SESSION"/>. Only specify <see cref="WTS_CURRENT_SESSION"/> when obtaining session information on the
/// local server. If it is specified when querying session information on a remote server, the returned session
/// information will be inconsistent. Do not use the returned data in this situation.</param>
/// <param name="wtsInfoClass">Specifies the type of information to retrieve. This parameter can be one of the values from the <see cref="WTSInfoClass"/> enumeration type. </param>
/// <param name="ppBuffer">Pointer to a variable that receives a pointer to the requested information. The format and contents of the data depend on the information class specified in the <see cref="WTSInfoClass"/> parameter.
/// To free the returned buffer, call the <see cref="WTSFreeMemory"/> function. </param>
/// <param name="pBytesReturned">Pointer to a variable that receives the size, in bytes, of the data returned in ppBuffer.</param>
/// <returns>If the function succeeds, the return value is a nonzero value.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
/// </returns>
[DllImport("Wtsapi32.dll")]
public static extern bool WTSQuerySessionInformation(
System.IntPtr hServer, int sessionId, WTSInfoClass wtsInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned);
VB .NET Signature:
Private Declare Auto Function WTSQuerySessionInformation Lib "wtsapi32.dll" ( _
ByVal hServer As Int32, _
ByVal SessionId As Int32, _
ByVal InfoClass As WTS_INFO_CLASS, _
ByRef ppBuffer As IntPtr, _
ByRef pCount As Int32) As Int32
[DllImport("Wtsapi32.dll")]
private static extern bool WTSQuerySessionInformation(IntPtr hServer, int sessionId, WTS_INFO_CLASS wtsInfoClass, out IntPtr ppBuffer, out int pBytesReturned);
Sample Code:
using System;
using System.Runtime.InteropServices;
[DllImport("Wtsapi32.dll")]
private static extern void WTSFreeMemory(IntPtr pointer);
namespace TerminalServer
{
/// <summary>
/// Static helper methods.
/// </summary>
internal class Utilities
{
/// <summary>
/// The WTSQuerySessionInformation function retrieves session information for the specified
/// session on the specified terminal server.
/// It can be used to query session information on local and remote terminal servers.
/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/termserv/termserv/wtsquerysessioninformation.asp
/// </summary>
/// <param name="hServer">Handle to a terminal server. Specify a handle opened by the WTSOpenServer function,
/// or specify <see cref="WTS_CURRENT_SERVER_HANDLE"/> to indicate the terminal server on which your application is running.</param>
/// <param name="sessionId">A Terminal Services session identifier. To indicate the session in which the calling application is running
/// (or the current session) specify <see cref="WTS_CURRENT_SESSION"/>. Only specify <see cref="WTS_CURRENT_SESSION"/> when obtaining session information on the
/// local server. If it is specified when querying session information on a remote server, the returned session
/// information will be inconsistent. Do not use the returned data in this situation.</param>
/// <param name="wtsInfoClass">Specifies the type of information to retrieve. This parameter can be one of the values from the <see cref="WTSInfoClass"/> enumeration type. </param>
/// <param name="ppBuffer">Pointer to a variable that receives a pointer to the requested information. The format and contents of the data depend on the information class specified in the <see cref="WTSInfoClass"/> parameter.
/// To free the returned buffer, call the <see cref="WTSFreeMemory"/> function. </param>
/// <param name="pBytesReturned">Pointer to a variable that receives the size, in bytes, of the data returned in ppBuffer.</param>
/// <returns>If the function succeeds, the return value is a nonzero value.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
/// </returns>
[DllImport("Wtsapi32.dll")]
public static extern bool WTSQuerySessionInformation(
System.IntPtr hServer, int sessionId, WTSInfoClass wtsInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned);
public static String GetUsernameBySessionId(int sessionId)
{
IntPtr buffer;
int strLen;
var username = "SYSTEM"; // assume SYSTEM as this will return "\0" below
if (WTSQuerySessionInformation(IntPtr.Zero, sessionId, WTS_INFO_CLASS.WTSUserName, out buffer, out strLen) && strLen > 1)
{
username = Marshal.PtrToStringAnsi(buffer); // don't need length as these are null terminated strings
WTSFreeMemory(buffer);
if (WTSQuerySessionInformation(IntPtr.Zero, sessionId, WTS_INFO_CLASS.WTSDomainName, out buffer, out strLen) && strLen > 1)
{
username = Marshal.PtrToStringAnsi(buffer)+"\\"+username; // prepend domain name
WTSFreeMemory(buffer);
}
}
return username;
}
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/termserv/termserv/wtsgetactiveconsolesessionid.asp
/// <summary>
/// The WTSGetActiveConsoleSessionId function retrieves the
/// Terminal Services session currently attached to the physical console.
/// The physical console is the monitor, keyboard, and mouse.
/// </summary>
/// <returns>An <see cref="int"/> equal to 0 indicates that the current session is attached to the physical console.</returns>
/// <remarks>It is not necessary that Terminal Services be running for this function to succeed.</remarks>
[DllImport("Kernel32.dll")]
public static extern int WTSGetActiveConsoleSessionId();
Sample Code:
using System;
using System.Runtime.InteropServices;
/// <summary>
/// The WTSFreeMemory function frees memory allocated by a Terminal Services function.
/// </summary>
/// <param name="memory">Pointer to the memory to free.</param>
[DllImport("wtsapi32.dll", ExactSpelling=true, SetLastError=false)]
public static extern void WTSFreeMemory( IntPtr memory );
namespace TerminalServer
{
/// <summary>
/// Static helper methods.
/// </summary>
internal class Utilities
{
/// <summary>
/// The WTSQuerySessionInformation function retrieves session information for the specified
/// session on the specified terminal server.
/// It can be used to query session information on local and remote terminal servers.
/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/termserv/termserv/wtsquerysessioninformation.asp
/// </summary>
/// <param name="hServer">Handle to a terminal server. Specify a handle opened by the WTSOpenServer function,
/// or specify <see cref="WTS_CURRENT_SERVER_HANDLE"/> to indicate the terminal server on which your application is running.</param>
/// <param name="sessionId">A Terminal Services session identifier. To indicate the session in which the calling application is running
/// (or the current session) specify <see cref="WTS_CURRENT_SESSION"/>. Only specify <see cref="WTS_CURRENT_SESSION"/> when obtaining session information on the
/// local server. If it is specified when querying session information on a remote server, the returned session
/// information will be inconsistent. Do not use the returned data in this situation.</param>
/// <param name="wtsInfoClass">Specifies the type of information to retrieve. This parameter can be one of the values from the <see cref="WTSInfoClass"/> enumeration type. </param>
/// <param name="ppBuffer">Pointer to a variable that receives a pointer to the requested information. The format and contents of the data depend on the information class specified in the <see cref="WTSInfoClass"/> parameter.
/// To free the returned buffer, call the <see cref="WTSFreeMemory"/> function. </param>
/// <param name="pBytesReturned">Pointer to a variable that receives the size, in bytes, of the data returned in ppBuffer.</param>
/// <returns>If the function succeeds, the return value is a nonzero value.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
/// </returns>
[DllImport("Wtsapi32.dll")]
public static extern bool WTSQuerySessionInformation(
System.IntPtr hServer, int sessionId, WTSInfoClass wtsInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned);
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/termserv/termserv/wtsgetactiveconsolesessionid.asp
/// <summary>
/// The WTSGetActiveConsoleSessionId function retrieves the
/// Terminal Services session currently attached to the physical console.
/// The physical console is the monitor, keyboard, and mouse.
/// </summary>
/// <returns>An <see cref="int"/> equal to 0 indicates that the current session is attached to the physical console.</returns>
/// <remarks>It is not necessary that Terminal Services be running for this function to succeed.</remarks>
[DllImport("Kernel32.dll")]
public static extern int WTSGetActiveConsoleSessionId();
/// <summary>
/// The WTSFreeMemory function frees memory allocated by a Terminal Services function.
/// </summary>
/// <param name="memory">Pointer to the memory to free.</param>
[DllImport("wtsapi32.dll", ExactSpelling=true, SetLastError=false)]
public static extern void WTSFreeMemory( IntPtr memory );
public const int WTS_CURRENT_SESSION = -1;
/// <summary>
/// Gets a <see cref="bool"/> indicating if the current ehmsas.exe is being run locally or in a Remote session.
/// </summary>
/// <returns>Returns true if the process is being executed locally or from a Terminal Server session.</returns>
internal static bool GetIsRunningLocally()
{
System.IntPtr buffer = IntPtr.Zero;
uint bytesReturned;
int sessionID;
try
{
bool sessionInfo = WTSQuerySessionInformation( System.IntPtr.Zero, WTS_CURRENT_SESSION, WTSInfoClass.WTSSessionId, out buffer, out bytesReturned );
sessionID = Marshal.ReadInt32( buffer );
Contains values that indicate the type of session information to retrieve in a call to the WTSQuerySessionInformation function.
11/30/2011 7:29:47 AM - -128.104.196.79
TODO - a short description
2/25/2008 3:31:28 AM - -86.67.222.44
The WTSGetActiveConsoleSessionId function retrieves the Terminal Services session currently attached to the physical console. The physical console is the monitor, keyboard, and mouse.
10/28/2014 9:07:43 AM - s_verdiesen@hotmail.com-89.146.46.169
The WTSFreeMemory function frees memory allocated by a Terminal Services function.
7/12/2011 7:01:29 AM - -87.178.3.148
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).