Asked by:
GetWindowThreadProcessId is returning 0 on windows server 2012R2

Question
-
User-1681336077 posted
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 11:26 AM
All replies
-
User753101303 posted
Hi,
You could likelly use https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.getprocesses?view=netframework-4.8 instead to find "excel.exe". I suspect it is 0 because there is no active desktop session on which a window could be shown.
Make sure to do that only if you have a real problem (ie Excel doesn't close properly a common problem being blocked by a dialog with nobody to answer, you can disable them).
It is also strongly suggested to avoid using an end user interactive application as a programming library.
Thursday, October 17, 2019 11:49 AM -
User-1681336077 posted
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).
I need not find all processes, need to find the excel one.
Thanks,
Salil
</div> </div> </div>
Friday, October 18, 2019 3:55 AM -
User-719153870 posted
Hi Salil,
Usually, this Excel.Application library is not suggested to set in your server, sometimes it will throw error.
@PatriceSc's recommendation is helpful, or you can provide your related code to see if we can reproduce your problem and even solve it.
Again, it's not suggested in your server side.
Best Regard,
Yang Shen
Friday, October 18, 2019 8:50 AM -
User753101303 posted
Ok and so what if trying something such as
var excelInstances=Process.GetProcessesByName("EXCEL");
to find directly the Excel process rather than going through a UI related stuff on a server that doesn't show any UI ???
More likely you have the problem I alluded ealier when using an interactive application as if it is a programming library (ie Excel likely waits for some user input such as telling a file can't be written or if you want to quit Excel without saving etc...).
Edit: on your machine you do have a desktop session unlike the server (and you may have different settings or permission for Excel dialogs, it could even ask something on the frst launch etc...).
Friday, October 18, 2019 9:31 AM -
User-1681336077 posted
Hi,
I need to find that specific excel process which I am using to create report. Below code will find all excel process instances on server and kill them.
Please help me finding that specific excel process.
Thanks,
Salil
Tuesday, October 22, 2019 4:01 AM -
User753101303 posted
Ok but what about past suggestions? What if you try System.Diagnostics.Process.GetProcessesByName("Excel")?
Tuesday, October 22, 2019 7:45 AM -
User-1681336077 posted
This will find all excel processes on server, I need to find the one I created.
Tuesday, October 22, 2019 8:45 AM -
User753101303 posted
How do you launch Excel ? The https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.start?view=netframework-4.8 method returns the process you created....
I Would try first to double check what happens but my guess is that you have a dialog that prevents Excel to go further...
Then at a later time I would reconsider using Excel as a programming library using the OpenXML SDK or whatever instead....
Tuesday, October 22, 2019 9:17 AM