问题
I've confronted with problem of getting active window's name.
When I use this code:
HWND currentWindowHWND = GetForegroundWindow();
char title[100];
GetWindowTextA(currentWindowHWND, title, 100);
I get something like: "How to get active window's name? - Stack Overflow - Google Chrome".
But I want to get "Google Chrome", which WINAPI function should I use?
回答1:
in a c
code use the following winapi functions:
DWORD WINAPI GetModuleFileName(
_In_opt_ HMODULE hModule,
_Out_ LPTSTR lpFilename,
_In_ DWORD nSize
);
or
DWORD WINAPI GetModuleBaseName(
_In_ HANDLE hProcess,
_In_opt_ HMODULE hModule,
_Out_ LPTSTR lpBaseName,
_In_ DWORD nSize
);
How to get the process name in C++
In c#
:
Int32 pid = win32.GetWindowProcessID(hwnd);
Process p = Process.GetProcessById(pid);
string appName = p.ProcessName;
You should get the process name and not the window's title.
来源:https://stackoverflow.com/questions/14540118/how-to-get-name-of-active-window