Get the path to the process executable file: Getmodulefilenameex, Getprocessimagefilename, Queryfullprocessimagename

Source: Internet
Author: User



http://blog.csdn.net/bichenggui/article/details/4774457



--------------------------------------------------


get the path to the process executable file: Getmodulefilenameex, Getprocessimagefilename, Queryfullprocessimagename2009-11-05 23:20 2782 People read comments (0) favorite reports








there are usually three ways to get the path of the process executable file:



One: Call the Getmodulefilenameex function to get the module path of the executable file This function is available from Windows NT 4.0 to the current Vista system, and backwards compatibility is better.






Second: Call the getprocessimagefilename function, which can be used in both Windows XP and later systems.






Three: Call Windows Vista new function queryfullprocessimagename, because it is vista new, so compatibility is not good.






Here's a look at the prototypes of these three functions:




DWORD Getmodulefilenameex(HANDLE hprocess,hmodule hmodule,lptstr lpfilename,dword nSize)
Hprocess is the handle to the target process, hmodule is the handle to the target module (when this parameter is NULL, the function returns the path of the process executable), lpFileName is the string buffer that holds the path, and the nsize represents the size of the buffer. A function call failure returns 0. Note: The handle to the process must have Process_query_information and Process_vm_read permissions.






DWORD getprocessimagefilename(HANDLE hprocess,lptstr lpimagefilename,dword nSize)
Hprocess is the handle to the target process, the lpimagefilename is the string buffer that holds the path, and the nsize represents the size of the buffer. Function failure will return 0. Note: The process handle requires Process_query_information permissions.






BOOL queryfullprocessimagename(HANDLE hprocess,dword dwflags,lptstr lpexename,pdword lpdwsize)
Hprocess is the handle to the target process, dwflags is generally set to 0 (indicating that the path returned is a path format of Win32, such as "c:/...", as set to Process_name_native will return "/device/harddiskvolume1 Format path), Lpexename is the string buffer that holds the path, and lpdwsize represents the size of the buffer. function failure will return false. Note: The handle to call this function must have process_query_information or this is process_query_limited_information permission, and can only be used in a Vista or later version of the system.






Calls to Getmodulefilenameex and getprocessimagefilename need to contain the Psapi.h header file and are linked to the Psapi.lib



#include <Psapi.h>
#pragma comment (lib, "Psapi.lib")





Getprocessimagefilename Get Process Path



HANDLE hprocess=openprocess (PROCESS_QUERY_INFORMATION,FALSE,PE.TH32PROCESSID);
if (Getprocessimagefilename (Hprocess,szfilepath,max_path)!=0) {
MyString strFilePath = Ccommon::D osdevicepath2logicalpath (Szfilepath);

}



Dosdevicepath2logicalpath code excerpt from: Ms-help://ms. Msdnqtr.v80.chs/ms. Msdn.v80/ms. Win32com.v10.en/fileio/fs/obtaining_a_file_name_from_a_file_handle.htm

MyString Ccommon::D osdevicepath2logicalpath (LPCTSTR lpszdospath)
{
MyString strresult;

Translate path with device name to drive letters.
TCHAR Sztemp[max_path];
Sztemp[0] = ' + ';

if (Lpszdospath==null | |!) GetLogicalDriveStrings (_countof (sztemp)-1, sztemp)) {
return strresult;
}

TCHAR Szname[max_path];
TCHAR szdrive[3] = TEXT (":");
BOOL bfound = FALSE;
tchar* p = sztemp;

do{
Copy the drive letter to the template string
*szdrive = *p;

Look up each device name
if (Querydosdevice (szdrive, SzName, _countof (szName))) {
UINT Unamelen = (UINT) _tcslen (szName);

if (Unamelen < MAX_PATH)
{
Bfound = _tcsnicmp (Lpszdospath, szName, unamelen) = = 0;

if (bfound) {
Reconstruct pszFileName using sztemp
Replace device path with DOS path
TCHAR Sztempfile[max_path];
_stprintf_s (Sztempfile, TEXT ("%s%s"), szdrive, Lpszdospath+unamelen);
strresult = Sztempfile;
}
}
}

Go to the next NULL character.
while (*p++);
} while (!bfound && *p); End of string

return strresult;
}





Http://www.cnblogs.com/-clq/archive/2012/03/14/2395564.html



Get the path to the process executable file: Getmodulefilenameex, Getprocessimagefilename, Queryfullprocessimagename


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.