因为项目的原因今天又搞起了Windows service,由于项目的需求要在windows下show一个messageBox出来.我记得以前曾经搞过,拍胸脯说Ok,这个好办.

我记得windows service call一下win32的MessageBox就搞定了吧

int MessageBox(      
    HWND hWnd,     LPCTSTR lpText,     LPCTSTR lpCaption,     UINT uType );
 

DllImport一下,咦,没有反应.囧

查了一下才知道,这个方式在visit之后就不好使了.我们可以通过WTTSendMessage来发送消息.

BOOL WTSSendMessage(
  __in   HANDLE hServer,
  __in   DWORD SessionId,
  __in   LPTSTR pTitle,
  __in   DWORD TitleLength,
  __in   LPTSTR pMessage,
  __in   DWORD MessageLength,
  __in   DWORD Style,
  __in   DWORD Timeout,
  __out  DWORD *pResponse,
  __in   BOOL bWait
);

安装国际惯例我们包一层供别人更好的使用

 

 1Windows Service下的MessageBoxpublic class MessageBox 
 2

 

使用方法和普通的messagebox没有多少区别,Show方法可以根据自己的喜好重载一下咯,我就不糊代码了!

 

参考:

http://blogs.msdn.com/yvesdolc/archive/2009/08/20/do-you-still-use-the-messagebox-api-in-your-windows-service.aspx

http://msdn.microsoft.com/en-us/library/aa383842(VS.85).aspx

http://www.pinvoke.net/default.aspx/wtsapi32/WTSSendMessage.html

相关文章: