Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open Process Failing

Status
Not open for further replies.

ianwin

Programmer
Jul 5, 2005
44
US
Using VB6 I am attempting to terminate a process which is running under a different user to the VB application.

I have got the process ID but when attempting to get a handle to the process using the following code I always get a return value of 0.

handle = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, ProcessID)

Both PROCESS_QUERY_INFORMATION and PROCESS_VM_READ have been setup as constants with values 1024 and 16 respectivly.

Can anyone help?

Thanks,

-Ian
 
You can call GetLastError() after the failure of OpenProcess to find out why it failed.
I expect it will return 5 (ERROR_ACCESS_DENIED) because I don't think you are allowed to open a process of another user.

Marcel
 
I have check this and I am getting ERROR_ACCESS_DENIED is there anyway around this as I am attempting to overwrite a .exe file for a software upgrade and need to close the application so that the exe can be overwritten.

Thanks,

-Ian
 
Two ways of doing that:

1. Using FindWindow to search for the main window of the application and call SendMessage to send it a WM_CLOSE or WM_QUIT message.

2. Suppose the app is called app.exe
- copy the new program to appnew.exe
- call MoveFileEx to move appnew.exe to app.exe, using the flags MOVEFILE_DELAY_UNTIL_REBOOT | MOVEFILE_REPLACE_EXISTING.
This will overwrite app.exe with appnew.exe when the system is rebooted.

Marcel
 
Thanks for that I have managed to successfully close the program using the window handle.

Is it possible to get information such as the program name etc. from either the window handle or the process id?

Thanks,

Ian
 
Copied from MSDN Library:

The GetWindowModuleFileName function retrieves the full path and file name of the module associated with the specified window handle.

Syntax

UINT GetWindowModuleFileName( HWND hwnd,
LPTSTR lpszFileName,
UINT cchFileNameMax
);
Parameters

hwnd
[in] Handle to the window whose module file name will be retrieved.
lpszFileName
[out] Pointer to a buffer that receives the path and file name.
cchFileNameMax
[in] Specifies the maximum number of TCHARs that can be copied into the lpszFileName buffer.
Return Value

The return value is the total number of TCHARs copied into the buffer.

Marcel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top