03-01-2010 06:39 AM
03-01-2010 07:43 AM
03-01-2010 09:11 AM
Hey Simon -
When I try your code, GetLastError following DestroyWindow returns error 5, which is "Access is denied". That is because DestroyWindow can only be called from the thread that created the window. To see how to cleanly shut down a process, check out this Microsoft KB.
NickB
National Instruments
03-01-2010 09:25 AM
03-01-2010 09:40 AM
Simon -
No - you are right to use EnumWindows, you need to get a reference to the application you want to shut down somehow. However, instead of telling the application to shut down with DestroyWindow, you should instead use something like PostMessage to send a WM_CLOSE message to the window.
Once the message has been posted, you can wait on the handle returned from OpenProcess (using the process id returned from GetWindowThreadProcessId) with WaitForSingleObject. If you timeout, you know that the application you tried to close did not respond to the WM_CLOSE message, and you can then call TerminateProcess on that process.
This is all laid out very clearly in the KB I linked to in my previous post.
NickB
National Instruments
03-01-2010 11:22 AM
03-02-2010 10:30 AM
hey Simon -
I'm not sure what you mean when you say you are unable to open the process. What error do you get? What does GetLastError return? Could you not have permission to open it with terminate rights?
Also - You have a misplaced parentheses in your WaitForSingleObject call - the closing paren should be before the '!=WAIT_OBJECT_0'
NickB
National Instruments
03-03-2010 02:48 AM
03-03-2010 09:09 AM
Hey Simon -
Turns out it was a simple mistake I should have caught the last time I looked. In your UIR_Callback function, you are calling GetWindowThreadProcessId, but are assigning the return value to your dwPID variable. The return value of GetWindowThreadProcessId is the thread ID for the window, and not the process ID, which is required by OpenProcess. The call should instead look like it does in your TerminateAppEnum call:
Once you make this change, I think you should see the program work as expected.
NickB
National Instruments
03-03-2010 10:25 AM