How can I get the name of any process having a visible window - WinAPI?
<p>I'm trying to get the name of processes which have a visible window. For example, if I have Chrome opened, I would like to get the string "chrome.exe", but I only get the init value "unknown" using the code below.</p>

<p>I read around it could be an access rights problem, can you suggest me how to change them in order to get the name of processes?</p>

<pre><code>DWORD idProc = 0;       //pointer to the process which created the window
DWORD idThread = GetWindowThreadProcessId(Wnd-&gt;get_handle(), &amp;idProc);
Wnd-&gt;set_pid(idThread); //Wnd is an object of a class i created, to collect processes info
// Get a handle to the process.
TCHAR szProcessName[DEFAULT_BUFLEN] = TEXT("&lt;unknown&gt;");
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
    PROCESS_VM_READ,
    FALSE, idProc);

if (hProcess!=NULL) {
    HMODULE hMod;
    DWORD cbNeeded;
    if (EnumProcessModules(hProcess, &amp;hMod, sizeof(hMod),
        &amp;cbNeeded))
    {
        GetModuleBaseName(hProcess, hMod, szProcessName,        
            sizeof(szProcessName) / sizeof(TCHAR));
    }
}
Wnd-&gt;set_processname(szProcessName);                            
CloseHandle(hProcess);
</code></pre>

<p>It works fine for some processes, but it doesn't for many others like Chrome, as I said.</p>

<p>EDIT: I forgot to say, I've just filtered visible windows, so suppose handles are what I need yet.</p>