获取当前焦点窗口进程名

 1 void GetForegroundWindowProc(CString& szProcPath)
 2 {
 3     HWND hWnd = ::GetForegroundWindow();
 4     WCHAR winClass[128];
 5     WCHAR winTitle[128];
 6     //通过句柄获取窗口类名
 7     ::GetClassName(hWnd, winClass, 128);
 8 
 9     //通过句柄获取窗口标题
10     ::GetWindowText(hWnd, winTitle, 128);
11 
12     printf("!!!%s, winClass = %S!
", __FUNCTION__, winClass);
13     printf("!!!%s, winTitle = %S!
", __FUNCTION__, winTitle);
14 
15     DWORD dwThreadId;
16     DWORD dwProcId;
17     dwThreadId = GetWindowThreadProcessId(hWnd, &dwProcId);
18     printf("dwProcId = %ul
", dwProcId);
19     printf("dwThreadId = %ul
", dwThreadId);
20 
21     HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcId);
22     WCHAR proname[MAX_PATH] = { 0 };
23 
24     DWORD dwRetLength = GetModuleFileNameEx(hProcess, NULL, proname, MAX_PATH);
25     if (dwRetLength)
26     {
27         szProcPath = proname;
28     }
29 }
 1 bool ParseFileName(_In_ const CString& filePath, _Out_ CString& fileName)
 2 {
 3     WCHAR fPath[MAX_PATH] = { 0 };
 4     PWCHAR pfTail;
 5     wcscpy_s(fPath, filePath.GetString());
 6     pfTail = fPath + filePath.GetLength();
 7     while (pfTail != fPath && *pfTail != L'\')
 8     {
 9         pfTail--;
10     }
11     if (pfTail == fPath)
12         return false;
13     fileName = ++pfTail;
14     return true;
15 }
原文地址:https://www.cnblogs.com/endenvor/p/10882709.html