VC++ 根据窗口句柄获取进程名和进程ID

CString GetProcessExeName(HWND hWnd)
{
    CString strProcess;
    unsigned long dwPid = 0;
    ::GetWindowThreadProcessId(hWnd, &dwPid);
    HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwPid);
    if (NULL != hProcess)
    {
        TCHAR szProcess[MAX_PATH]={0};
        ::GetProcessImageFileName(hProcess, szProcess, MAX_PATH);
        strProcess = szProcess;
        int nPos = strProcess.ReverseFind(L'\\');
        if (-1 != nPos)
            strProcess = strProcess.Mid(nPos+1);
    }

    return strProcess;
}

你可能感兴趣的:(源代码,窗口句柄,进程名,vc++,windows,id)