Blender V5.3
blender_launcher_win32.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#ifdef WIN32_LEAN_AND_MEAN
10# undef WIN32_LEAN_AND_MEAN
11#endif
12#include <Windows.h>
13#include <strsafe.h>
14
15#include <PathCch.h>
16#include <tlhelp32.h>
17
18static BOOL LaunchedFromSteam(void)
19{
20 HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
21 BOOL isSteam = FALSE;
22 if (!hSnapShot) {
23 return (FALSE);
24 }
25
26 PROCESSENTRY32 process_entry;
27 process_entry.dwSize = sizeof(PROCESSENTRY32);
28
29 if (!Process32First(hSnapShot, &process_entry)) {
30 CloseHandle(hSnapShot);
31 return (FALSE);
32 }
33
34 /* First find our parent process ID. */
35 DWORD our_pid = GetCurrentProcessId();
36 DWORD parent_pid = -1;
37
38 do {
39 if (process_entry.th32ProcessID == our_pid) {
40 parent_pid = process_entry.th32ParentProcessID;
41 break;
42 }
43 } while (Process32Next(hSnapShot, &process_entry));
44
45 if (parent_pid == -1 || !Process32First(hSnapShot, &process_entry)) {
46 CloseHandle(hSnapShot);
47 return (FALSE);
48 }
49 /* Then do another loop to find the process name of the parent.
50 * this is done in 2 loops, since the order of the processes is
51 * unknown and we may already have passed the parent process by
52 * the time we figure out its pid in the first loop. */
53 do {
54 if (process_entry.th32ProcessID == parent_pid) {
55 if (_wcsicmp(process_entry.szExeFile, L"steam.exe") == 0) {
56 isSteam = TRUE;
57 }
58 break;
59 }
60 } while (Process32Next(hSnapShot, &process_entry));
61
62 CloseHandle(hSnapShot);
63 return isSteam;
64}
65
66int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
67{
68 /* Silence unreferenced formal parameter warning. */
69 (void)hInstance;
70 (void)hPrevInstance;
71 (void)nCmdShow;
72
73 STARTUPINFO siStartInfo = {0};
74 PROCESS_INFORMATION procInfo;
75 wchar_t path[MAX_PATH];
76
77 siStartInfo.wShowWindow = SW_HIDE;
78 siStartInfo.dwFlags = STARTF_USESHOWWINDOW;
79
80 /* Get the path to the currently running executable (`blender-launcher.exe`). */
81
82 DWORD nSize = GetModuleFileName(NULL, path, MAX_PATH);
83 if (!nSize) {
84 return -1;
85 }
86
87 /* #GetModuleFileName returns the number of characters written, but GetLastError needs to be
88 * called to see if it ran out of space or not. However where would we be without exceptions
89 * to the rule: "If the buffer is too small to hold the module name, the function returns nSize.
90 * The last error code remains ERROR_SUCCESS." - source: MSDN. */
91
92 if (GetLastError() == ERROR_SUCCESS && nSize == MAX_PATH) {
93 return -1;
94 }
95
96 /* Remove the filename (blender-launcher.exe) from path. */
97 if (PathCchRemoveFileSpec(path, MAX_PATH) != S_OK) {
98 return -1;
99 }
100
101 /* Add blender.exe to path, resulting in the full path to the blender executable. */
102 if (PathCchCombine(path, MAX_PATH, path, L"blender.exe") != S_OK) {
103 return -1;
104 }
105
106 int required_size_chars = lstrlenW(path) + /* Module name. */
107 3 + /* 2 quotes + Space. */
108 lstrlenW(pCmdLine) + /* Original command line. */
109 1; /* Zero terminator. */
110 size_t required_size_bytes = required_size_chars * sizeof(wchar_t);
111 wchar_t *buffer = (wchar_t *)malloc(required_size_bytes);
112 if (!buffer) {
113 return -1;
114 }
115
116 if (StringCbPrintfEx(buffer,
117 required_size_bytes,
118 NULL,
119 NULL,
120 STRSAFE_NULL_ON_FAILURE,
121 L"\"%s\" %s",
122 path,
123 pCmdLine) != S_OK)
124 {
125 free(buffer);
126 return -1;
127 }
128
129 BOOL success = CreateProcess(
130 path, buffer, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &siStartInfo, &procInfo);
131
132 DWORD returnValue = success ? 0 : -1;
133
134 if (success) {
135 /* If blender-launcher is called with background command line flag or launched from steam,
136 * wait for the blender process to exit and return its return value. */
137 BOOL background = LaunchedFromSteam();
138 int argc = 0;
139 LPWSTR *argv = CommandLineToArgvW(pCmdLine, &argc);
140 for (int i = 0; i < argc; i++) {
141 if ((wcscmp(argv[i], L"-b") == 0) || (wcscmp(argv[i], L"--background") == 0)) {
142 background = TRUE;
143 break;
144 }
145 }
146
147 if (background) {
148 WaitForSingleObject(procInfo.hProcess, INFINITE);
149 GetExitCodeProcess(procInfo.hProcess, &returnValue);
150 }
151
152 /* Handles in PROCESS_INFORMATION must be closed with CloseHandle when they are no longer
153 * needed - MSDN. Closing the handles will NOT terminate the thread/process that we just
154 * started. */
155 CloseHandle(procInfo.hThread);
156 CloseHandle(procInfo.hProcess);
157 }
158
159 free(buffer);
160 return returnValue;
161}
static BOOL LaunchedFromSteam(void)
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
#define L