已知一个窗口的句柄,如何取得该窗口的线程或进程的句柄

解决方案 »

  1.   

    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. DWORD GetWindowThreadProcessId(
      HWND hWnd,             // handle to window
      LPDWORD lpdwProcessId  // address of variable for process identifier
    );
      

  2.   

    //参考如下的代码:
    DWORD dwProcessId;
    HANDLE hProcess;
    HWND hwnd = FindWindow(NULL, “计算器”);
    GetWindowThreadProcessId(hwnd, &dwProcessId);
    hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessId);
    TerminateProcess(hProcess, 0);
    CloseHandle(hProcess);