简体   繁体   English

如何从其进程名称获取窗口标题?

[英]How Could I Get the Window Title from Its Process Name?

This is my code to check if the process name existed or not: 这是我的代码,用于检查进程名称是否存在:

bool isRunning (LPCSTR processname)
{
    HANDLE Snapshot;
    Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

    if(Snapshot != INVALID_HANDLE_VALUE)
    {
        PROCESSENTRY32 ProcessEntry;
        BOOL           Succeed;
        ProcessEntry.dwSize = sizeof(PROCESSENTRY32);
        Succeed = Process32First(Snapshot, &ProcessEntry);

        while(Succeed)
        {
          if(lstrcmp(ProcessEntry.szExeFile,processname) == 0)
          {
           return true;
          }
      Succeed = Process32Next(Snapshot, &ProcessEntry);
    }

    CloseHandle(Snapshot);
    }
}

How could I use it or edit it to get the window title from its process name (for example "notepad.exe")? 我如何使用它或对其进行编辑以从其进程名称(例如“ notepad.exe”)中获取窗口标题? If it existed the program would return text like "New text document - Notepad" 如果存在,程序将返回“ New text document-Notepad”之类的文本

This is actually addressed in Microsoft's description of CreateToolhelp32Snapshot . 这实际上在Microsoft的CreateToolhelp32Snapshot描述中得到了CreateToolhelp32Snapshot

It says "You can use the QueryFullProcessImageName function to retrieve the full name of an executable image for both 32- and 64-bit processes from a 32-bit process." 它说:“您可以使用QueryFullProcessImageName函数从32位进程中检索32位和64位进程的可执行映像的全名。”

You'll basically need to iterate through checking your HANDLE s with QueryFullProcessImageName . 您基本上需要通过使用QueryFullProcessImageName检查HANDLE来进行迭代。

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

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