CreateProcessAsUser doesn't draw the GUI

微笑、不失礼 提交于 2019-12-08 02:03:55

问题


I have a windows service running under "SYSTEM" account that checks if a specific application is running for each logged in user. If the application is not running, the service starts it (under corresponding user name).
I'm trying to accomplish my goal using CreateProcessAsUser(). The service does start the application under corresponding user name, but the GUI is not drawn. (Yes, I'm making sure that "Allow service to interact with desktop" check box is enabled).

System: XP SP3, language: C#

Here is some code that might be of interest:

PROCESS_INFORMATION processInfo = new PROCESS_INFORMATION();  
startInfo.cb = Marshal.SizeOf(startInfo);  
startInfo.lpDesktop = "winsta0\\default";  
bResult = Win32.CreateProcessAsUser(hToken, null, strCommand, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startInfo, out processInfo);  

As far as I understand, setting startInfo.lpDesktop = "winsta0\default"; should have used the desktop of corresponding user.
Even contrary to what is stated here: http://support.microsoft.com/kb/165194, I tried setting lpDesktop to null, or not setting it at all, both giving the same result: process was started in the name of expected user and I could see a part of window's title bar. The "invisible" window intercepts mouse click events, handles them as expected. It just doesn't draw itself.

Is anyone familiar with such a problem and knows what am I doing wrong?


回答1:


MSDN has a sample of how to create a process as another user setting explicit permissions on the window station and desktop objects:

CreateProcessAsUser() windowstations and desktops

You can port the code to C# using P/Invoke or you could use a C++/CLI assembly.

However, be aware that your scenario is not supported and likely to break with Vista's (and Windows 7's) Session-0 isolation (download the whitepaper on the right).



来源:https://stackoverflow.com/questions/2584227/createprocessasuser-doesnt-draw-the-gui

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