请问,如何根据窗口句柄,或者窗口标题得到进程句柄呢????

解决方案 »

  1.   

    GetWindowThreadProcessId Function--------------------------------------------------------------------------------The GetWindowThreadProcessId function retrieves the identifier of the thread that created the specified window and, optionally, the identifier of the process that created the window. SyntaxDWORD GetWindowThreadProcessId(
        HWND hWnd,
        LPDWORD lpdwProcessId
    );ParametershWnd
    [in] Handle to the window. lpdwProcessId
    [out] Pointer to a variable that receives the process identifier. If this parameter is not NULL, GetWindowThreadProcessId copies the identifier of the process to the variable; otherwise, it does not. Return ValueThe return value is the identifier of the thread that created the window. 
      

  2.   

    GetWindowThreadProcessId
    这是获得线程ID的,楼主要的是进程句柄
      

  3.   

    正是!请看MSDN对第二个参数的说明,这个我用过,返回的正是进程ID.主线程ID就是进程ID.lpdwProcessId 
    [out] Pointer to a variable that receives the process identifier. If this parameter is not NULL, GetWindowThreadProcessId copies the identifier of the process to the variable; otherwise, it does not.
      

  4.   

    哦,要进程句柄,我没看清楚,再OpenProcess得到句柄.传入之前得到进程ID(第三个参数).
    HANDLE WINAPI OpenProcess(
      __in          DWORD dwDesiredAccess,
      __in          BOOL bInheritHandle,
      __in          DWORD dwProcessId
    );
      

  5.   


    得進程就是  FindWindow
      

  6.   

    给你个示例:
    procedure TForm1.Button1Click(Sender: TObject);
    var
      hWindow: HWND;       { 窗体句柄 }
      dwProcessID: DWORD;  { 进程 ID }
      hProcess: THandle;   { 进程句柄 }
    begin
      { 根据标题获取窗体的句柄 }
      hWindow := FindWindow(nil, '标题');  { 通过窗体句柄获取进程 ID }
      GetWindowThreadProcessId(hWindow, dwProcessID);  { 通过进程 ID 获取进程句柄 }
      hProcess := OpenProcess(PROCESS_ALL_ACCESS, False, dwProcessID);  { 结束该进程 }
      TerminateProcess(hProcess, 0);
    end;
      

  7.   

    得到窗口句柄:
    winh := FindWindow(nil, PChar('记事本'));得到进程句柄:
    var
      pid: DWORD; // 进程ID
      tid: DWORD; // 线程IDtid = GetWindowThreadProcessId(winh, pid);
      

  8.   

    FindowWindow先找到窗口
    然后GetWindowThreadId找到句柄
      

  9.   

    来晚了. FindWindow 和 GetWindowThreadProcessId组合一下就行了
      

  10.   

    LZ 给你一段根据进程名获取进程ID的代码
    unit GetProcessPid;interfaceuses
      TLhelp32,SysUtils;Function GetPID(_GetPID:String):String;implementation
    //------------------------------------------------------------------------------Function GetPID(_GetPID:String):String;
    var
         h:thandle;
         f:boolean;
         lppe:tprocessentry32;
    begin
         h := CreateToolhelp32Snapshot(TH32cs_SnapProcess, 0);
         lppe.dwSize := sizeof(lppe);
         f := Process32First(h, lppe);     //lppe.szExeFile是进程的名字,自己挑选你要的
         //lppe.th32ProcessID就是你要的进程号
         while integer(f) <> 0 do
         begin
           //if lppe.szExeFile='QQ.exe' then showmessage('ok');
           if lppe.szExeFile = _GetPID then
           begin
             Result:=(inttostr(lppe.th32ProcessID));
             break;
           end;
           f := Process32Next(h, lppe);
         end;
    end;
    end.
      

  11.   

    然后转换一下类型,再OPENPROCESS得到它的句柄.