123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- #include "il2cpp-config.h"
- #if IL2CPP_TARGET_WINDOWS_DESKTOP || IL2CPP_TARGET_WINDOWS_GAMES
- #include "WindowsHelpers.h"
- #include <Psapi.h>
- #include "os/Process.h"
- struct ProcessHandle
- {
- HANDLE handle;
- };
- namespace il2cpp
- {
- namespace os
- {
- int Process::GetCurrentProcessId()
- {
- return ::GetCurrentProcessId();
- }
- utils::Expected<ProcessHandle*> Process::GetProcess(int processId)
- {
- return (ProcessHandle*)OpenProcess(PROCESS_ALL_ACCESS, TRUE, processId);
- }
- void Process::FreeProcess(ProcessHandle* handle)
- {
- ::CloseHandle((HANDLE)handle);
- }
- utils::Expected<std::string> Process::GetProcessName(ProcessHandle* handle)
- {
- const size_t bufferLength = 256;
- WCHAR buf[bufferLength];
- DWORD length = ::GetModuleBaseName((HANDLE)handle, NULL, buf, bufferLength);
- if (length == 0)
- return std::string();
- char multiByteStr[bufferLength];
- size_t numConverted = wcstombs(multiByteStr, buf, bufferLength);
- if (numConverted <= 0)
- return std::string();
- return std::string(multiByteStr, numConverted);
- }
- #if IL2CPP_TARGET_WINDOWS_GAMES
- intptr_t Process::GetMainWindowHandle(int32_t pid)
- {
- return 0;
- }
- #else
- typedef struct
- {
- DWORD pid;
- HWND hwnd;
- } EnumWindowsArgs;
- static BOOL STDCALL Il2CppEnumWindowsCallback(HWND hwnd, LPARAM lparam)
- {
- EnumWindowsArgs* args = (EnumWindowsArgs*)lparam;
- DWORD pid = 0;
- GetWindowThreadProcessId(hwnd, &pid);
- if (pid != args->pid || GetWindow(hwnd, GW_OWNER) != NULL || !IsWindowVisible(hwnd)) return TRUE;
- args->hwnd = hwnd;
- return FALSE;
- }
- intptr_t Process::GetMainWindowHandle(int32_t pid)
- {
- EnumWindowsArgs args = { (DWORD)pid, 0 };
- EnumWindows(Il2CppEnumWindowsCallback, (LPARAM)&args);
- return (intptr_t)args.hwnd;
- }
- #endif
- }
- }
- #endif // IL2CPP_TARGET_WINDOWS_DESKTOP || IL2CPP_TARGET_WINDOWS_GAMES
|