简体   繁体   English

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

[英]C++ Get all running processes and their titles

I am currently working on a project in C++ and what i want to do is to take all of the running available proceses and with them their titles , but currently I'm able only to track their handlers and count them and no to take their title. 我目前正在开发一个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;
}

Your code is misusing GetWindowText() . 您的代码滥用了GetWindowText() You are not allocating any memory for it to fill in, and it does not return a std::string or even a char* / TCHAR* , like your code is assuming. 您没有为其填充任何内存,并且它不会返回std::string甚至是char* / TCHAR* ,就像您的代码所假设的那样。 And even if you were using it correctly, you are outputting only the last window title that is assigned to hwndTitle instead of concatenating multiple titles together. 即使您正确使用它,您也只输出分配给hwndTitle的最后一个窗口标题,而不是将多个标题连接在一起。

Try something more like this instead: 尝试更像这样的东西:

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;
}

Alternatively: 或者:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM