【问题标题】:C++ Get all running processes and their titlesC++ 获取所有正在运行的进程及其标题
【发布时间】:2015-09-10 02:00:39
【问题描述】:

我目前正在使用 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;
}

【问题讨论】:

  • 并非所有进程都有窗口或标题。你想用这些做什么?
  • 我正在开发一个类似的任务管理器应用程序。我是一个简单的标题。
  • 你没有回答我的问题。
  • 问题包括:WindowTitle 从未分配。 GetWindowText 不返回 string。回调函数接收到HWND,因此WindowHandle = GetForegroundWindow() 适得其反。另外:.I's a simple header. 是什么意思?

标签: c++ api winapi


【解决方案1】:

您的代码滥用了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;
}

【讨论】:

  • @IInspectable:首先,您不知道 OP 在包含字符串后将如何使用它们。原始代码使用的是std::string,所以我坚持使用它并删除了TCHAR 依赖项。如果 OP 想要切换到 Unicode,只需将 std::string 替换为 std::wstring 并使用 GetWindowText(Length)W()。其次,在这个例子中,Windows 控制台不能很好地支持输出 Unicode 字符串(有很多问题,所以我不会在这里重复这些问题)。
  • 控制台输出是工件,仅用于调试。
猜你喜欢
  • 1970-01-01
  • 2014-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-26
  • 2012-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多