11# ifdef WIN32_LEAN_AND_MEAN
12# undef WIN32_LEAN_AND_MEAN
23# define WIN32_SKIP_HKEY_PROTECTION
43 GetModuleFileName(
nullptr, filepath,
sizeof(filepath));
46 if (dir[a - 1] ==
'\\') {
59 return (
BLI_strcasestr(install_dir,
"\\WindowsApps\\") !=
nullptr);
62static void registry_error(HKEY root,
const char *message)
67 fprintf(stderr,
"%s\n", message);
70static bool open_registry_hive(
bool all_users, HKEY *r_root)
72 if (RegOpenKeyEx(all_users ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
76 r_root) != ERROR_SUCCESS)
78 registry_error(*r_root,
"Unable to open the registry with the required permissions");
84static bool register_blender_prog_id(
const char *prog_id,
85 const char *executable,
86 const char *friendly_name,
95 if (!open_registry_hive(all_users, &root)) {
99 lresult = RegCreateKeyEx(root,
103 REG_OPTION_NON_VOLATILE,
109 if (lresult == ERROR_SUCCESS) {
110 lresult = RegSetValueEx(
111 hkey_progid,
nullptr, 0, REG_SZ, (BYTE *)friendly_name, strlen(friendly_name) + 1);
113 if (lresult == ERROR_SUCCESS) {
114 lresult = RegSetValueEx(
115 hkey_progid,
"AppUserModelId", 0, REG_SZ, (BYTE *)prog_id, strlen(prog_id) + 1);
117 if (lresult != ERROR_SUCCESS) {
118 registry_error(root,
"Unable to register Blender App Id");
122 SNPRINTF(buffer,
"%s\\shell\\open", prog_id);
123 lresult = RegCreateKeyEx(root,
127 REG_OPTION_NON_VOLATILE,
133 lresult = RegSetValueEx(
134 hkey_progid,
"FriendlyAppName", 0, REG_SZ, (BYTE *)friendly_name, strlen(friendly_name) + 1);
136 SNPRINTF(buffer,
"%s\\shell\\open\\command", prog_id);
138 lresult = RegCreateKeyEx(root,
142 REG_OPTION_NON_VOLATILE,
148 if (lresult == ERROR_SUCCESS) {
149 const size_t buffer_len =
SNPRINTF_RLEN(buffer,
"\"%s\" \"%%1\"", executable);
150 lresult = RegSetValueEx(hkey_progid,
nullptr, 0, REG_SZ, (BYTE *)buffer, buffer_len + 1);
151 RegCloseKey(hkey_progid);
153 if (lresult != ERROR_SUCCESS) {
154 registry_error(root,
"Unable to register Blender App Id");
158 SNPRINTF(buffer,
"%s\\DefaultIcon", prog_id);
159 lresult = RegCreateKeyEx(root,
163 REG_OPTION_NON_VOLATILE,
169 if (lresult == ERROR_SUCCESS) {
170 const size_t buffer_len =
SNPRINTF_RLEN(buffer,
"\"%s\", 1", executable);
171 lresult = RegSetValueEx(hkey_progid,
nullptr, 0, REG_SZ, (BYTE *)buffer, buffer_len + 1);
172 RegCloseKey(hkey_progid);
174 if (lresult != ERROR_SUCCESS) {
175 registry_error(root,
"Unable to register Blender App Id");
184 fprintf(stderr,
"Registration not possible from Microsoft Store installation.");
189 char blender_path[MAX_PATH];
194 const char *prog_id = BLENDER_WIN_APPID;
195 const char *friendly_name = BLENDER_WIN_APPID_FRIENDLY_NAME;
197 GetModuleFileName(0, blender_path,
sizeof(blender_path));
200 if (strlen(blender_path) > (
sizeof(blender_path) - 10)) {
205 blender_app = strstr(blender_path,
"blender.exe");
209 strcpy(blender_app,
"blender-launcher.exe");
211 if (!open_registry_hive(all_users, &root)) {
215 if (!register_blender_prog_id(prog_id, blender_path, friendly_name, all_users)) {
216 registry_error(root,
"Unable to register Blender file type");
220 lresult = RegCreateKeyEx(
221 root,
".blend", 0,
nullptr, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
nullptr, &hkey, &dwd);
222 if (lresult == ERROR_SUCCESS) {
224 lresult = RegSetValueEx(hkey,
nullptr, 0, REG_SZ, (BYTE *)prog_id, strlen(prog_id) + 1);
226 if (lresult != ERROR_SUCCESS) {
227 registry_error(root,
"Unable to register Blender file type");
233 lresult = RegCreateKeyEx(root,
234 ".blend\\OpenWithProgids",
237 REG_OPTION_NON_VOLATILE,
243 if (lresult != ERROR_SUCCESS) {
244 registry_error(root,
"Unable to register Blender file type");
248 lresult = RegSetValueEx(hkey, prog_id, 0, REG_NONE,
nullptr, 0);
252 if (lresult != ERROR_SUCCESS) {
253 registry_error(root,
"Unable to register Blender file type");
258 fprintf(stderr,
"Update of pinned launcher failed.");
262# ifdef WITH_BLENDER_THUMBNAILER
264 char reg_cmd[MAX_PATH * 2];
268 GetSystemDirectory(system_dir,
sizeof(system_dir));
269 const char *thumbnail_handler =
"BlendThumb.dll";
270 SNPRINTF(reg_cmd,
"%s\\regsvr32 /s \"%s\\%s\"", system_dir, install_dir, thumbnail_handler);
278 "Blend file extension registered for %s.",
279 all_users ?
"all users" :
"the current user");
288 fprintf(stderr,
"Unregistration not possible from Microsoft Store installation.");
296 if (!open_registry_hive(all_users, &root)) {
302 RegDeleteTree(root, BLENDER_WIN_APPID);
304 lresult = RegOpenKeyEx(root,
".blend", 0, KEY_ALL_ACCESS, &hkey);
305 if (lresult == ERROR_SUCCESS) {
306 char buffer[256] = {0};
307 DWORD
size =
sizeof(buffer);
308 lresult = RegGetValueA(hkey,
nullptr,
nullptr, RRF_RT_REG_SZ,
nullptr, &buffer, &size);
309 if (lresult == ERROR_SUCCESS &&
STREQ(buffer, BLENDER_WIN_APPID)) {
310 RegSetValueEx(hkey,
nullptr, 0, REG_SZ, 0, 0);
314# ifdef WITH_BLENDER_THUMBNAILER
316 char reg_cmd[MAX_PATH * 2];
320 GetSystemDirectory(system_dir,
sizeof(system_dir));
321 const char *thumbnail_handler =
"BlendThumb.dll";
322 SNPRINTF(reg_cmd,
"%s\\regsvr32 /u /s \"%s\\%s\"", system_dir, install_dir, thumbnail_handler);
327 lresult = RegOpenKeyEx(hkey,
"OpenWithProgids", 0, KEY_ALL_ACCESS, &hkey);
328 if (lresult == ERROR_SUCCESS) {
329 RegDeleteValue(hkey, BLENDER_WIN_APPID);
335 "Blend file extension unregistered for %s.",
336 all_users ?
"all users" :
"the current user");
348static bool BLI_windows_file_operation_is_registered(
const char *extension,
const char *operation)
351 HRESULT hr = AssocQueryKey(ASSOCF_INIT_IGNOREUNKNOWN,
352 ASSOCKEY_SHELLEXECCLASS,
365 if (
STREQ(operation,
"open") ||
STREQ(operation,
"properties")) {
370 return BLI_windows_file_operation_is_registered(
"Directory", operation);
374 return BLI_windows_file_operation_is_registered(extension, operation);
389 SHELLEXECUTEINFOW shellinfo = {0};
390 shellinfo.cbSize =
sizeof(SHELLEXECUTEINFO);
391 shellinfo.fMask = SEE_MASK_INVOKEIDLIST;
392 shellinfo.lpVerb = woperation;
393 shellinfo.lpFile = wpath;
394 shellinfo.nShow = SW_SHOW;
396 return ShellExecuteExW(&shellinfo);
404 char blender_path[MAX_PATH];
405 GetModuleFileName(0, blender_path, MAX_PATH);
407 SHELLEXECUTEINFOA shellinfo = {0};
408 shellinfo.cbSize =
sizeof(SHELLEXECUTEINFO);
409 shellinfo.fMask = wait ? SEE_MASK_NOCLOSEPROCESS : SEE_MASK_DEFAULT;
410 shellinfo.hwnd =
nullptr;
411 shellinfo.lpVerb = elevated ?
"runas" :
nullptr;
412 shellinfo.lpFile = blender_path;
413 shellinfo.lpParameters = parameters;
414 shellinfo.lpDirectory =
nullptr;
415 shellinfo.nShow = silent ? SW_HIDE : SW_SHOW;
416 shellinfo.hInstApp =
nullptr;
417 shellinfo.hProcess = 0;
420 if (!ShellExecuteExA(&shellinfo)) {
427 if (shellinfo.hProcess != 0) {
428 WaitForSingleObject(shellinfo.hProcess, INFINITE);
429 GetExitCodeProcess(shellinfo.hProcess, &exitCode);
430 CloseHandle(shellinfo.hProcess);
431 return (exitCode == 0);
439 char str[MAX_PATH + 1];
444 if (GetWindowsDirectory(
str, MAX_PATH + 1)) {
453 if (GetModuleFileName(
nullptr,
str, MAX_PATH + 1)) {
455 "Error! Could not get the Windows Directory - "
456 "Defaulting to Blender installation Dir!\n");
468 "Error! Could not get the Windows Directory - "
469 "Defaulting to first valid drive! Path might be invalid!\n");
470 tmp = GetLogicalDrives();
471 for (i = 2; i < 26; i++) {
472 if ((tmp >> i) & 1) {
477 if (GetFileAttributes(root) != 0xFFFFFFFF) {
484 printf(
"ERROR in 'BLI_windows_get_default_root_dir': cannot find a valid drive!\n");
495 long long *r_driverVersion)
497 IDXGIFactory *pFactory =
nullptr;
498 IDXGIAdapter *pAdapter =
nullptr;
499 if (CreateDXGIFactory(__uuidof(IDXGIFactory), (
void **)&pFactory) == S_OK) {
500 for (UINT i = 0; pFactory->EnumAdapters(i, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++i) {
502 if (pAdapter->CheckInterfaceSupport(__uuidof(IDXGIDevice), &version) == S_OK) {
503 DXGI_ADAPTER_DESC desc;
504 if (pAdapter->GetDesc(&desc) == S_OK) {
505 if (wcsstr(desc.Description, deviceSubString)) {
506 *r_driverVersion =
version.QuadPart;
524 int *driver_version_minor,
525 int *driver_version_major,
528 IDXGIFactory *pFactory =
nullptr;
529 IDXGIAdapter *pAdapter =
nullptr;
530 if (CreateDXGIFactory(__uuidof(IDXGIFactory), (
void **)&pFactory) == S_OK) {
531 for (UINT i = 0; pFactory->EnumAdapters(i, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++i) {
533 if (pAdapter->CheckInterfaceSupport(__uuidof(IDXGIDevice), &version) == S_OK) {
534 DXGI_ADAPTER_DESC desc;
535 if (pAdapter->GetDesc(&desc) == S_OK) {
538 if (device_luid == adapter_luid) {
539 *device_id = desc.DeviceId;
540 *driver_version_major = int((
version.QuadPart >> 16) & 0xFFFF);
541 *driver_version_minor = int(
version.QuadPart & 0xFFFF);
562 HMODULE hMod = ::GetModuleHandleW(
L"ntdll.dll");
567 typedef NTSTATUS(WINAPI * RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
568 RtlGetVersionPtr rtl_get_version = (RtlGetVersionPtr)::GetProcAddress(hMod,
"RtlGetVersion");
569 if (rtl_get_version ==
nullptr) {
570 fprintf(stderr,
"BLI_windows_is_build_version_greater_or_equal: RtlGetVersion not found.");
574 RTL_OSVERSIONINFOW osVersioninfo{};
575 osVersioninfo.dwOSVersionInfoSize =
sizeof(RTL_OSVERSIONINFOW);
576 if (rtl_get_version(&osVersioninfo) != 0) {
577 fprintf(stderr,
"BLI_windows_is_build_version_greater_or_equal: RtlGetVersion failed.");
580 if (majorVersion != osVersioninfo.dwMajorVersion) {
581 return osVersioninfo.dwMajorVersion > majorVersion;
583 if (minorVersion != osVersioninfo.dwMinorVersion) {
584 return osVersioninfo.dwMajorVersion > minorVersion;
586 return osVersioninfo.dwBuildNumber >= buildNumber;
592 if (
int(qos_precedence) <
int(qos_precedence_last)) {
603 PROCESS_POWER_THROTTLING_STATE processPowerThrottlingState{};
604 processPowerThrottlingState.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION;
607 processPowerThrottlingState.ControlMask = 0;
608 processPowerThrottlingState.StateMask = 0;
611 processPowerThrottlingState.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
612 processPowerThrottlingState.StateMask = 0;
615 processPowerThrottlingState.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
616 processPowerThrottlingState.StateMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
619 HANDLE hProcess = GetCurrentProcess();
620 if (!SetProcessInformation(hProcess,
621 ProcessPowerThrottling,
622 &processPowerThrottlingState,
623 sizeof(PROCESS_POWER_THROTTLING_STATE)))
626 "BLI_windows_set_process_qos: SetProcessInformation failed: %lx\n",
630 qos_precedence_last = qos_precedence;
File and directory operations.
#define SNPRINTF(dst, format,...)
#define SNPRINTF_RLEN(dst, format,...)
Compatibility-like things for windows.
Read Blender memory allocation and freeing.
int size(const AssetLibraryReference *library_reference)
constexpr toml::spec version
bool BLI_windows_unregister_blend_extension(bool all_users)
unsigned __int64 uint64_t
bool BLI_windows_execute_self(const char *parameters, const bool wait, const bool elevated, const bool silent)
bool BLI_windows_external_operation_execute(const char *filepath, const char *operation)
const char * BLI_path_extension(const char *filepath) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
int char * BLI_strcasestr(const char *s, const char *find) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
bool BLI_windows_get_directx_driver_version(const wchar_t *deviceSubString, long long *r_driverVersion)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
bool BLI_windows_update_pinned_launcher(const char *launcher_path)
bool BLI_windows_is_store_install(void)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool BLI_windows_get_directx_intel_driver_info(uint64_t device_luid, int *driver_version_minor, int *driver_version_major, uint32_t *device_id)
void BLI_windows_process_set_qos(QoSMode qos_mode, QoSPrecedence qos_precedence)
bool BLI_windows_external_operation_supported(const char *filepath, const char *operation)
bool BLI_windows_register_blend_extension(bool all_users)
int BLI_windows_get_executable_dir(char r_dirpath[])
void BLI_windows_get_default_root_dir(char root_dir[4])
bool BLI_windows_is_build_version_greater_or_equal(DWORD majorVersion, DWORD minorVersion, DWORD buildNumber)
int conv_utf_8_to_16(const char *in8, wchar_t *out16, size_t size16)