Displaying Message Box using .NET Windows Services In Windows 7

99封情书 提交于 2019-12-06 02:22:46

You've declared WTSGetActiveConsoleSessionId but then never called it. That function returns the session ID that is currently active. That's the session ID that you must pass to WTSSendMessage.

Your current code is attempting to display the message in the service's session, session 0. Not what you intended.

Before you can call the function though, you must correct its declaration. You have spelled it incorrectly. It is named WTSGetActiveConsoleSessionId.

I believe that the string length parameters need to account for the zero terminator. Add 1 to the values that you pass. If you switched to the Unicode API then you'd have to multiply these values by 2 since they are measured in bytes rather than characters.

One more point. Only ever ask for the error code when the API call fails. So in your code you must only call Marshal.GetLastWin32Error if result is false.

The call to WTSSendMessage() specifies WTS_CURRENT_SESSION, which means "show the message box on the current session". When you run as a service, you are on session 0, which is isolated on Windows 7. You surely don't want your message box to show up there!

To show up on a regular user's session, you have to provide a non-zero session ID. See the WTSSendMessage() documentation for details.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!