none
GetWindowThreadProcessId is returning 0 on windows server 2012R2 RRS feed

  • Question

  • Hi Team,

    On windows server 2012R2, using GetWindowThreadProcessId, I am unable to get process id of object of an excel application(Excel.Application) though the Hwnd of this object is getting send correctly. 

    On Dev machine(windows 7), this process id is getting returned correctly, looks like some issue with the server.

    I need to find above process id so that I could so that I could kill it(after finding all excel processes on the server).

    Can you please help me resolve this.

    Thanks,

    Salil

    Thursday, October 17, 2019 9:52 AM

All replies

  • It's likely that the HWND you are passing to GetWindowThreadProcessId is not valid if the function returns 0.

    Immediately after the function fails, obtain the last error code with Marshal.GetLastWin32Error().  I expect it will return error code 1400 (Invalid window handle).

    On Windows Server2012R2 is Excel running in an interactive session for a logged-on user?


    • Edited by RLWA32 Thursday, October 17, 2019 12:33 PM added question
    Thursday, October 17, 2019 11:54 AM
  • I need to find above process id so that I could so that I could kill it(after finding all excel processes on the server).

    The term kill varies in definition depending on the operating system. In Android it is normal to kill processes. In Windows Microsoft recommends never killing any process unless something abnormal has happened and killing is a last resort.

    Also see Considerations for server-side Automation of Office.



    Sam Hobbs
    SimpleSamples.Info

    Thursday, October 17, 2019 3:48 PM
  • Wouldn't it be much simpler to just call CloseWindow on the window handles you get back?

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Thursday, October 17, 2019 8:48 PM
  • Hi All,

    Wanted to mention that I am using an ASP.net MVC application for this with c# as language. After a report generation my excel object is not getting closed and lies open on the server. Applied a no. of ways to close the excel objects but nothing is working so thought of killing that particular process.

    Using  GetWindowThreadProcessId, on my dev machine(win 7), I am able to close the excel process but as mentioned above it does not closes on server(windows server 2012 R2).

    Not sure how to find if excel is running interactive session for logged on user.

    Thanks,

    Salil

    Friday, October 18, 2019 3:54 AM
  • The article I referenced says:

    a number of complications can occur if Automation takes place from server-side code such as Microsoft Active Server Pages (ASP), ASP.NET ...

    And there is no guarantee of anything. It is not guaranteed to fail. If it works you are lucky but it is careless to depend on that luck for production systems.



    Sam Hobbs
    SimpleSamples.Info

    Friday, October 18, 2019 5:01 AM
  • Additionally, Microsoft does not support automation of Office applications in an ASP.Net environment.

    "Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment."

    >>Not sure how to find if excel is running interactive session for logged on user.

    By definition, a logged-on user at the console is in an interactive session.

    Friday, October 18, 2019 8:48 AM
  • Hi Salil,

    Thank you for posting here.

    For your question, you want to find some processes and kill them.

    Sorry for not having your work environment, I can't completely reproduce your problem.

    But I recommend that you could use Process to get these threads.

    You could try the following code to get it.

    Process[] processes = Process.GetProcessesByName("EXCEL");
    foreach (var item in processes)
    {
        item.Kill();
    }
    

    Hope my solution could be helpful.

    Best regards,
    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 18, 2019 9:37 AM
  • Hi,

    This will kill all Excel processes running on the server. I need to close the one that just got produced after I produced a report.

    Thanks

    Salil

    Friday, October 18, 2019 11:51 AM
  • Have you tried to obtain the error code after GetWindowThreadProcessId fails as I suggested earlier?
    Friday, October 18, 2019 12:10 PM
  • Additionally, Microsoft does not support automation of Office applications in an ASP.Net environment.

    I already said that twice. The first time I posted a link to the article.



    Sam Hobbs
    SimpleSamples.Info

    Friday, October 18, 2019 4:36 PM
  • Additionally, Microsoft does not support automation of Office applications in an ASP.Net environment.

    I already said that twice. The first time I posted a link to the article.

    Actually you never said it explicitly in any post although it was indeed contained in the referenced link.  And referring to "...a number of complications..." didn't explicitly convey Microsoft's lack of support.

    So what's the problem with a quote from the referenced link to add clarity?


    Friday, October 18, 2019 6:06 PM
  • So what's the problem with a quote from the referenced link to add clarity?


    I did that in my second attempt to get the point across.



    Sam Hobbs
    SimpleSamples.Info

    Friday, October 18, 2019 6:27 PM
  • Hi,

    Yes, it coming as 1400, please let me know if there is a way of killing particular excel process for the scenario I mentioned above (on server windows 2012r2).

    Thanks

    Tuesday, October 22, 2019 4:46 AM
  • Hi All,

    Thank you All for making me clear on Automation.

    Wanted to mention, if it is possible to kill the excel process for scenario that I mentioned above (on server windows 2012r2).

    Thanks,

    Salil

    Tuesday, October 22, 2019 4:49 AM
  • Hi All,

    Can you please make me clear on how above scenario is mentioned as office automation.

    I understand that Microsoft does not support automation of Office applications in an ASP.Net environment but wanted to understand how above process could be termed as Office automation.

    Thanks,

    Salil

    Tuesday, October 22, 2019 11:15 AM
  • Hi All,

    Can you please make me clear on how above scenario is mentioned as office automation.

    I understand that Microsoft does not support automation of Office applications in an ASP.Net environment but wanted to understand how above process could be termed as Office automation.

    Thanks,

    Salil

    Earlier you wrote "On windows server 2012R2, using GetWindowThreadProcessId, I am unable to get process id of object of an excel application(Excel.Application) though the Hwnd of this object is getting send correctly. "

    The reference to "Excel.Application", which is the COM progid for Excel's Application Object as well as references to the Hwnd property of that object indicates that you are automating Excel.


    • Edited by RLWA32 Tuesday, October 22, 2019 11:24 AM
    Tuesday, October 22, 2019 11:20 AM
  • Hi,

    Wanted to understand how above process is related to Automation.

    Issue is still the same  - On windows server 2012R2, using GetWindowThreadProcessId, I am unable to get process id of object of an excel application(Excel.Application), Other update is after calling GetWindowThreadProcessId(on windows 2012 server) the error code 1400.

    So, can you please mention is there a way through which I could get process id of Excel.Application object on windows 2012 server so that I could kill the process.

    Thanks,

    Salil

    Tuesday, October 22, 2019 11:29 AM
  • I'm not an ASP.Net programmer.

    Can you identify the windows session (sometimes called terminal server session) in which your application is running and use that to identify the specific Excel process in the array returned by -

    Process[] processes = Process.GetProcessesByName("EXCEL");

    by checking the Process.SessionId Property?

    Tuesday, October 22, 2019 11:36 AM