git.y1.nz

SameBoy

Accurate GB/GBC emulator
download: https://git.y1.nz/archives/sameboy.tar.gz
README | Files | Log | Refs | LICENSE

SDL/windows_associations.c

      1 #ifdef _WIN32
      2 #include <windows.h>
      3 #include <shlobj.h>
      4 #include <stdbool.h>
      5 #include "windows_associations.h"
      6 
      7 static bool set_registry_string(HKEY hive, const char *folder, const char *name, const char *value)
      8 {
      9     HKEY hkey;
     10     LONG status = RegCreateKeyExA(hive, folder, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL);
     11     if (status != ERROR_SUCCESS || hkey == NULL) {
     12         return false;
     13     }
     14     status = RegSetValueExA(hkey, name, 0, REG_SZ, (void *)value, strlen(value) + 1);
     15     RegCloseKey(hkey);
     16     return status == ERROR_SUCCESS;
     17 }
     18 
     19 static bool delete_registry_key(HKEY hive, const char *folder, const char *name)
     20 {
     21     HKEY hkey;
     22     LONG status = RegCreateKeyExA(hive, folder, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL);
     23     if (status != ERROR_SUCCESS || hkey == NULL) {
     24         return false;
     25     }
     26     status = RegDeleteTreeA(hkey, name);
     27     RegCloseKey(hkey);
     28     return status == ERROR_SUCCESS;
     29 }
     30 
     31 static bool set_registry_string_unicode(HKEY hive, const char *folder, const char *name, const wchar_t *value)
     32 {
     33     HKEY hkey;
     34     LONG status = RegCreateKeyExA(hive, folder, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL);
     35     if (status != ERROR_SUCCESS || hkey == NULL) {
     36         return false;
     37     }
     38     
     39     wchar_t wide_name[strlen(name) + 1];
     40     MultiByteToWideChar(CP_UTF8, 0, name, -1, wide_name, sizeof(wide_name) / sizeof(wide_name[0]));
     41     status = RegSetValueExW(hkey, wide_name, 0, REG_SZ, (void *)value, (wcslen(value) + 1) * 2);
     42     
     43     RegCloseKey(hkey);
     44     return status == ERROR_SUCCESS;
     45 }
     46 
     47 
     48 static bool associate(const char *extension, const char *class, const char *description, signed icon)
     49 {
     50     char path[128] = "Software\\Classes\\";
     51     strcat(path, extension);
     52     if (!set_registry_string(HKEY_CURRENT_USER, path, "", class)) return false;
     53     
     54     strcpy(path, "Software\\Classes\\");
     55     strcat(path, class);
     56     if (!set_registry_string(HKEY_CURRENT_USER, path, "", description)) return false;
     57     
     58     strcat(path, "\\shell\\open\\command");
     59     
     60     wchar_t exe[MAX_PATH];
     61     GetModuleFileNameW(NULL, exe, MAX_PATH);
     62     
     63     wchar_t temp[sizeof(exe) + 32];
     64     wsprintfW(temp, L"\"\%s\" \"%%1\"", exe);
     65     if (!set_registry_string_unicode(HKEY_CURRENT_USER, path, "", temp)) return false;
     66     
     67     strcpy(path, "Software\\Classes\\");
     68     strcat(path, class);
     69     strcat(path, "\\DefaultIcon");
     70     
     71     wsprintfW(temp, L"\%s,%d", exe, icon);
     72     if (!set_registry_string_unicode(HKEY_CURRENT_USER, path, "", temp)) return false;
     73     
     74     strcpy(path, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\");
     75     strcat(path, extension);
     76     delete_registry_key(HKEY_CURRENT_USER, path, "UserChoice"); // Might not exist, do not check return value
     77     
     78     return true;
     79 }
     80 
     81 bool GB_do_windows_association(void)
     82 {
     83     bool ret = true;
     84     ret &= associate(".gb",  "SameBoy.gb",  "Game Boy Game", 1);
     85     ret &= associate(".gbc", "SameBoy.gbc", "Game Boy Color Game", 2);
     86     ret &= associate(".isx", "SameBoy.isx", "Game Boy ISX File", 2);
     87     
     88     SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
     89     
     90     return ret;
     91 }
     92 #endif

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.