Get location of file from ProcessID 
Author Message
 Get location of file from ProcessID

I have the ID of a process and I want to find out the location of the
executable on the harddisk for that process.

I'm aware of the method:

DWORD GetModuleFileName(
  HMODULE hModule,    // handle to module to find filename for
  LPTSTR lpFilename,  // pointer to buffer to receive module path
  DWORD nSize         // size of buffer, in characters
);

but that relies on having a HMODULE and

HANDLE OpenProcess(
  DWORD dwDesiredAccess,  // access flag
  BOOL bInheritHandle,    // handle inheritance flag
  DWORD dwProcessId       // process identifier
);

only supplies a HANDLE.  How can I do this?

Cheers
Stephen



Wed, 16 Feb 2005 10:10:51 GMT  
 Get location of file from ProcessID

Quote:
> I have the ID of a process and I want to find out the location of the
> executable on the harddisk for that process.

> I'm aware of the method:

> DWORD GetModuleFileName(
>   HMODULE hModule,    // handle to module to find filename for
>   LPTSTR lpFilename,  // pointer to buffer to receive module path
>   DWORD nSize         // size of buffer, in characters
> );

> but that relies on having a HMODULE and

> HANDLE OpenProcess(
>   DWORD dwDesiredAccess,  // access flag
>   BOOL bInheritHandle,    // handle inheritance flag
>   DWORD dwProcessId       // process identifier
> );

> only supplies a HANDLE.  How can I do this?

Use OpenProcess() to get a handle to a process. Use EnumProcessModules() or
CreateToolhelp32Snapshot()/Module32First()/Module32Next() to get a list of
handles to modules. Pass the module handles to GetModuleFileNameEx().

Regards,
Will



Wed, 16 Feb 2005 13:10:54 GMT  
 Get location of file from ProcessID
Thanks alot.  In order to use EnumProcessModules and GetModuleFileNameEx, I
have to install PSAPI from the Platform SDK right?

Steve



Wed, 16 Feb 2005 14:52:09 GMT  
 Get location of file from ProcessID

Quote:
> Thanks alot.

You are welcome.

Quote:
> In order to use EnumProcessModules and GetModuleFileNameEx, I
> have to install PSAPI from the Platform SDK right?

The MSDN-CD and the web site at http://msdn.microsoft.com  is chock full of
useful information. You'll find this there about that: :-)

"These functions are available in Psapi.dll, which is included in Microsoft?
Windows? 2000 and Microsoft? Windows? XP. To use these functions on
Microsoft? Windows NT? 4.0, you must obtain the redistributable version of
this DLL."

Regards,
Will



Thu, 17 Feb 2005 00:28:23 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. getting a file from the app directory location

2. Getting ProcessID from application in MSVC debugger?

3. Getting ProcessId

4. getting the program location ??????

5. HELP: Getting text into a dialog window at an arbitrary location

6. Getting location of controls

7. Draggin files to the explorer and retrieving the dropped on location (file path)

8. Assembly.Location returning shadowed location

9. Location of pdb files

10. Assembly files and there location

11. Config file location

12. help file locations

 

 
Powered by phpBB® Forum Software