"Fossies" - the Fresh Open Source Software Archive

Member "blender-3.3.1/source/blender/blenlib/intern/winstuff.c" (28 Jul 2022, 6786 Bytes) of package /linux/misc/blender-3.3.1.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "winstuff.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3.1.2_vs_3.2.0.

    1 /* SPDX-License-Identifier: GPL-2.0-or-later
    2  * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
    3 
    4 /** \file
    5  * \ingroup bli
    6  * WIN32-POSIX compatibility layer, MS-Windows-specific functions.
    7  */
    8 
    9 #ifdef WIN32
   10 
   11 #  include <conio.h>
   12 #  include <stdio.h>
   13 #  include <stdlib.h>
   14 
   15 #  include "MEM_guardedalloc.h"
   16 
   17 #  define WIN32_SKIP_HKEY_PROTECTION /* Need to use HKEY. */
   18 #  include "BLI_path_util.h"
   19 #  include "BLI_string.h"
   20 #  include "BLI_utildefines.h"
   21 #  include "BLI_winstuff.h"
   22 
   23 #  include "utf_winfunc.h"
   24 #  include "utfconv.h"
   25 
   26 /* FILE_MAXDIR + FILE_MAXFILE */
   27 
   28 int BLI_windows_get_executable_dir(char *str)
   29 {
   30   char dir[FILE_MAXDIR];
   31   int a;
   32   /* Change to utf support. */
   33   GetModuleFileName(NULL, str, FILE_MAX);
   34   BLI_split_dir_part(str, dir, sizeof(dir)); /* shouldn't be relative */
   35   a = strlen(dir);
   36   if (dir[a - 1] == '\\') {
   37     dir[a - 1] = 0;
   38   }
   39 
   40   strcpy(str, dir);
   41 
   42   return 1;
   43 }
   44 
   45 static void register_blend_extension_failed(HKEY root, const bool background)
   46 {
   47   printf("failed\n");
   48   if (root) {
   49     RegCloseKey(root);
   50   }
   51   if (!background) {
   52     MessageBox(0, "Could not register file extension.", "Blender error", MB_OK | MB_ICONERROR);
   53   }
   54 }
   55 
   56 bool BLI_windows_register_blend_extension(const bool background)
   57 {
   58   LONG lresult;
   59   HKEY hkey = 0;
   60   HKEY root = 0;
   61   BOOL usr_mode = false;
   62   DWORD dwd = 0;
   63   char buffer[256];
   64 
   65   char BlPath[MAX_PATH];
   66   char InstallDir[FILE_MAXDIR];
   67   char SysDir[FILE_MAXDIR];
   68   const char *ThumbHandlerDLL;
   69   char RegCmd[MAX_PATH * 2];
   70   char MBox[256];
   71   char *blender_app;
   72 #  ifndef _WIN64
   73   BOOL IsWOW64;
   74 #  endif
   75 
   76   printf("Registering file extension...");
   77   GetModuleFileName(0, BlPath, MAX_PATH);
   78 
   79   /* Replace the actual app name with the wrapper. */
   80   blender_app = strstr(BlPath, "blender.exe");
   81   if (blender_app != NULL) {
   82     strcpy(blender_app, "blender-launcher.exe");
   83   }
   84 
   85   /* root is HKLM by default */
   86   lresult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Classes", 0, KEY_ALL_ACCESS, &root);
   87   if (lresult != ERROR_SUCCESS) {
   88     /* try HKCU on failure */
   89     usr_mode = true;
   90     lresult = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Classes", 0, KEY_ALL_ACCESS, &root);
   91     if (lresult != ERROR_SUCCESS) {
   92       register_blend_extension_failed(0, background);
   93       return false;
   94     }
   95   }
   96 
   97   lresult = RegCreateKeyEx(
   98       root, "blendfile", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
   99   if (lresult == ERROR_SUCCESS) {
  100     strcpy(buffer, "Blender File");
  101     lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, (BYTE *)buffer, strlen(buffer) + 1);
  102     RegCloseKey(hkey);
  103   }
  104   if (lresult != ERROR_SUCCESS) {
  105     register_blend_extension_failed(root, background);
  106     return false;
  107   }
  108 
  109   lresult = RegCreateKeyEx(root,
  110                            "blendfile\\shell\\open\\command",
  111                            0,
  112                            NULL,
  113                            REG_OPTION_NON_VOLATILE,
  114                            KEY_ALL_ACCESS,
  115                            NULL,
  116                            &hkey,
  117                            &dwd);
  118   if (lresult == ERROR_SUCCESS) {
  119     sprintf(buffer, "\"%s\" \"%%1\"", BlPath);
  120     lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, (BYTE *)buffer, strlen(buffer) + 1);
  121     RegCloseKey(hkey);
  122   }
  123   if (lresult != ERROR_SUCCESS) {
  124     register_blend_extension_failed(root, background);
  125     return false;
  126   }
  127 
  128   lresult = RegCreateKeyEx(root,
  129                            "blendfile\\DefaultIcon",
  130                            0,
  131                            NULL,
  132                            REG_OPTION_NON_VOLATILE,
  133                            KEY_ALL_ACCESS,
  134                            NULL,
  135                            &hkey,
  136                            &dwd);
  137   if (lresult == ERROR_SUCCESS) {
  138     sprintf(buffer, "\"%s\", 1", BlPath);
  139     lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, (BYTE *)buffer, strlen(buffer) + 1);
  140     RegCloseKey(hkey);
  141   }
  142   if (lresult != ERROR_SUCCESS) {
  143     register_blend_extension_failed(root, background);
  144     return false;
  145   }
  146 
  147   lresult = RegCreateKeyEx(
  148       root, ".blend", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwd);
  149   if (lresult == ERROR_SUCCESS) {
  150     strcpy(buffer, "blendfile");
  151     lresult = RegSetValueEx(hkey, NULL, 0, REG_SZ, (BYTE *)buffer, strlen(buffer) + 1);
  152     RegCloseKey(hkey);
  153   }
  154   if (lresult != ERROR_SUCCESS) {
  155     register_blend_extension_failed(root, background);
  156     return false;
  157   }
  158 
  159 #  ifdef WITH_BLENDER_THUMBNAILER
  160   BLI_windows_get_executable_dir(InstallDir);
  161   GetSystemDirectory(SysDir, FILE_MAXDIR);
  162   ThumbHandlerDLL = "BlendThumb.dll";
  163   snprintf(
  164       RegCmd, MAX_PATH * 2, "%s\\regsvr32 /s \"%s\\%s\"", SysDir, InstallDir, ThumbHandlerDLL);
  165   system(RegCmd);
  166 #  endif
  167 
  168   RegCloseKey(root);
  169   printf("success (%s)\n", usr_mode ? "user" : "system");
  170   if (!background) {
  171     sprintf(MBox,
  172             "File extension registered for %s.",
  173             usr_mode ? "the current user. To register for all users, run as an administrator" :
  174                        "all users");
  175     MessageBox(0, MBox, "Blender", MB_OK | MB_ICONINFORMATION);
  176   }
  177   return true;
  178 }
  179 
  180 void BLI_windows_get_default_root_dir(char root[4])
  181 {
  182   char str[MAX_PATH + 1];
  183 
  184   /* the default drive to resolve a directory without a specified drive
  185    * should be the Windows installation drive, since this was what the OS
  186    * assumes. */
  187   if (GetWindowsDirectory(str, MAX_PATH + 1)) {
  188     root[0] = str[0];
  189     root[1] = ':';
  190     root[2] = '\\';
  191     root[3] = '\0';
  192   }
  193   else {
  194     /* if GetWindowsDirectory fails, something has probably gone wrong,
  195      * we are trying the blender install dir though */
  196     if (GetModuleFileName(NULL, str, MAX_PATH + 1)) {
  197       printf(
  198           "Error! Could not get the Windows Directory - "
  199           "Defaulting to Blender installation Dir!\n");
  200       root[0] = str[0];
  201       root[1] = ':';
  202       root[2] = '\\';
  203       root[3] = '\0';
  204     }
  205     else {
  206       DWORD tmp;
  207       int i;
  208       int rc = 0;
  209       /* now something has gone really wrong - still trying our best guess */
  210       printf(
  211           "Error! Could not get the Windows Directory - "
  212           "Defaulting to first valid drive! Path might be invalid!\n");
  213       tmp = GetLogicalDrives();
  214       for (i = 2; i < 26; i++) {
  215         if ((tmp >> i) & 1) {
  216           root[0] = 'a' + i;
  217           root[1] = ':';
  218           root[2] = '\\';
  219           root[3] = '\0';
  220           if (GetFileAttributes(root) != 0xFFFFFFFF) {
  221             rc = i;
  222             break;
  223           }
  224         }
  225       }
  226       if (0 == rc) {
  227         printf("ERROR in 'BLI_windows_get_default_root_dir': can't find a valid drive!\n");
  228         root[0] = 'C';
  229         root[1] = ':';
  230         root[2] = '\\';
  231         root[3] = '\0';
  232       }
  233     }
  234   }
  235 }
  236 
  237 #else
  238 
  239 /* intentionally empty for UNIX */
  240 
  241 #endif