Why does GetWindowThreadProcessId return 0 when called from w3wp.exe

核能气质少年 提交于 2019-12-25 14:17:12

问题


When running the following code, GetWindowThreadProcessId returns 0. I've read about the same problem happening with a service, but this code is being run as an Application Pool in IIS (namely, w3wp.exe). Also, both w3wp.exe and the EXCEL.EXE it is trying to kill are running in the same session (session 0), and as the same user.

if (appClassXls != null)
{
    IntPtr processId = default(IntPtr);
    GetWindowThreadProcessId(appClassXls.Hwnd, out processId);
    Process processXls = Process.GetProcessById(processId.ToInt32());
    if (processXls != null)
    {
        processXls.Kill();
    }
}

Obviously this code fails since it will try to kill pid 0 which is the System Idle Process instead of the Excel Application instance it's trying to kill. Why would GetWindowThreadProcessId return 0 when called from w3wp.exe in the same session as the same user? And how do I make it return the correct pid?

Windows Server 2008 R2 Standard (64 bit) IIS 7.5 .NET Framework v4.0 Microsoft Office Professional Plus 2010 (clean install, set to launch as specific user in DCOM setting, launched by w3wp.exe)

UPDATE (2011-02-17 08:33 UTC): I just realized that w3wp.exe is launched by "svchost.exe -k iissvcs" which corresponds to the "World Wide Web Publishing Service" and the "Windows Process Activation Service" and EXCEL.EXE is launched by "svchost.exe -k DcomLaunch" which corresponds to "DCOM Server Process Launcher", all 3 of which are services. Both of the svchost.exe's are running as SYSTEM in session 0 though, so I still don't see why they or their children would have trouble accessing each other.


回答1:


See this blog entry which explains how Session 0 isolation works. It looks like services and applications cannot talk to each other. I am trying to figure out how "session 0" is assigned. I believe the window station w3wp is assigned to is not the same as the one for the excel (I assume) it launched.

I recommend avoiding launching excel from a web application. You can easily hang the server doing that. I remember several issues due to reference count leaks on the excel causing memory issues that would ultimately lead to the server's demise. :)



来源:https://stackoverflow.com/questions/5024788/why-does-getwindowthreadprocessid-return-0-when-called-from-w3wp-exe

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