WTSSendMessage弹出窗口
private readonly IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;
public void ShowMessageBox(string message, string title)
{
List<Int32> sessions = GetActiveSessions(WTS_CURRENT_SERVER_HANDLE);
if (sessions.Count < 1)
{
int err = Marshal.GetLastWin32Error();
LogHelper.GetInstance().ErrorLog("No active sessions found: errorCode:"+ err);
}
else
{
int resp = 0;
WTSSendMessage(
WTS_CURRENT_SERVER_HANDLE,
WTSGetActiveConsoleSessionId(),
title, title.Length,
message, message.Length,
0, 0, out resp, false);
}
}
private List<Int32> GetActiveSessions(System.IntPtr server)
{
List<Int32> ret = new List<int>();
IntPtr ppSessionInfo = IntPtr.Zero;
Int32 count = 0;
Int32 retval = WTSEnumerateSessions(server, 0, 1, ref ppSessionInfo, ref count);
Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO));
Int64 current = (int)ppSessionInfo;
if (retval != 0)
{
for (int i = 0; i < count; i++)
{
WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)current, typeof(WTS_SESSION_INFO));
current += dataSize;
if (si.State == WTS_CONNECTSTATE_CLASS.WTSActive)
ret.Add(si.SessionID);
}
WTSFreeMemory(ppSessionInfo);
}
return ret;
}
[DllImport("wtsapi32.dll", ExactSpelling = true, SetLastError = false)]
private static extern void WTSFreeMemory(IntPtr memory);
[StructLayout(LayoutKind.Sequential)]
private struct WTS_SESSION_INFO
{
public readonly int SessionID;
[MarshalAs(UnmanagedType.LPStr)]
private readonly string pWinStationName;
public readonly WTS_CONNECTSTATE_CLASS State;
}
[DllImport("wtsapi32.dll", SetLastError = true)]
static extern int WTSEnumerateSessions(
System.IntPtr hServer,
int Reserved,
int Version,
ref System.IntPtr ppSessionInfo,
ref int pCount);
public enum WTS_CONNECTSTATE_CLASS
{
WTSActive,
WTSConnected,
WTSConnectQuery,
WTSShadow,
WTSDisconnected,
WTSIdle,
WTSListen,
WTSReset,
WTSDown,
WTSInit
}
[DllImport("wtsapi32.dll", SetLastError = true)]
private static extern bool WTSSendMessage(
IntPtr hServer,
int SessionId,
String pTitle,
int TitleLength,
String pMessage,
int MessageLength,
int Style,
int Timeout,
out int pResponse,
bool bWait);
浙公网安备 33010602011771号