C ++获取所有正在运行的进程及其标题

时间:2015-09-08 18:13:27

标签: c++ api winapi

我目前正在使用C ++开发一个项目,我想要做的是采用所有正在运行的可用程序并使用它们的标题,但目前我只能跟踪它们的处理程序并对它们进行计数。不接受他们的头衔。

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam);
int i;
string hwndTitle;
LPTSTR WindowTitle;
int length = 0;
int getHWND()
{
    std::cout << "Finding all open windows\n";
    if(EnumWindows(EnumWindowsProc, 0)) {
        std::cout << i << " windows are open\n"<<hwndTitle<<"\n"<<"Call was successful...\n" << std::endl;
        std::cin.get();
    } else {
        std::cout << "Call was unsuccessful...\n" << std::endl;
        std::cin.get();
    }

    return 0;
} 

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) {
    i++;
    HWND WindowHandle;
    WindowHandle = GetForegroundWindow();
    length = GetWindowTextLength (hWnd);
    hwndTitle = GetWindowText(hWnd , WindowTitle , length); 
    return true;
}

1 个答案:

答案 0 :(得分:3)

您的代码滥用GetWindowText()。您没有为其填充任何内存,并且它不会返回std::string甚至char* / TCHAR*,就像您的代码所假设的那样。即使您正确使用它,您也只输出分配给hwndTitle的最后一个窗口标题,而不是将多个标题连接在一起。

尝试更像这样的东西:

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam);

int getHWND()
{
    std::cout << "Finding all open windows" << std::endl;
    int i = 0;
    if (EnumWindows(EnumWindowsProc, (LPARAM) &i)) {
        std::cout << i << " windows are open\n" << "Call was successful...\n" << std::endl;
    } else {
        std::cout << "Call was unsuccessful...\n" << std::endl;
    }
    std::cin.get();
    return 0;
} 

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
    int *i = (int*) lParam;
    ++(*i);
    int length = GetWindowTextLengthA(hWnd);
    std::vector<char> WindowTitle(length+1);
    length = GetWindowTextA(hWnd, &WindowTitle[0], length); 
    if (length > 0) {
        WindowTitle[length] = 0;
        std::cout << &WindowTitle[0] << std::endl;
    } else {
        std::cout << "(none)" << std::endl;
    }
    return TRUE;
}

可替换地:

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam);

int getHWND()
{
    std::cout << "Finding all open windows" << std::endl;
    std::vector<std::string> WindowTitles;
    if (EnumWindows(EnumWindowsProc, (LPARAM) &WindowTitles)) {
        for (std::vector<std::string>::iterator i = WindowTitles.begin(); i != WindowTitles.end(); ++i) {
            if (!i->empty()) {
                std::cout << *i << std::endl;
            } else {
                std::cout << "(none)" << std::endl;
            }
        }
        std::cout << WindowTitles.size() << " windows are open\n" << "Call was successful...\n" << std::endl;
    } else {
        std::cout << "Call was unsuccessful...\n" << std::endl;
    }
    std::cin.get();
    return 0;
} 

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
    std::vector<std::string> *WindowTitles = (std::vector<std::string>*) lParam;
    int length = GetWindowTextLengthA(hWnd);
    std::string WindowTitle(length+1, '\0');
    length = GetWindowTextA(hWnd, &WindowTitle[0], length); 
    WindowTitle.resize(length);
    WindowTitles->push_back(WindowTitle);
    return TRUE;
}
相关问题