Tuesday, 10 September 2013

Displaying Message Box using .NET Windows Services In Windows 7

Displaying Message Box using .NET Windows Services In Windows 7

Displaying Message Box using .NET Windows Services In Windows 7
We had a windows service that was used to display a confirmation message
box once a user scanned their access card on windows xp but once we
migrated to windows 7, that pop up functionality no longer works. As
described in this post Displaying Message Box using .NET Windows Services
In Windows 7 , i have followed the instruction and it works when i run the
application as console application but when i install it as a windows
service it does not work. below is my code. Also the result variable is
returning false and the err variable is returning 5.
class Class1
{
public static IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;
public static int WTS_CURRENT_SESSION = -1;
public void test()
{
bool result = false;
string title = "Hello";
int tlen = title.Length;
string msg = "Terminal Service!";
int mlen = msg.Length;
int resp = 0;
result = WTSSendMessage(WTS_CURRENT_SERVER_HANDLE,
WTS_CURRENT_SESSION, title, tlen, msg, mlen, 0, 0, out resp,
true);
int err = Marshal.GetLastWin32Error();
System.Console.WriteLine("result:{0}, errorCode:{1},
response:{2}", result, err, resp);
}
[DllImport("wtsapi32.dll", SetLastError = true)]
static extern bool WTSSendMessage(
IntPtr hServer,
[MarshalAs(UnmanagedType.I4)] int SessionId,
String pTitle,
[MarshalAs(UnmanagedType.U4)] int TitleLength,
String pMessage,
[MarshalAs(UnmanagedType.U4)] int MessageLength,
[MarshalAs(UnmanagedType.U4)] int Style,
[MarshalAs(UnmanagedType.U4)] int Timeout,
[MarshalAs(UnmanagedType.U4)] out int pResponse,
bool bWait);
[DllImport("Kernel32.dll", SetLastError = true)]
static extern int WTSGetActiveConsoleSessionID();
}

No comments:

Post a Comment