10

I'm trying to get the current window or the active window and the process name of that window, in Windows with winapi.

So, I was able to get the active window with GetForegroundWindow() and I'm using OpenProcess() to get the process, the problem it's that OpenProcess needs the process id, so I though I could use GetProcessId() but this one receives the window in a HANDLE type and I have the current window in HWND type.

I've try a couple of things but couldn't made it work. So can anyone tell how can I get the process id with the window in HWND ?? or get the HANDLE of current window ??

I leave my code in here in case some sees a solution that could be helpful for me. I'm working with Qt and C++

char wnd_title[256];
HWND hwnd=GetForegroundWindow(); // get handle of currently active window
GetWindowText(hwnd,wnd_title,sizeof(wnd_title));
HANDLE Handle = OpenProcess(
                  PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
                  FALSE,
                  GetProcessId(hwnd) // GetProcessId is returning 0
                );
if (Handle)
{
  TCHAR Buffer[MAX_PATH];
  if (GetModuleFileNameEx(Handle, 0, Buffer, MAX_PATH))
  {
    printf("Paht: %s", Buffer);
    // At this point, buffer contains the full path to the executable
  }
  CloseHandle(Handle);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
SujaM
  • 409
  • 6
  • 16
  • `GetProcessId()` does not accept a window handle as input, it accepts a process handle instead. It retrieves the ID of a specified process. – Remy Lebeau Sep 15 '15 at 16:20

1 Answers1

10

You can use GetWindowThreadProcessId(), which takes in an HWND and outputs the ID of the window's owning process.

For example:

#include <tchar.h>

TCHAR wnd_title[256];
HWND hwnd = GetForegroundWindow(); // get handle of currently active window
GetWindowTextA(hwnd, wnd_title, 256);

DWORD dwPID;
GetWindowThreadProcessId(hwnd, &dwPID);

HANDLE Handle = OpenProcess(
                  PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
                  FALSE,
                  dwPID
                );
if (Handle)
{
    TCHAR Buffer[MAX_PATH];
    if (GetModuleFileNameEx(Handle, 0, Buffer, MAX_PATH))
    {
        _tprintf(_T("Path: %s"), Buffer);
        // At this point, buffer contains the full path to the executable
    }
    CloseHandle(Handle);
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
zaw
  • 220
  • 3
  • 12
  • Thanks for your anwser, but this solution it's not working for all process, it works fine with Skype, Qt Creator, firefox, chrome, but it doesn't show anything with the desktop, windows prompt, word, my documents ,ect... – SujaM Sep 15 '15 at 20:45
  • Oh, no the problem it's not while getting the id, that part it's working fine, the problem whit all those process I mention before is when I try to get the process name, `GetModuleFileNameEx` it's not working for all process. – SujaM Sep 15 '15 at 20:53
  • 1
    I fix the problem with the process name changing `GetModuleFileNameEx` for `GetProcessImageFileName` Thanks Remy Lebeau. – SujaM Sep 15 '15 at 23:51