2 * @file sipe-win32dep.c
6 * Copyright (C) 2010 pier11 <pier11@operamail.com> (fix for REG_EXPAND_SZ)
7 * Copyright (C) 2002-2003, Herman Bloggs <hermanator12002@yahoo.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* This file is an attempt to fix purple's wpurple_read_reg_string()
25 * which doesn't read REG_EXPAND_SZ types from registry, only REG_SZ.
26 * The code is identical (v2.6.6) apart from one line.
28 * The nead is desperate to read Lotus Notes' notes.ini file location
29 * stored in REG_EXPAND_SZ type.
34 #include "sipe-win32dep.h"
35 #include "sipe-backend.h"
37 static HKEY
_reg_open_key(HKEY rootkey
, const char *subkey
, REGSAM access
) {
41 if(G_WIN32_HAVE_WIDECHAR_API()) {
42 wchar_t *wc_subkey
= g_utf8_to_utf16(subkey
, -1, NULL
,
44 rv
= RegOpenKeyExW(rootkey
, wc_subkey
, 0, access
, ®_key
);
47 char *cp_subkey
= g_locale_from_utf8(subkey
, -1, NULL
,
49 rv
= RegOpenKeyExA(rootkey
, cp_subkey
, 0, access
, ®_key
);
53 if (rv
!= ERROR_SUCCESS
) {
54 char *errmsg
= g_win32_error_message(rv
);
55 SIPE_DEBUG_ERROR("Could not open reg key '%s' subkey '%s'.\nMessage: (%ld) %s\n",
56 ((rootkey
== HKEY_LOCAL_MACHINE
) ? "HKLM" :
57 (rootkey
== HKEY_CURRENT_USER
) ? "HKCU" :
58 (rootkey
== HKEY_CLASSES_ROOT
) ? "HKCR" : "???"),
66 static gboolean
_reg_read(HKEY reg_key
, const char *valname
, LPDWORD type
, LPBYTE data
, LPDWORD data_len
) {
69 if(G_WIN32_HAVE_WIDECHAR_API()) {
70 wchar_t *wc_valname
= NULL
;
72 wc_valname
= g_utf8_to_utf16(valname
, -1, NULL
, NULL
, NULL
);
73 rv
= RegQueryValueExW(reg_key
, wc_valname
, 0, type
, data
, data_len
);
76 char *cp_valname
= NULL
;
78 cp_valname
= g_locale_from_utf8(valname
, -1, NULL
, NULL
, NULL
);
79 rv
= RegQueryValueExA(reg_key
, cp_valname
, 0, type
, data
, data_len
);
83 if (rv
!= ERROR_SUCCESS
) {
84 char *errmsg
= g_win32_error_message(rv
);
85 SIPE_DEBUG_ERROR("Could not read from reg key value '%s'.\nMessage: (%ld) %s\n",
90 return (rv
== ERROR_SUCCESS
);
93 char *wpurple_read_reg_expand_string(HKEY rootkey
, const char *subkey
, const char *valname
) {
97 HKEY reg_key
= _reg_open_key(rootkey
, subkey
, KEY_QUERY_VALUE
);
101 if(_reg_read(reg_key
, valname
, &type
, NULL
, &nbytes
) && (type
== REG_SZ
|| type
== REG_EXPAND_SZ
)) {
103 if(G_WIN32_HAVE_WIDECHAR_API())
104 data
= (LPBYTE
) g_new(wchar_t, ((nbytes
+ 1) / sizeof(wchar_t)) + 1);
106 data
= (LPBYTE
) g_malloc(nbytes
+ 1);
108 if(_reg_read(reg_key
, valname
, &type
, data
, &nbytes
)) {
109 if(G_WIN32_HAVE_WIDECHAR_API()) {
110 wchar_t *wc_temp
= (wchar_t*) data
;
111 wc_temp
[nbytes
/ sizeof(wchar_t)] = '\0';
112 result
= g_utf16_to_utf8(wc_temp
, -1,
115 char *cp_temp
= (char*) data
;
116 cp_temp
[nbytes
] = '\0';
117 result
= g_locale_to_utf8(cp_temp
, -1,
123 RegCloseKey(reg_key
);