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

C++ Get all running processes and their titles

本文关键字:进程 标题 运行 获取      更新时间:2023-10-16

我目前正在做一个c++项目,我想做的是把所有正在运行的可用进程和它们的标题,但目前我只能跟踪它们的处理程序并计算它们,而不能取它们的标题。

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam);
int i;
string hwndTitle;
LPTSTR WindowTitle;
int length = 0;
int getHWND()
{
    std::cout << "Finding all open windowsn";
    if(EnumWindows(EnumWindowsProc, 0)) {
        std::cout << i << " windows are openn"<<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;
}

您的代码正在滥用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 openn" << "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 openn" << "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, '');
    length = GetWindowTextA(hWnd, &WindowTitle[0], length); 
    WindowTitle.resize(length);
    WindowTitles->push_back(WindowTitle);
    return TRUE;
}