Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

GetProcessImageFileName (psapi)
 
.
Summary
Retrieves the name of the executable file for the specified process.

C# Signature:

[DllImport("psapi.dll")]
static extern uint GetProcessImageFileName(
    IntPtr hProcess,
    [Out] StringBuilder lpImageFileName,
    [In] [MarshalAs(UnmanagedType.U4)] int nSize
);

VB Signature:

Declare Function GetProcessImageFileName Lib "psapi.dll"
<DllImport("psapi.dll")> _
Public Shared Function GetProcessImageFileName(
    ByVal hProcess As IntPtr,
    <Out> lpImageFileName As StringBuilder,
    <[In]> <MarshalAs(UnmanagedType.U4)> uSize As Integer
) As UInteger
End Function

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

From MSDN: The GetProcessImageFileName function returns the path in device form, rather than drive letters. For example, the file name C:\Winnt\System32\Ctype.nls would look as follows in device form:

\Device\Harddisk0\Partition1\WINNT\System32\Ctype.nls

This can be used in place of the GetModuleFileName call, which doesn't work on processes that haven't been loaded by the current one.

Tips & Tricks:

I found this to be more comprehensive than GetModuleFileNameEx. Didnt need to enumerate windows or modules

Sample Code:

Context
Purpose of program is finding the executable under the mouse cursor..a Point

uint pid = 0;
StringBuilder fileName = new StringBuilder(2000);

/** get the handle for point under cursor **/
IntPtr hWnd = WindowFromPoint(pt);

/** Need to get the process ID from handle under cursor **/
GetWindowThreadProcessId(hWnd, out pid);

/** Hook into process **/
IntPtr pic = OpenProcess(NativeMethods.ProcessAccessFlags.All, true, (int)pid);

/** This gets the filename of the process image. Path is in device format **/
GetProcessImageFileName(pic, fileName, 2000);

MessageBox.Show(fileName.ToString());

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions