从Visual C ++获取ProcessName

时间:2013-02-06 10:47:20

标签: c++ visual-c++

在Visual C ++中,我使用

获取当前窗口的标题
::GetWindowText(wnd, str, 290);

strcpy(curr_wnd_txt, str); 

但是如何获取应用程序的进程名称,例如“Iexplore”用于使用Internet Explorer,“Skype”用于使用“Skype - 用户名”,“资源管理器”用于使用Windows资源管理器的窗口?

这是我试过的:

void CMainWndDlg::Monitor_ActiveWindowCaptions()
{
    HWND hwnd;
    DWORD process_id=0;
    GetWindowThreadProcessId(hwnd, &process_id);
    CString strEXEName = GetEXEName (process_id);
}

CString GetEXEName(DWORD dwProcessID)
{
DWORD aProcesses [1024], cbNeeded, cProcesses;
unsigned int i;

if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
    return "";

cProcesses = cbNeeded / sizeof(DWORD);

TCHAR szEXEName[MAX_PATH];
for (i = 0; i < cProcesses; i++)
{
    if (aProcesses [i] == dwProcessID)
    {
        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
                          PROCESS_VM_READ, FALSE, dwProcessID);
        if (NULL != hProcess)
        {
            HMODULE hMod;
            DWORD cbNeeded;
            if(EnumProcessModules(hProcess, &hMod, 
                                  sizeof(hMod), &cbNeeded))
            {
                GetModuleBaseName(hProcess, hMod, szEXEName, 
                    sizeof(szEXEName)/sizeof(TCHAR));
                return CString (szEXEName);
            }
        }
    }    
}
return "";
}

2 个答案:

答案 0 :(得分:1)

GetModuleFileName()可用于检索当前进程的可执行文件的路径。

http://msdn.microsoft.com/en-gb/library/windows/desktop/ms683197(v=vs.85).aspx

答案 1 :(得分:0)

您必须枚举窗口,获取进程句柄,然后从下面的函数中获取可执行文件的名称。

#include "psapi.h"
#pragma comment(lib, "psapi.lib")

CString GetEXEName(DWORD dwProcessID)
{
    DWORD aProcesses [1024], cbNeeded, cProcesses;
    unsigned int i;

    //Enumerate all processes
    if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
        return NULL;

    // Calculate how many process identifiers were returned.
    cProcesses = cbNeeded / sizeof(DWORD);

    TCHAR szEXEName[MAX_PATH];
    //Loop through all process to find the one that matches
    //the one we are looking for
    for (i = 0; i < cProcesses; i++)
    {
        if (aProcesses [i] == dwProcessID)
        {
            // Get a handle to the process
            HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
                              PROCESS_VM_READ, FALSE, dwProcessID);

            // Get the process name
            if (NULL != hProcess)
            {
                HMODULE hMod;
                DWORD cbNeeded;

                if(EnumProcessModules(hProcess, &hMod, 
                                      sizeof(hMod), &cbNeeded))
                {
                    //Get the name of the exe file
                    GetModuleBaseName(hProcess, hMod, szEXEName, 
                        sizeof(szEXEName)/sizeof(TCHAR));

                    return CString (szEXEName);
                }
            }
        }    
    }

    return NULL;
}

请参阅此处:http://www.codeproject.com/Articles/14843/Finding-module-name-from-the-window-handle