diff --git a/src/core/kernel/init/CxbxKrnl.cpp b/src/core/kernel/init/CxbxKrnl.cpp index b35a4a645..8da4a072f 100644 --- a/src/core/kernel/init/CxbxKrnl.cpp +++ b/src/core/kernel/init/CxbxKrnl.cpp @@ -872,11 +872,38 @@ void ImportLibraries(XbeImportEntry *pImportDirectory) } } +bool CheckLoadArgument(int argc, char* argv[], DWORD *pguiProcessID) +{ + bool bHasLoadArgument; + + if (argc >= 2 && std::strcmp(argv[1], "/load") == 0 && std::strlen(argv[2]) > 0) { + HWND hWnd = nullptr; + bHasLoadArgument = true; + // Perform check if command line contain gui's hWnd value. + if (argc > 3) { + hWnd = (HWND)std::stoi(argv[3], nullptr, 10); + hWnd = IsWindow(hWnd) ? hWnd : nullptr; + if (hWnd != nullptr) { + // We don't need thread ID from window handle. + GetWindowThreadProcessId(hWnd, pguiProcessID); + } + } + } + else { + bHasLoadArgument = false; + *pguiProcessID = GetCurrentProcessId(); + } + + g_exec_filepath = argv[0]; // NOTE: Workaround solution until simulated "main" function is made. + + return bHasLoadArgument; +} + bool CreateSettings() { g_Settings = new Settings(); if (g_Settings == nullptr) { - MessageBox(nullptr, szSettings_alloc_error, "Cxbx-Reloaded", MB_OK); + MessageBox(nullptr, szSettings_alloc_error, "Cxbx-Reloaded", MB_OK | MB_ICONERROR); return false; } diff --git a/src/core/kernel/init/CxbxKrnl.h b/src/core/kernel/init/CxbxKrnl.h index 1d29a90aa..42180ee91 100644 --- a/src/core/kernel/init/CxbxKrnl.h +++ b/src/core/kernel/init/CxbxKrnl.h @@ -224,6 +224,8 @@ bool CxbxKrnlVerifyVersion(const char *szVersion); extern bool g_bIsDebugKernel; +bool CheckLoadArgument(int argc, char* argv[], DWORD *pguiProcessID); + bool CreateSettings(); bool HandleFirstLaunch(); diff --git a/src/emulator/CxbxEmulator.cpp b/src/emulator/CxbxEmulator.cpp index 9f516576f..1ef3a05d8 100644 --- a/src/emulator/CxbxEmulator.cpp +++ b/src/emulator/CxbxEmulator.cpp @@ -133,30 +133,40 @@ DWORD WINAPI Emulate(int system) /*! Verify our host executable, CxbxLoader.exe, is loaded to base address 0x00010000 */ if (!VerifyBaseAddr()) { - MessageBox(NULL, "CxbxLoader.exe was not loaded to base address 0x00010000 (which is a requirement for Xbox emulation)", "Cxbx-Reloaded", MB_OK); + MessageBox(NULL, "CxbxLoader.exe was not loaded to base address 0x00010000 (which is a requirement for Xbox emulation)", "Cxbx-Reloaded", + MB_OK | MB_ICONERROR); return EXIT_FAILURE; } // Before doing anything else that might cause memory fragmentation, // verify that we still got control over all ranges the loader reserved if (!VerifyAddressRanges(system)) { - MessageBox(NULL, "Failed to claim required address ranges (which is a requirement for Xbox emulation)", "Cxbx-Reloaded", MB_OK); + MessageBox(NULL, "Failed to claim required address ranges (which is a requirement for Xbox emulation)", "Cxbx-Reloaded", + MB_OK | MB_ICONERROR); return EXIT_FAILURE; } - /*! initialize shared memory */ - if (!EmuShared::Init(0)) { - MessageBox(NULL, "Could not map shared memory!", "Cxbx-Reloaded", MB_OK); - return EXIT_FAILURE; - } - - HandleFirstLaunch(); - LPSTR CommandLine = GetCommandLine(); int argc; PCHAR *argv = CommandLineToArgvA(CommandLine, &argc); - CxbxKrnlMain(argc, argv); + DWORD guiProcessID = 0; + bool bHasLoadArgument = CheckLoadArgument(argc, argv, &guiProcessID); + + /*! initialize shared memory */ + if (!EmuShared::Init(guiProcessID)) { + MessageBox(NULL, "Could not map shared memory!", "Cxbx-Reloaded", MB_OK | MB_ICONERROR); + LocalFree(argv); + return EXIT_FAILURE; + } + + HandleFirstLaunch(); + + if (bHasLoadArgument) { + CxbxKrnlMain(argc, argv); + } else { + // TODO : Enter "2nd GUI" mode here, as done in Cxbx's WinMain() - but how? + } LocalFree(argv); diff --git a/src/gui/WinMain.cpp b/src/gui/WinMain.cpp index 31035f6a1..f0ed1e34f 100644 --- a/src/gui/WinMain.cpp +++ b/src/gui/WinMain.cpp @@ -60,28 +60,9 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine return EXIT_FAILURE; } - bool bHasLoadArgument; - HWND hWnd = nullptr; DWORD guiProcessID = 0; - // TODO: Convert ALL __argc & __argv to use main(int argc, char** argv) method. - if (__argc >= 2 && std::strcmp(__argv[1], "/load") == 0 && std::strlen(__argv[2]) > 0) { - bHasLoadArgument = true; - // Perform check if command line contain gui's hWnd value. - if (__argc > 3) { - hWnd = (HWND)std::stoi(__argv[3], nullptr, 10); - hWnd = IsWindow(hWnd) ? hWnd : nullptr; - if (hWnd != nullptr) { - // We don't need thread ID from window handle. - GetWindowThreadProcessId(hWnd, &guiProcessID); - } - } - } else { - bHasLoadArgument = false; - guiProcessID = GetCurrentProcessId(); - } - - g_exec_filepath = __argv[0]; // NOTE: Workaround solution until simulated "main" function is made. + bool bHasLoadArgument = CheckLoadArgument(__argc, __argv, &guiProcessID); /*! initialize shared memory */ EmuShared::Init(guiProcessID);