可以知道系统当前其它进程的窗口句柄,怎样获得其执行文件的路径?

解决方案 »

  1.   

    DWORD dwProcessID;
    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.   

    GetModuleFileName/GetModuleFileNameEx
    不过好像有些系统级的还是得不到路径
      

  3.   

    枚举进程,取得进程句柄,然后用GetModuleFIleName呵呵。
      

  4.   

    1. GetWindowThreadProcessId 得到窗口所在的进程id
    2. 枚举所有进程,比较id
    3. 得到进程后,取得进程模块的exe名
      

  5.   

    my example:
    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 "";
    }
      

  6.   

    GetWindowThreadProcessIdBOOL GetFileNameFromHwnd(HWND hWnd,LPTSTR pszFileName,DWORD dwSize)
    {
      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 ;
    }
      

  7.   

    GetWindowModuleFileName就可以吧。刚刚看到这个函数。
      

  8.   

    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.