return d - 1;
}
+
+/* This is special cased, because we must avoid using many dbus
+ functions (such as memory allocations): Those functions may in turn
+ cause verbose output and check the flag! */
+static char *
+get_verbose_setting()
+{
+ const wchar_t dir[] = L"Software\\freedesktop\\DBus";
+ const wchar_t name[] = L"Verbose";
+ HKEY root_key;
+ HKEY key_handle;
+ DWORD nbytes;
+ DWORD n1;
+ DWORD type;
+ wchar_t *result_w = NULL;
+ char *result;
+ int len;
+
+ root_key = HKEY_LOCAL_MACHINE;
+ if (RegOpenKeyExW (root_key, dir, 0, KEY_READ, &key_handle))
+ return NULL;
+
+ nbytes = 1;
+ if (RegQueryValueExW (key_handle, name, 0, NULL, NULL, &nbytes))
+ {
+ RegCloseKey (key_handle);
+ return NULL;
+ }
+ /* Round up to multiple of wchar_t, convert to number of wchar_t's, and add 1. */
+ n1 = ((nbytes + sizeof(wchar_t) - 1) / sizeof (wchar_t)) + 1;
+ result_w = malloc (n1 * sizeof (wchar_t));
+ if (!result_w)
+ {
+ RegCloseKey (key_handle);
+ return NULL;
+ }
+ if (RegQueryValueExW (key_handle, name, 0, &type, (LPBYTE) result_w, &nbytes))
+ {
+ RegCloseKey (key_handle);
+ free (result_w);
+ return NULL;
+ }
+ RegCloseKey (key_handle);
+ result_w[n1 - 1] = 0; /* Make sure it is really a string. */
+
+ /* NOTE: REG_MULTI_SZ and REG_EXPAND_SZ not supported, because they
+ are not needed in this module. */
+ if (type != REG_SZ)
+ {
+ free (result_w);
+ return NULL;
+ }
+
+ len = WideCharToMultiByte (CP_UTF8, 0, result_w, -1, NULL, 0, NULL, NULL);
+ if (len < 0)
+ {
+ free (result_w);
+ return NULL;
+ }
+
+ result = malloc (len + 1);
+ if (!result)
+ {
+ free (result_w);
+ return NULL;
+ }
+
+ len = WideCharToMultiByte (CP_UTF8, 0, result_w, -1, result, len, NULL, NULL);
+ free (result_w);
+ if (len < 0)
+ {
+ free (result);
+ return NULL;
+ }
+ return result;
+}
+
+
/* Return a string from the W32 Registry or NULL in case of error.
Caller must release the return value. A NULL for root is an alias
for HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE in turn. */
}
if (! strcmp (name, "DBUS_VERBOSE"))
- return past_result = find_env_in_registry ("Verbose");
+ return past_result = get_verbose_setting ();
else if (! strcmp (name, "HOMEPATH"))
return past_result = find_my_documents_folder ();
else if (! strcmp (name, "DBUS_DATADIR"))
/** Gets our SID
- * @param points to sid buffer, need to be freed with LocalFree()
+ * @param sid points to sid buffer, need to be freed with LocalFree()
* @returns process sid
*/
dbus_bool_t
/* If err is ERROR_MORE_DATA, there probably was a race condition.
We can punt this to the caller just as well. */
if (err)
- return err;
+ {
+ free (data);
+ return err;
+ }
/* NOTE: REG_MULTI_SZ and REG_EXPAND_SZ not supported, because they
are not needed in this module. */
/* This is valid since we allocated one more above. */
data[data_len] = '\0';
data[data_len + 1] = '\0';
-
+
+ /* The cast is valid because malloc guarantees alignment of
+ basic types. */
data_c = _dbus_win_utf16_to_utf8 ((wchar_t*) data, NULL);
if (!data_c)
- return GetLastError();
+ {
+ free (data);
+ return GetLastError();
+ }
data_c_len = strlen (data_c) + 1;
_dbus_assert (data_c_len <= data_len + sizeof (wchar_t));
else
memcpy (lpData, data, data_len);
}
+ free (data);
*lpcbData = data_len;
return err;
}
DWORD
GetModuleFileNameA (HINSTANCE hModule, LPSTR lpFilename, DWORD nSize)
{
- wchar_t filename_w[MAX_PATH];
+ wchar_t *filename_w;
char *filename_c;
DWORD len;
- _dbus_assert (MAX_PATH >= nSize);
+ if (nSize == 0)
+ {
+ /* Windows XP/2000. */
+ SetLastError (0);
+ return 0;
+ }
+
+ filename_w = malloc (sizeof (wchar_t) * nSize);
+ if (! filename_w)
+ return 0;
len = GetModuleFileNameW (hModule, filename_w, nSize);
if (len == 0)
- return 0;
+ {
+ /* Note: If we fail with ERROR_INSUFFICIENT_BUFFER, this is still
+ (approximately) correct. */
+ free (filename_w);
+ return 0;
+ }
filename_w[nSize - 1] = '\0';
filename_c = _dbus_win_utf16_to_utf8 (filename_w, NULL);
+ free (filename_w);
if (! filename_c)
return 0;
lpszPath[MAX_PATH - 1] = '\0';
return result;
}
+
+
+void
+OutputDebugStringA (LPCSTR lpOutputString)
+{
+ wchar_t *str;
+ HANDLE result;
+ int err;
+
+ str = _dbus_win_utf8_to_utf16 (lpOutputString, NULL);
+ if (!str)
+ return;
+
+ OutputDebugStringW (str);
+
+ err = GetLastError ();
+ dbus_free (str);
+ SetLastError (err);
+}