using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace MS.SDKMSDNKeywordIndex
{
class NativeMethods
{
[DllImport("Wtsapi32.dll")]
private static extern bool WTSQuerySessionInformation(System.IntPtr hServer, int sessionId, WTSInfoClass wtsInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned);
[DllImport("wtsapi32.dll", ExactSpelling = true, SetLastError = false)]
private static extern void WTSFreeMemory(IntPtr memory);
private enum WTSInfoClass
{
WTSInitialProgram,
WTSApplicationName,
WTSWorkingDirectory,
WTSOEMId,
WTSSessionId,
WTSUserName,
WTSWinStationName,
WTSDomainName,
WTSConnectState,
WTSClientBuildNumber,
WTSClientName,
WTSClientDirectory,
WTSClientProductId,
WTSClientHardwareId,
WTSClientAddress,
WTSClientDisplay,
WTSClientProtocolType
}
private static IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;
private static int WTS_CURRENT_SESSION = -1;
public static int GetSessionNumber()
{
IntPtr pSessionId = IntPtr.Zero;
Int32 sessionId = 0;
uint bytesReturned;
try
{
bool retVal = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSInfoClass.WTSSessionId, out pSessionId, out bytesReturned);
if (retVal)
sessionId = Marshal.ReadInt32(pSessionId);
}
finally
{
if (pSessionId != IntPtr.Zero)
WTSFreeMemory(pSessionId);
}
return sessionId;
}
}
}