2023-07-13 14:08:23 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
|
|
|
|
#include "messaging.hpp"
|
|
|
|
|
|
|
|
#include <hex/helpers/logger.hpp>
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
namespace hex::messaging {
|
|
|
|
|
|
|
|
std::optional<HWND> getImHexWindow() {
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
HWND imhexWindow = nullptr;
|
2023-07-13 14:08:23 +02:00
|
|
|
|
|
|
|
::EnumWindows([](HWND hWnd, LPARAM ret) -> BOOL {
|
|
|
|
// Get the window name
|
|
|
|
auto length = ::GetWindowTextLength(hWnd);
|
|
|
|
std::string windowName(length + 1, '\x00');
|
2024-06-28 11:12:17 +02:00
|
|
|
::GetWindowTextA(hWnd, windowName.data(), windowName.size());
|
2023-07-13 14:08:23 +02:00
|
|
|
|
|
|
|
// Check if the window is visible and if it's an ImHex window
|
|
|
|
if (::IsWindowVisible(hWnd) == TRUE && length != 0) {
|
|
|
|
if (windowName.starts_with("ImHex")) {
|
2023-10-04 12:00:32 +02:00
|
|
|
// It's our window, return it and stop iteration
|
2023-07-13 14:08:23 +02:00
|
|
|
*reinterpret_cast<HWND*>(ret) = hWnd;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Continue iteration
|
2023-07-13 14:08:23 +02:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
}, reinterpret_cast<LPARAM>(&imhexWindow));
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
if (imhexWindow == nullptr)
|
|
|
|
return { };
|
|
|
|
else
|
|
|
|
return imhexWindow;
|
2023-07-13 14:08:23 +02:00
|
|
|
}
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
void sendToOtherInstance(const std::string &eventName, const std::vector<u8> &args) {
|
2023-07-13 14:08:23 +02:00
|
|
|
log::debug("Sending event {} to another instance (not us)", eventName);
|
|
|
|
|
|
|
|
// Get the window we want to send it to
|
|
|
|
HWND imHexWindow = *getImHexWindow();
|
|
|
|
|
|
|
|
// Create the message
|
|
|
|
std::vector<u8> fulleventData(eventName.begin(), eventName.end());
|
|
|
|
fulleventData.push_back('\0');
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
fulleventData.insert(fulleventData.end(), args.begin(), args.end());
|
2023-07-13 14:08:23 +02:00
|
|
|
|
|
|
|
u8 *data = &fulleventData[0];
|
2023-11-10 20:47:08 +01:00
|
|
|
DWORD dataSize = fulleventData.size();
|
2023-07-13 14:08:23 +02:00
|
|
|
|
|
|
|
COPYDATASTRUCT message = {
|
|
|
|
.dwData = 0,
|
|
|
|
.cbData = dataSize,
|
|
|
|
.lpData = data
|
|
|
|
};
|
|
|
|
|
|
|
|
// Send the message
|
|
|
|
SendMessage(imHexWindow, WM_COPYDATA, reinterpret_cast<WPARAM>(imHexWindow), reinterpret_cast<LPARAM>(&message));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool setupNative() {
|
|
|
|
|
2024-06-28 11:12:17 +02:00
|
|
|
constexpr static auto UniqueMutexId = L"ImHex/a477ea68-e334-4d07-a439-4f159c683763";
|
2023-07-13 14:08:23 +02:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Check if an ImHex instance is already running by opening a global mutex
|
2024-06-28 11:12:17 +02:00
|
|
|
HANDLE globalMutex = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, UniqueMutexId);
|
2023-07-13 14:08:23 +02:00
|
|
|
if (globalMutex == nullptr) {
|
|
|
|
// If no ImHex instance is running, create a new global mutex
|
2024-06-28 11:12:17 +02:00
|
|
|
globalMutex = CreateMutexW(nullptr, FALSE, UniqueMutexId);
|
2023-07-13 14:08:23 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|