|
| 1 | +/* |
| 2 | +DataLab-WinPython launcher script |
| 3 | +--------------------------------- |
| 4 | +
|
| 5 | +Licensed under the terms of the BSD 3-Clause |
| 6 | +(see ../LICENSE for details) |
| 7 | +
|
| 8 | +*/ |
| 9 | + |
| 10 | +#include <windows.h> |
| 11 | +#include <string> |
| 12 | + |
| 13 | +int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int /*nShowCmd*/) { |
| 14 | + // Get the path to the current executable |
| 15 | + wchar_t exePath[MAX_PATH]; |
| 16 | + GetModuleFileNameW(NULL, exePath, MAX_PATH); |
| 17 | + |
| 18 | + // Determine the directory of the executable |
| 19 | + std::wstring exeDir = exePath; |
| 20 | + exeDir = exeDir.substr(0, exeDir.find_last_of(L"\\/")); |
| 21 | + |
| 22 | + // Define the path to the "scripts" directory |
| 23 | + std::wstring scriptsDir = exeDir + L"\\scripts"; |
| 24 | + |
| 25 | + // Check if the "scripts" directory exists |
| 26 | + DWORD attributes = GetFileAttributesW(scriptsDir.c_str()); |
| 27 | + if (attributes == INVALID_FILE_ATTRIBUTES || !(attributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 28 | + MessageBoxW(NULL, L"The 'scripts' directory does not exist. Please ensure it is in the same folder as the launcher.", |
| 29 | + L"Launcher Error", MB_ICONERROR); |
| 30 | + return 1; |
| 31 | + } |
| 32 | + |
| 33 | + // Set the working directory to the "scripts" folder |
| 34 | + if (!SetCurrentDirectoryW(scriptsDir.c_str())) { |
| 35 | + MessageBoxW(NULL, L"Failed to set the working directory to 'scripts'.", |
| 36 | + L"Launcher Error", MB_ICONERROR); |
| 37 | + return 1; |
| 38 | + } |
| 39 | + |
| 40 | + // Define the command to run |
| 41 | + std::wstring target = L"cmd.exe /c \"" LAUNCH_TARGET L"\""; |
| 42 | + |
| 43 | + // Configure the process startup info |
| 44 | + STARTUPINFO si = { sizeof(si) }; |
| 45 | + si.dwFlags = STARTF_USESHOWWINDOW; // Prevent the window from appearing |
| 46 | + si.wShowWindow = SW_HIDE; // Hide the command window |
| 47 | + |
| 48 | + PROCESS_INFORMATION pi = {}; |
| 49 | + |
| 50 | + // Start the process with CREATE_NO_WINDOW flag |
| 51 | + if (!CreateProcessW( |
| 52 | + NULL, // Application name (NULL because we pass the command in the command line) |
| 53 | + &target[0], // Command line |
| 54 | + NULL, // Process security attributes |
| 55 | + NULL, // Thread security attributes |
| 56 | + FALSE, // Inherit handles |
| 57 | + CREATE_NO_WINDOW, // Flags to prevent creating a window |
| 58 | + NULL, // Environment block (NULL to inherit parent) |
| 59 | + NULL, // Current directory (NULL to use the parent process's current directory) |
| 60 | + &si, // Startup info |
| 61 | + &pi // Process information |
| 62 | + )) { |
| 63 | + MessageBoxW(NULL, L"Failed to launch the script.", L"Launcher Error", MB_ICONERROR); |
| 64 | + return 1; |
| 65 | + } |
| 66 | + |
| 67 | + // Wait for the script to finish |
| 68 | + WaitForSingleObject(pi.hProcess, INFINITE); |
| 69 | + |
| 70 | + // Cleanup |
| 71 | + CloseHandle(pi.hProcess); |
| 72 | + CloseHandle(pi.hThread); |
| 73 | + |
| 74 | + return 0; |
| 75 | +} |
0 commit comments