我用GetActiveWindow或GetForegroundWindow获得一个窗口的句柄,我如何获取这个窗口是属于什么exe/dll文件的?请给出代码
谢谢

解决方案 »

  1.   

    uses
      PsAPI;
    // GetProcessPath 根据进程ID得到进程全路径名称
    function GetProcessPath(PID : Integer): String;
    var hProcess: THandle;
        hMod    : hModule;
        cbNeeded: DWORD;
        szProcessName: array[0..1024] of Char;
    begin
      hProcess := OpenProcess(PROCESS_QUERY_INFORMATION OR PROCESS_VM_READ,
                              FALSE, PID );  szProcessName := 'unknown';
      if (hProcess<>0) then begin
        if(EnumProcessModules(hProcess,@hMod,sizeof(hMod),cbNeeded)) then begin
           GetModuleFileNameEx(hProcess,hMod,szProcessName,sizeof(szProcessName));
           Result := StrPas(szProcessName);
        end;
      end;
      CloseHandle(hProcess);
    end;
      

  2.   

    忘记了,还要先获得当前窗口的进程ID:
    var
      wnd:HWND;
      ThreadId: DWORD;
      FileName:string;
    begin  
        GetWindowThreadProcessId(wnd, ThreadId);
        FileName:=GetProcessPath(ThreadId);
    end;
      

  3.   

    呵呵,正确!GetWindowThreadProcessId