简体   繁体   中英

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")? If it existed the program would return text like "New text document - Notepad"

This is actually addressed in Microsoft's description of 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."

You'll basically need to iterate through checking your HANDLE s with QueryFullProcessImageName .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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