Run macOS applications on your Windows desktop, with a background runtime and integrated app windows.
C++
#include "wsm/qmp_unix.hpp"
#include <winsock2.h>
#include <afunix.h>
#include <windows.h>
#include <cstring>
#include <filesystem>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
static bool line(SOCKET s) {
char c;
while (recv(s, &c, 1, 0) == 1) if (c == '\n') return true;
return false;
}
static void reply(SOCKET s, const char* text) { send(s, text, static_cast<int>(strlen(text)), 0); }
int main(int argc, char**) {
if (argc > 1) { Sleep(5000); return 0; }
WSADATA data{};
if (WSAStartup(MAKEWORD(2, 2), &data)) return 1;
for (bool persistent : {false, true}) {
const auto path = (std::filesystem::temp_directory_path() /
("qmp-inherit-" + std::to_string(GetCurrentProcessId()) + (persistent ? "s" : "c") + ".sock")).string();
if (path.size() >= sizeof(SOCKADDR_UN{}.sun_path)) return 1;
SOCKET listener = WSASocketW(AF_UNIX, SOCK_STREAM, 0, nullptr, 0,
WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT);
SOCKADDR_UN address{}; address.sun_family = AF_UNIX;
memcpy(address.sun_path, path.c_str(), path.size() + 1);
if (bind(listener, reinterpret_cast<sockaddr*>(&address), sizeof(address)) || listen(listener, 1)) return 1;
bool eof = false, child_started = false;
std::thread server([&] {
SOCKET client = accept(listener, nullptr, nullptr);
SetHandleInformation(reinterpret_cast<HANDLE>(client), HANDLE_FLAG_INHERIT, 0);
DWORD timeout = 1500;
setsockopt(client, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<const char*>(&timeout), sizeof(timeout));
reply(client, "{\"QMP\":{}}\n");
if (!line(client)) { closesocket(client); return; }
wchar_t exe[MAX_PATH]{}; GetModuleFileNameW(nullptr, exe, MAX_PATH);
std::wstring command = L"\"" + std::wstring(exe) + L"\" --hold";
STARTUPINFOW startup{}; startup.cb = sizeof(startup); PROCESS_INFORMATION child{};
child_started = CreateProcessW(nullptr, command.data(), nullptr, nullptr, TRUE,
CREATE_NO_WINDOW, nullptr, nullptr, &startup, &child) != 0;
reply(client, "{\"return\":{}}\n");
if (line(client)) reply(client, "{\"return\":{}}\n");
char c;
// The helper remains alive: EOF proves it did not inherit the production socket.
eof = recv(client, &c, 1, 0) == 0;
if (child_started) {
TerminateProcess(child.hProcess, 0); WaitForSingleObject(child.hProcess, 5000);
CloseHandle(child.hThread); CloseHandle(child.hProcess);
}
closesocket(client);
});
std::string result; bool ok;
if (persistent) {
wsm::core::QmpSession q(path); ok = q.execute("{\"execute\":\"query-status\"}", result); q.close();
} else ok = wsm::core::QmpUnixClient(path).execute("{\"execute\":\"query-status\"}", result);
server.join(); closesocket(listener); std::filesystem::remove(path);
if (!ok || !child_started || !eof) { std::cerr << "A helper retained the QMP connection\n"; return 1; }
}
WSACleanup();
std::cout << "Short-lived and persistent QMP connections are not retained by child processes.\n";
}