1 /* w32_reg.c - Posix emulation layer for Sylpheed (Claws)
3 * This file is part of w32lib.
5 * w32lib is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * w32lib is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * For more information and a list of changes, see w32lib.h
25 static HKEY get_root_key_from_str(char *parent)
28 if (!parent || !strlen(parent))
29 rootKey = HKEY_CURRENT_USER ;
30 else if (!strcmp(parent, "HKCR") || !strcmp(parent,"HKEY_CLASSES_ROOT"))
31 rootKey = HKEY_CLASSES_ROOT ;
32 else if (!strcmp(parent, "HKCU") || !strcmp(parent,"HKEY_CURRENT_USER"))
33 rootKey = HKEY_CURRENT_USER ;
34 else if (!strcmp(parent, "HKLM") || !strcmp(parent,"HKEY_LOCAL_MACHINE"))
35 rootKey = HKEY_LOCAL_MACHINE ;
36 else if (!strcmp(parent, "HKU") || !strcmp(parent,"HKEY_USERS"))
37 rootKey = HKEY_USERS ;
38 else if (!strcmp(parent, "HKCC") || !strcmp(parent,"HKEY_CURRENT_CONFIG"))
39 rootKey = HKEY_CURRENT_CONFIG ;
44 int write_w32_registry_string( char *parent, char *section, char *value, char *data )
49 rootKey = get_root_key_from_str(parent);
50 ret = RegCreateKeyEx(rootKey, section, 0, NULL,
51 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
52 if (ret != ERROR_SUCCESS) {
53 debug_print("can't write key %s\\%s: %d\n", parent, section, ret);
56 ret = RegSetValueEx(hKey, value, 0, REG_SZ, (LPVOID)data, strlen(data)+1);
57 if (ret != ERROR_SUCCESS) {
59 debug_print("can't write key %s\\%s: %d\n", parent, section, ret);
66 int write_w32_registry_dword( char *parent, char *section, char *value, int data )
71 rootKey = get_root_key_from_str(parent);
72 ret = RegCreateKeyEx(rootKey, section, 0, NULL,
73 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
74 if (ret != ERROR_SUCCESS) {
75 debug_print("can't write key %s\\%s: %d\n", parent, section, ret);
78 ret = RegSetValueEx(hKey, value, 0, REG_DWORD, (LPBYTE)&data, sizeof(data));
79 if (ret != ERROR_SUCCESS) {
81 debug_print("can't write key %s\\%s: %d\n", parent, section, ret);
88 char *read_w32_registry_string( char *parent, char *section, char *key )
95 DWORD bufsiz = sizeof( buf );
97 rootKey = get_root_key_from_str(parent);
103 ret = RegOpenKeyEx( rootKey, section, 0, KEY_READ, &hKey );
104 if ( ERROR_SUCCESS == ret ){
105 ret = RegQueryValueEx( hKey, key, 0, NULL,
106 (LPBYTE)buf, &bufsiz );
107 if ( ERROR_SUCCESS == ret ){
115 char *get_content_type_from_registry_with_ext( char *ext )
119 char buf[ MAX_PATH ];
121 char *section, *key, *value;
126 // parent : HKEY_CLASSES_ROOT
128 parent = HKEY_CLASSES_ROOT;
129 section = malloc ( 1 + strlen (ext) + 1);
133 strcpy (section+1, ext);
137 ret = RegOpenKeyEx( parent, section, 0, KEY_READ, &hKey );
138 if ( ERROR_SUCCESS != ret ) {
139 // If section is not found...
144 // key : "Content Type"
145 key = "Content Type";
146 bufsiz = sizeof( buf );
147 ret = RegQueryValueEx( hKey, key, 0, NULL, (LPBYTE)buf, &bufsiz );
148 if ( ERROR_SUCCESS == ret ) {
149 // If value is found!
151 value = strdup( buf );
156 bufsiz = sizeof( buf );
157 ret = RegQueryValueEx( hKey, key, 0, NULL, (LPBYTE)buf, &bufsiz );
158 if ( ERROR_SUCCESS != ret ) {
166 section = strdup( buf );