GetWindowModuleFileName这个函数只能获得本进程的窗口路径,不能获得其他进程窗口的路径。MSDN是这样说的: If all this new HWND-related information wasn’t enough, there’s even more miscellaneous new APIs that retrieve information for a given HWND. GetWindowModuleFileName returns the name of a EXE or DLL that created a window. Be forewarned though; this API only works on HWNDs created by the process that called the API. This means it’s only of use for windows that your program created or within a systemwide hook procedure.
TCHAR strPath[MAX_PATH];
GetWindowThreadProcessId(hwnd, &dwProcessID);
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessID);
if (hProcess != NULL)
{
HMODULE hModule;
DWORD dw;
EnumProcessModules(hProcess, &hModule, sizeof(HMODULE), &dw);
GetModuleFileNameEx(hProcess, hModule, strPath, MAX_PATH);
CloseHandle(hProcess);
}
不过好像有些系统级的还是得不到路径
2. 枚举所有进程,比较id
3. 得到进程后,取得进程模块的exe名
CString CContactImport::GetFoxPathName()
{
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
return "";
}
PROCESSENTRY32 pe32 = {0};
pe32.dwSize = sizeof(PROCESSENTRY32);
// 遍历拍下来的所有进程
if (Process32First(hProcessSnap, &pe32))
{
do
{
if (pe32.th32ProcessID && !strcmp(pe32.szExeFile, "Foxmail.exe"))
{
// 保存进程的名字、PID
HANDLE handle=OpenProcess(PROCESS_ALL_ACCESS,NULL,pe32.th32ProcessID);
char szPath[256];
GetModuleFileNameEx(handle,NULL,szPath,256);
return szPath;
}
}
while (Process32Next(hProcessSnap, &pe32));
}
CloseHandle(hProcessSnap);
return "";
}
{
DWORD dwProcessId = NULL ;
GetWindowThreadProcessId(hwnd,&dwProcessId) ;
if(dwProcessId == 0)
{
return FALSE ;
}
HANDLE hProcess = OpenProcess(..,dwProcessId) ;
if(hProcess == NULL)
{
return FALSE ;
}
GetModuleFileNameEx(hProcess,pszFileName,dwSize) ;
return TRUE ;
}
If all this new HWND-related information wasn’t enough, there’s even more miscellaneous new APIs that retrieve information for a given HWND. GetWindowModuleFileName returns the name of a EXE or DLL that created a window. Be forewarned though; this API only works on HWNDs created by the process that called the API. This means it’s only of use for windows that your program created or within a systemwide hook procedure.