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