windowsrustprocesswindows-rsgetmodulefilename

Rust get executable path of process by window handle or process id


I'm trying to get executable path of process on Windows OS. I have window handle coming from some windows event, from window handle I can acquire process id using GetWindowThreadProcessId(). Then, according to windows api docs I can use OpenProcess() function to make process handle and then with this use GetModuleFileNameExA() or GetModuleFileNameExW() for unicode characters to get process executable path. But sadly, in windows crate I can't find none of this functions.

I tried to use GetWindowModuleFileNameW(), but this works only for process that is calling this function or processes that come from this process (that's might be not exact explanation, but the way I understand it).

I found this functions in winapi crate, but as I was suggested earlier, I better use windows crate because its still maintained unlike winapi.

Also I found kind of way to do this with sysinfo crate, but if possible, I would like to not bring it just for this. Any help will be appreciated!

UPD: There is OpenProcess() function available after I installed rest of windows crate features, but this still can't help me much without GetModuleFileNameExW()

UPDD: I found feature in windows crate called Win32_System_ProcessStatus which contains necessary functions. My problem is solved and I leave this here in case anybody will have same question.


Solution

  • The windows crate, like the API it provides bindings for, is massive. It comprises quite literally hundreds of thousands of line of code. To tame compile times, the crate is split into modules, each gated behind a feature.

    To use any given API binding, client code needs to enable the respective feature(s). The documentation lists the required feature(s) for every public constant, type, or function.

    GetWindowThreadProcessId:

    Required features: "Win32_UI_WindowsAndMessaging", "Win32_Foundation"

    OpenProcess:

    Required features: "Win32_System_Threading", "Win32_Foundation"

    GetModuleFileNameExW:

    Required features: "Win32_System_ProcessStatus", "Win32_Foundation"

    To implement this program, the Cargo.toml file must contain the following table:

    [dependencies.windows]
    version = "0.48.0"    # Current version as of writing
    features = [
        "Win32_Foundation",
        "Win32_System_ProcessStatus",
        "Win32_System_Threading",
        "Win32_UI_WindowsAndMessaging",
    ]