Fix error creation and warning
[claws.git] / src / common / w32_reg.c
blob37ac1d984e90e44c6a558ae856c70b746e2137f8
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2021 the Claws Mail team and Hiroyuki Yamamoto
5 * This program 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 * This program 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/>.
19 #include "config.h"
21 #include "w32_reg.h"
22 #include "utils.h"
24 gboolean reg_set_value(HKEY root,
25 const gchar *subkey,
26 const gchar *value,
27 DWORD type,
28 const BYTE *data,
29 DWORD data_size)
31 DWORD ret;
32 HKEY key;
33 gchar *tmp;
35 ret = RegCreateKeyEx(root, subkey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &key, NULL);
36 if (ret != ERROR_SUCCESS) {
37 tmp = g_win32_error_message(ret);
38 debug_print("RegCreateKeyEx %p \"%s\" had error: \"%s\"\n", root, subkey, tmp);
39 g_free(tmp);
40 return FALSE;
43 ret = RegSetValueEx(key, value, 0, type, data, data_size);
44 if (ret != ERROR_SUCCESS) {
45 tmp = g_win32_error_message(ret);
46 debug_print("RegSetValueEx %p \"%s\" had error: \"%s\"\n", root, subkey, tmp);
47 g_free(tmp);
50 RegCloseKey(key);
51 return (ret == ERROR_SUCCESS);
54 gboolean write_w32_registry_string(HKEY root,
55 const gchar *subkey,
56 const gchar *value,
57 const gchar *data)
59 return reg_set_value(root, subkey, value, REG_SZ, (BYTE *)data, strlen(data) + 1);
62 gboolean write_w32_registry_dword(HKEY root,
63 const gchar *subkey,
64 const gchar *value,
65 DWORD data)
67 return reg_set_value(root, subkey, value, REG_DWORD, (BYTE *)&data, sizeof(DWORD));
70 gchar *read_w32_registry_string(HKEY root, const gchar *subkey, const gchar *value)
72 HKEY hkey;
73 DWORD ret;
74 DWORD type;
75 BYTE *data;
76 DWORD data_size;
77 gchar *tmp;
79 if (subkey == NULL)
80 return NULL;
82 ret = RegOpenKeyEx(root, subkey, 0, KEY_READ, &hkey);
83 if (ret != ERROR_SUCCESS) {
84 tmp = g_win32_error_message(ret);
85 debug_print("RegOpenKeyEx %p \"%s\" had error: \"%s\"\n", root, subkey, tmp);
86 g_free(tmp);
87 return NULL;
90 // Get the needed buffer size
91 ret = RegQueryValueEx(hkey, value, 0, &type, NULL, &data_size);
92 if (ret != ERROR_SUCCESS) {
93 tmp = g_win32_error_message(ret);
94 debug_print("RegQueryValueEx %p \"%s\" \"%s\" had error: \"%s\" when getting buffer size\n",
95 root, subkey, value, tmp);
96 RegCloseKey(hkey);
97 g_free(tmp);
98 return NULL;
99 } else if (type != REG_SZ) {
100 debug_print("RegQueryValueEx %p \"%s\" \"%s\" returned type %lu instead of REG_SZ\n",
101 root, subkey, value, type);
102 RegCloseKey(hkey);
103 return NULL;
104 } else if (data_size == 0) {
105 debug_print("RegQueryValueEx %p \"%s\" \"%s\" returned data size 0\n",
106 root, subkey, value);
107 RegCloseKey(hkey);
108 return NULL;
111 // The raw value is not necessarily NUL-terminated
112 data = g_malloc(data_size + 1);
114 ret = RegQueryValueEx(hkey, value, 0, NULL, data, &data_size);
115 if (ret == ERROR_SUCCESS) {
116 data[data_size] = '\0';
117 } else {
118 tmp = g_win32_error_message(ret);
119 debug_print("RegQueryValueEx %p \"%s\" \"%s\" had error: \"%s\"\n",
120 root, subkey, value, tmp);
121 RegCloseKey(hkey);
122 g_free(data);
123 g_free(tmp);
124 return NULL;
127 RegCloseKey(hkey);
128 return (gchar *)data;