全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:9536
推到 Plurk!
推到 Facebook!

Program Name、PID、Window Handle、hProcess 的轉換函式

 
chris_shieh
高階會員


發表:46
回覆:308
積分:240
註冊:2004-04-26

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-12-16 17:20:23 IP:218.167.xxx.xxx 未訂閱
Program Name、Process ID、Window Handle、Process Handle 的轉換函式    在關閉或取得其他程式執行狀況時 常會需要用到 ProcessID, Process Handle, Window Handle等 例如  TerminateProcess=> Process Handle PostMessage,SendMessage=> Window Handle    以下是一些它們之間的轉換函式    宣告:
// Get ProcessID By ProgramName (Include Path or Not Include)
function GetPIDByProgramName(const APName: string): THandle;
// Get Window Handle By ProgramName (Include Path or Not Include)
function GetHWndByProgramName(const APName: string): THandle;
// Get Window Handle By ProcessID
function GetHWndByPID(const hPID: THandle): THandle;
// Get ProcessID By Window Handle
function GetPIDByHWnd(const hWnd: THandle): THandle;
// Get Process Handle By Window Handle
function GetProcessHndByHWnd(const hWnd: THandle): THandle;
// Get Process Handle By Process ID
function GetProcessHndByPID(const hAPID: THandle): THandle; 
實作:
// Get Window Handle By ProgramName (Include Path or Not Include)
function GetHWndByProgramName(const APName: string): THandle;
begin
  Result:=GetHWndByPID(GetPIDByProgramName(APName));
end;    // Get Process Handle By Window Handle
function GetProcessHndByHWnd(const hWnd: THandle): THandle;
var
  PID: DWORD;
  AhProcess: THandle;
begin
  if hWnd<>0 then
  begin
    GetWindowThreadProcessID(hWnd, @PID);
    AhProcess := OpenProcess(PROCESS_ALL_ACCESS, false, PID);
    Result:=AhProcess;
    CloseHandle(AhProcess);
  end
  else
    Result:=0;
end;    // Get Process Handle By Process ID
function GetProcessHndByPID(const hAPID: THandle): THandle;
var
  AhProcess: THandle;
begin
  if hAPID<>0 then
  begin
    AhProcess := OpenProcess(PROCESS_ALL_ACCESS, false, hAPID);
    Result:=AhProcess;
    CloseHandle(AhProcess);
  end
  else
    Result:=0;
end;    // Get Window Handle By ProcessID
function GetPIDByHWnd(const hWnd: THandle): THandle;
var
  PID: DWORD;
begin
  if hWnd<>0 then
  begin
    GetWindowThreadProcessID(hWnd, @PID);
    Result:=PID;
  end
  else
    Result:=0;
end;    // Get Window Handle By ProcessID
function GetHWndByPID(const hPID: THandle): THandle;
type
  PEnumInfo = ^TEnumInfo;
  TEnumInfo = record
    ProcessID: DWORD;
    HWND: THandle;
  end;      function EnumWindowsProc(Wnd: DWORD; var EI: TEnumInfo): Bool; stdcall;
  var
    PID: DWORD;
  begin
    GetWindowThreadProcessID(Wnd, @PID);
    Result := (PID <> EI.ProcessID) or
              (not IsWindowVisible(WND)) or
              (not IsWindowEnabled(WND));        if not Result then EI.HWND := WND; //break on return FALSE 所以要反向檢查
  end;      function FindMainWindow(PID: DWORD): DWORD;
  var
    EI: TEnumInfo;
  begin
    EI.ProcessID := PID;
    EI.HWND := 0;
    EnumWindows(@EnumWindowsProc, Integer(@EI));
    Result := EI.HWND;
  end;
begin
  if hPID<>0 then
    Result:=FindMainWindow(hPID)
  else
    Result:=0;
end;    // Get ProcessID By ProgramName (Include Path or Not Include)
function GetPIDByProgramName(const APName: string): THandle;
var
  isFound: boolean;
  AHandle, AhProcess: THandle;
  ProcessEntry32: TProcessEntry32;
  APath: array[0..MAX_PATH] of char;
begin
  try
    Result := 0;
    AHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    ProcessEntry32.dwSize := Sizeof(ProcessEntry32);
    isFound := Process32First(AHandle, ProcessEntry32);        while isFound do
    begin
      AhProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
        false, ProcessEntry32.th32ProcessID);
      GetModuleFileNameEx(AhProcess, 0, @APath[0], sizeof(APath));          if (UpperCase(StrPas(APath)) = UpperCase(APName)) or
        (UpperCase(StrPas(ProcessEntry32.szExeFile)) = UpperCase(APName)) then
      begin
        Result := ProcessEntry32.th32ProcessID;
        break;
      end;
      isFound := Process32Next(AHandle, ProcessEntry32);
      CloseHandle(AhProcess);
    end;
  finally
    CloseHandle(AHandle);
  end;
end;
系統時間:2024-11-25 3:57:48
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!