[Delphi] 如何取得視窗標題、執行檔名

  • 1912
  • 0

摘要:[Delphi] 如何取得視窗標題、執行檔名

取得執行檔名稱(包含路徑)

 


  function GetWindowModuleFileNameU(const WindowHandle: HWND):WideString;
  type
    _QueryFullProcessImageNameW =
      function(
        hProcess: THandle;
        dwFlags: DWORD;
        lpExeName: PWCHAR;
        lpdwSize: PDWORD): BOOL; stdcall;
  const
    PROCESS_QUERY_LIMITED_INFORMATION = $1000;
    MAX_WIDE_LENGTH = 256;
    MAX_WIDE_BUFFER_SIZE=255;
  var
    WText: array [0..MAX_WIDE_LENGTH] of WCHAR;
    WTextSize: DWORD;
    ProcessID: DWORD;
    Process: THandle;
    Module: HMODULE;
    ModulesArraySizeNeeded: DWORD;
    ProcInfo: TProcessEntry32;
    g_QueryFullProcessImageNameW:_QueryFullProcessImageNameW;

    function IsVista: BOOL;
    var
      osi: OSVERSIONINFO;
    begin
      osi.dwOSVersionInfoSize:=sizeof(OSVERSIONINFO);
            GetVersionEx(osi);
            if(osi.dwMajorVersion >= 6) then
                    Result:=TRUE
            else
                    Result:=FALSE;
    end;

  begin
    GetWindowThreadProcessId(WindowHandle, @ProcessID);//先取得Process ID

    if IsVista then
    begin
      // Windows Vista以上,為了能夠正確取得高權限的執行檔名稱,使用新的PROCESS_QUERY_LIMITED_INFORMATION參數
      // 與新的QueryFullProcessImageNameW API
      Process := OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, ProcessID);//這個函式95以上都可以用沒問題
      if Process = 0 then
      begin
        Result := '';
        Exit;
      end;
      try
        WTextSize := MAX_WIDE_BUFFER_SIZE;

        @g_QueryFullProcessImageNameW := GetProcAddress(GetModuleHandle('Kernel32.dll'), 'QueryFullProcessImageNameW');
        if g_QueryFullProcessImageNameW(Process, 0, @WText[0], @WTextSize) then
        begin
          Result := WText;
          Exit;
        end;
      finally
        CloseHandle(Process);
      end;
    end
    else if Win32Platform = VER_PLATFORM_WIN32_NT then
    begin
      //使用PSAPI,只對Windows NT 4.0以上的電腦有效
      Process := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, FALSE, ProcessID);//這個函式95以上都可以用沒問題
      if Process = 0 then
      begin
        Result := '';
        Exit;
      end;
      try
      begin
        if false <> EnumProcessModules(Process, @Module, SizeOf(HMODULE), ModulesArraySizeNeeded) then
        begin
          GetModuleFileNameExW(Process, Module, @WText[0], MAX_WIDE_BUFFER_SIZE);
          Result := WText;
          Exit;
        end;
      end
      finally
        CloseHandle(Process);
      end;
    end
    else
    begin
      Process := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
      if Process <> INVALID_HANDLE_VALUE then
      begin
        try
          ProcInfo.dwSize := SizeOf(ProcInfo);
          if false = Process32First(Process, ProcInfo) then
          begin
            Result := '';
            Exit;
          end;

          repeat
            if ProcInfo.th32ProcessID = ProcessID then
            begin
              MultiByteToWideChar(CP_ACP, 0, @(ProcInfo.szExeFile[0]), -1, @WText[0], MAX_WIDE_LENGTH);
              WText[MAX_WIDE_LENGTH] := #0;
              Result := WText;
              Exit;
            end;
          until (false = Process32Next(Process, ProcInfo));

        finally
          CloseHandle(Process);
        end;
      end;
    end;
    Result := '';
  end;

 

 

取得視窗標題


function GetWindowCaption(const WindowHandle: HWND):WideString;
  var
    aCaption:array[0..1023] of WideChar;
  begin
  if GetWindowTextW(WindowHandle,@aCaption[0], Length(aCaption)) > 0 then
    Result := PWideChar(@aCaption[0])
  else
    Result := '';
  end;