#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <string>
static std::string _winerr(DWORD dwErr) {
char *lpszTemp;
if (!dwErr || !FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL, dwErr, MAKELANGID(LANG_ENGLISH, SUBLANG_NEUTRAL), (LPSTR)&lpszTemp, 0, NULL)) {
return "(no winapi error)";
} else {
char *i = strchr(lpszTemp, 0);
while (isspace(i[-1])) --i;
std::string res(lpszTemp, i);
LocalFree(lpszTemp);
return res;
}
}
static __inline std::string _winerr() {
return _winerr(GetLastError());
}
static __inline int _showerr(std::string &err) {
return MessageBoxA(NULL, err.c_str(), "Launch RoC - Error Occured", MB_OK | MB_ICONERROR);
}
int WINAPI wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) {
HKEY hKey;
LSTATUS lRes = RegOpenKeyEx(HKEY_CURRENT_USER, L"SOFTWARE\\Blizzard Entertainment\\Warcraft III",
0, KEY_READ, &hKey);
if (lRes != ERROR_SUCCESS) {
_showerr("Failed to open Warcraft III registry key: " + _winerr(lRes));
return 1;
}
WCHAR szBuffer[512];
DWORD cbBufferSize = (DWORD)sizeof(szBuffer);
lRes = RegQueryValueEx(hKey, L"Program", NULL, NULL, (LPBYTE)szBuffer, &cbBufferSize);
RegCloseKey(hKey);
if (lRes != ERROR_SUCCESS) {
_showerr("Failed to retrieve Warcraft III application path: " + _winerr(lRes));
return 1;
}
STARTUPINFO si; ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
PROCESS_INFORMATION pi; ZeroMemory(&pi, sizeof(pi));
if (!CreateProcess(NULL, (LPWSTR)(L'\"' + std::wstring(szBuffer) + L"\" -classic").data(),
NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
_showerr("Failed to launch Warcraft III: " + _winerr());
return 1;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}