How to Get the Executable Name of a Window

How to get the Executable name of a window

The GetWindowModuleFileName function works for windows in the current process only.

You have to do the following:

  1. Retrieve the window's process with GetWindowThreadProcessId.
  2. Open the process with PROCESS_QUERY_INFORMATION and PROCESS_VM_READ access rights using OpenProcess.
  3. Use GetModuleFileNameEx on the process handle.

If you really want to obtain the name of the module with which the window is registered (as opposed to the process executable), you can obtain the module handle with GetWindowLongPtr with GWLP_HINSTANCE. The module handle can then be passed to the aforementioned GetModuleFileNameEx.

Example:

TCHAR buffer[MAX_PATH] = {0};
DWORD dwProcId = 0;

GetWindowThreadProcessId(hWnd, &dwProcId);

HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ , FALSE, dwProcId);
GetModuleFileName((HMODULE)hProc, buffer, MAX_PATH);
CloseHandle(hProc);

How to get the executable file name from a window handle?

Here is a procedure which I use, which you are likely to find in other places on the internet. I don't recall the exact source, it may have been https://www.swissdelphicenter.ch.

uses
Windows, TlHelp32, ...

function WindowHandleToEXEName(handle : THandle) : string;
var
snap : THandle;
pe : tagPROCESSENTRY32;
pid : THandle;
found : boolean;
begin
Windows.SetLastError(ERROR_SUCCESS);

result := '';
if (handle = 0) then exit;

snap := TLHelp32.CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snap = Cardinal(-1)) then exit;

Windows.GetWindowThreadProcessId(handle, @pid);
pe.dwSize := Sizeof(pe);
found := TLHelp32.Process32First(snap, pe);

while found do
begin
if (pe.th32ProcessID = pid) then
begin
result := String(pe.szExeFile);
break;
end;
found := TLHelp32.Process32Next(snap, pe);
end;
CloseHandle(snap);
end;

How can program get executable name of itself?

#include <windows.h>
#include <Shlwapi.h>
// remember to link against shlwapi.lib
// in VC++ this can be done with
#pragma comment(lib, "Shlwapi.lib")

// ...

TCHAR buffer[MAX_PATH]={0};
TCHAR * out;
DWORD bufSize=sizeof(buffer)/sizeof(*buffer);
// Get the fully-qualified path of the executable
if(GetModuleFileName(NULL, buffer, bufSize)==bufSize)
{
// the buffer is too small, handle the error somehow
}
// now buffer = "c:\whatever\yourexecutable.exe"

// Go to the beginning of the file name
out = PathFindFileName(buffer);
// now out = "yourexecutable.exe"

// Set the dot before the extension to 0 (terminate the string there)
*(PathFindExtension(out)) = 0;
// now out = "yourexecutable"

Now in out you have a pointer to the "base name" of your executable; keep in mind that it points inside buffer, so when buffer goes out of scope out is not valid anymore.

How do I get the name of the current executable in C#?

System.AppDomain.CurrentDomain.FriendlyName

Getting executable name of current focused window?

With Standard C++: no.
With the Windows API: yes.

Retrieve the currently focussed window with GetForegroundWindow , obtain it's process ID with GetWindowThreadProcessId then call OpenProcess with the access rights needed by GetModuleFileNameEx.

How to Get the Filename of the Currently Running Executable in C++

On windows you can use:

TCHAR szExeFileName[MAX_PATH]; 
GetModuleFileName(NULL, szExeFileName, MAX_PATH);

szExeFileName will contain full path + executable name

[edit]

For more portable solution use argv[0] or some other platform specific code. You can find such aproach here: https://github.com/mirror/boost/blob/master/libs/log/src/process_name.cpp.

How to get the application executable name in WindowsC++/CLI?

Call GetModuleFileName() using 0 as a module handle.

Note: you can also use the argv[0] parameter to main or call GetCommandLine() if there is no main. However, keep in mind that these methods will not necessarily give you the complete path to the executable file. They will give back the same string of characters that was used to start the program. Calling GetModuleFileName(), instead, will always give you a complete path and file name.

How can I get the current instance's executable file name from native win32 C++ app?

You can do this via the GetModuleFileName function.

TCHAR szFileName[MAX_PATH];

GetModuleFileName(NULL, szFileName, MAX_PATH)


Related Topics



Leave a reply



Submit