Skip to content

armgit / WSM

Public Imported from GitHub
0

Run macOS applications on your Windows desktop, with a background runtime and integrated app windows.

C++
1.5 KB
Raw Blame
#pragma once
#include <windows.h>
#include <filesystem>
namespace wsm::runtime {
inline bool same_executable_file(const std::filesystem::path& a, const std::filesystem::path& b) {
    HANDLE first = CreateFileW(a.c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, 0, nullptr);
    HANDLE second = CreateFileW(b.c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, 0, nullptr);
    BY_HANDLE_FILE_INFORMATION x{}, y{};
    const bool same = first != INVALID_HANDLE_VALUE && second != INVALID_HANDLE_VALUE &&
        GetFileInformationByHandle(first, &x) && GetFileInformationByHandle(second, &y) &&
        x.dwVolumeSerialNumber == y.dwVolumeSerialNumber && x.nFileIndexHigh == y.nFileIndexHigh && x.nFileIndexLow == y.nFileIndexLow;
    if (first != INVALID_HANDLE_VALUE) CloseHandle(first);
    if (second != INVALID_HANDLE_VALUE) CloseHandle(second);
    return same;
}
inline bool process_runs_executable(DWORD pid, const std::filesystem::path& executable) {
    if (!pid) return false;
    HANDLE process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
    if (!process) return false;
    wchar_t image[32768]{}; DWORD length = 32768, code = 0;
    const bool alive = GetExitCodeProcess(process, &code) && code == STILL_ACTIVE && QueryFullProcessImageNameW(process, 0, image, &length);
    CloseHandle(process);
    return alive && same_executable_file(image, executable);
}
}