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