30#include "dbus-internals.h"
31#include "dbus-sysdeps.h"
32#include "dbus-sysdeps-win.h"
38#define CSIDL_PERSONAL 5
44stpcpy (
char *dest,
const char *src)
63 const wchar_t dir[] = L
"Software\\freedesktop\\DBus";
64 const wchar_t name[] = L
"Verbose";
70 wchar_t *result_w =
NULL;
74 root_key = HKEY_LOCAL_MACHINE;
75 if (RegOpenKeyExW (root_key, dir, 0, KEY_READ, &key_handle))
79 if (RegQueryValueExW (key_handle, name, 0,
NULL,
NULL, &nbytes))
81 RegCloseKey (key_handle);
85 n1 = ((nbytes +
sizeof(wchar_t) - 1) /
sizeof (wchar_t)) + 1;
86 result_w = malloc (n1 *
sizeof (
wchar_t));
89 RegCloseKey (key_handle);
92 if (RegQueryValueExW (key_handle, name, 0, &type, (LPBYTE) result_w, &nbytes))
94 RegCloseKey (key_handle);
98 RegCloseKey (key_handle);
109 len = WideCharToMultiByte (CP_UTF8, 0, result_w, -1,
NULL, 0,
NULL,
NULL);
116 result = malloc (len + 1);
123 len = WideCharToMultiByte (CP_UTF8, 0, result_w, -1, result, len,
NULL,
NULL);
138read_w32_registry_string (
const char *root,
const char *dir,
const char *name)
140 HKEY root_key, key_handle;
141 DWORD n1, nbytes, type;
145 root_key = HKEY_CURRENT_USER;
146 else if ( !strcmp( root,
"HKEY_CLASSES_ROOT" ) )
147 root_key = HKEY_CLASSES_ROOT;
148 else if ( !strcmp( root,
"HKEY_CURRENT_USER" ) )
149 root_key = HKEY_CURRENT_USER;
150 else if ( !strcmp( root,
"HKEY_LOCAL_MACHINE" ) )
151 root_key = HKEY_LOCAL_MACHINE;
152 else if ( !strcmp( root,
"HKEY_USERS" ) )
153 root_key = HKEY_USERS;
157 if (RegOpenKeyExA (root_key, dir, 0, KEY_READ, &key_handle))
162 if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle))
167 if (RegQueryValueExA (key_handle, name, 0,
NULL,
NULL, &nbytes))
172 RegCloseKey (key_handle);
173 if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle))
175 if (RegQueryValueExA (key_handle, name, 0,
NULL,
NULL, &nbytes))
179 result = malloc (n1);
182 if (RegQueryValueExA (key_handle, name, 0, &type, result, &n1))
191 RegCloseKey (key_handle);
199 return read_w32_registry_string (
"HKEY_LOCAL_MACHINE",
200 "Software\\freedesktop\\DBus",
201 "Install Directory");
206find_env_in_registry (
const char *name)
208 return read_w32_registry_string (
"HKEY_LOCAL_MACHINE",
209 "Software\\freedesktop\\DBus",
215find_program_in_inst_dir (
const char *name)
220 tmp = find_inst_dir ();
224 result = malloc (strlen (tmp) + 5 + strlen (name) + 1);
231 strcpy (stpcpy (stpcpy (result, tmp),
"\\bin\\"), name);
239find_inst_subdir (
const char *name)
244 tmp = find_inst_dir ();
248 result = malloc (strlen (tmp) + 1 + strlen (name) + 1);
255 strcpy (stpcpy (stpcpy (result, tmp),
"\\"), name);
263find_my_documents_folder ()
266 char dir[MAX_PATH + 1];
271 SHGetSpecialFolderPathA (0, dir, CSIDL_PERSONAL, 0);
275 result = malloc (strlen (dir) + 1);
278 strcpy (result, dir);
285char *environ[MAX_ENV + 1];
288getenv (
const char *name)
290 static char *past_result;
300 if (! strcmp (name,
"DBUS_VERBOSE"))
301 return past_result = get_verbose_setting ();
302 else if (! strcmp (name,
"HOMEPATH"))
303 return past_result = find_my_documents_folder ();
304 else if (! strcmp (name,
"DBUS_DATADIR"))
305 return past_result = find_inst_subdir (
"share");
307 for (envp = environ; *envp != 0; envp++)
309 const char *varp = name;
313 while (*varp == *ep && *varp !=
'\0')
319 if (*varp ==
'\0' && *ep ==
'=')
332 for (envp = environ; *envp != 0; envp++)
338 while (*varp == *ep && *varp !=
'\0')
346 if (*varp == *ep && *varp ==
'\0')
355 idx = envp - environ;
358 _dbus_win_set_errno (ENOMEM);
370 return GetTickCount ();
383GetSystemTimeAsFileTime (LPFILETIME ftp)
387 SystemTimeToFileTime (&st, ftp);
392_mbsrchr (
const unsigned char* str,
unsigned int ch)
395 return strrchr (str, ch);
399HANDLE OpenFileMappingA(DWORD dwDesiredAccess,
406 if (dwDesiredAccess & FILE_MAP_READ)
407 flProtect |= PAGE_READONLY;
409 if (dwDesiredAccess & FILE_MAP_WRITE)
410 flProtect |= PAGE_READWRITE;
413 hMapping = CreateFileMappingA(INVALID_HANDLE_VALUE,
414 NULL, flProtect, 0, 0, lpName);
415 if (hMapping != INVALID_HANDLE_VALUE)
419 if (GetLastError () != ERROR_ALREADY_EXISTS)
421 CloseHandle(hMapping);
422 hMapping = INVALID_HANDLE_VALUE;
430MoveFileExA (LPCSTR lpExistingFileName, LPCSTR lpNewFileName, DWORD dwFlags)
436 BOOL result = DeleteFileA (lpNewFileName);
440 return MoveFileA (lpExistingFileName, lpNewFileName);
445SetHandleInformation (HANDLE hObject, DWORD dwMask, DWORD dwFlags)
447 _dbus_assert (dwMask == (HANDLE_FLAG_INHERIT | HANDLE_FLAG_PROTECT_FROM_CLOSE));
457SearchPathA (LPCSTR lpPath, LPCSTR lpFileName, LPCSTR lpExtension,
458 DWORD nBufferLength, LPSTR lpBuffer, LPSTR* lpFilePart)
467 filename = find_program_in_inst_dir (lpFileName);
470 SetLastError (ERROR_FILE_NOT_FOUND);
474 filename_len = strlen (filename) + 1;
475 if (filename_len > nBufferLength)
481 strcpy (lpBuffer, filename);
484 filepart = _mbsrchr (lpBuffer,
'\\');
487 *lpFilePart = filepart;
489 return filename_len - 1;
498_dbus_getsid(
char **sid)
501 static const char asid[] =
"S-1-5-21-515967899-920026266-1708537768-1000";
502 char *buf = LocalAlloc (LMEM_FIXED,
sizeof (asid));
505 _dbus_win_warn_win_error (
"LocalAlloc failed", GetLastError ());
509 memcpy (buf, asid,
sizeof (asid));
516LookupAccountNameW (LPCWSTR lpSystemName, LPCWSTR lpAccountName, PSID Sid, PDWORD cbSid,
517 LPWSTR ReferencedDomainName, PDWORD cchReferencedDomainName, PSID_NAME_USE peUse)
525IsValidSid (PSID psid)
533CreateFileA (LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwSharedMode,
534 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
535 DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes,
536 HANDLE hTemplateFile)
542 filename = _dbus_win_utf8_to_utf16 (lpFileName,
NULL);
544 return INVALID_HANDLE_VALUE;
546 result = CreateFileW (filename, dwDesiredAccess, dwSharedMode,
547 lpSecurityAttributes, dwCreationDisposition,
548 dwFlagsAndAttributes, hTemplateFile);
550 err = GetLastError ();
558DeleteFileA (LPCSTR lpFileName)
564 filename = _dbus_win_utf8_to_utf16 (lpFileName,
NULL);
568 result = DeleteFileW (filename);
570 err = GetLastError ();
578MoveFileA (LPCSTR lpExistingFileName, LPCSTR lpNewFileName)
580 wchar_t *existing_filename;
581 wchar_t *new_filename;
585 existing_filename = _dbus_win_utf8_to_utf16 (lpExistingFileName,
NULL);
586 if (! existing_filename)
589 new_filename = _dbus_win_utf8_to_utf16 (lpNewFileName,
NULL);
596 result = MoveFileW (existing_filename, new_filename);
598 err = GetLastError ();
607GetFileAttributesA(LPCSTR lpFileName)
613 filename = _dbus_win_utf8_to_utf16 (lpFileName,
NULL);
615 return INVALID_FILE_ATTRIBUTES;
617 result = GetFileAttributesW (filename);
619 err = GetLastError ();
627GetFileAttributesExA (LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
628 PVOID lpFileInformation)
634 filename = _dbus_win_utf8_to_utf16 (lpFileName,
NULL);
636 return INVALID_FILE_ATTRIBUTES;
638 result = GetFileAttributesExW (filename, fInfoLevelId, lpFileInformation);
640 err = GetLastError ();
648CreateFileMappingA (HANDLE hFile, LPSECURITY_ATTRIBUTES lpAttributes,
649 DWORD flProtect, DWORD dwMaximumSizeHigh,
650 DWORD dwMaximumSizeLow, LPCSTR lpName)
658 name = _dbus_win_utf8_to_utf16 (lpName,
NULL);
660 return INVALID_HANDLE_VALUE;
665 result = CreateFileMappingW (hFile, lpAttributes, flProtect,
666 dwMaximumSizeHigh, dwMaximumSizeLow,
669 err = GetLastError ();
677CreateDirectoryA (LPCSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
683 pathname = _dbus_win_utf8_to_utf16 (lpPathName,
NULL);
687 result = CreateDirectoryW (pathname, lpSecurityAttributes);
689 err = GetLastError ();
697RemoveDirectoryA (LPCSTR lpPathName)
703 pathname = _dbus_win_utf8_to_utf16 (lpPathName,
NULL);
707 result = RemoveDirectoryW (pathname);
709 err = GetLastError ();
717convert_find_data (LPWIN32_FIND_DATAW fdw, LPWIN32_FIND_DATAA fda)
722 fda->dwFileAttributes = fdw->dwFileAttributes;
723 fda->ftCreationTime = fdw->ftCreationTime;
724 fda->ftLastAccessTime = fdw->ftLastAccessTime;
725 fda->ftLastWriteTime = fdw->ftLastWriteTime;
726 fda->nFileSizeHigh = fdw->nFileSizeHigh;
727 fda->nFileSizeLow = fdw->nFileSizeLow;
729 filename = _dbus_win_utf16_to_utf8 (fdw->cFileName,
NULL);
733 len =
sizeof (fda->cFileName);
734 strncpy (fda->cFileName, filename, len);
735 fda->cFileName[len - 1] =
'\0';
742FindFirstFileA (LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData)
745 WIN32_FIND_DATAW find_file_data;
749 pathname = _dbus_win_utf8_to_utf16 (lpFileName,
NULL);
751 return INVALID_HANDLE_VALUE;
753 result = FindFirstFileW (pathname, &find_file_data);
754 if (result != INVALID_HANDLE_VALUE)
756 BOOL res = convert_find_data (&find_file_data, lpFindFileData);
759 err = GetLastError ();
762 result = INVALID_HANDLE_VALUE;
766 err = GetLastError ();
774FindNextFileA (HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData)
776 WIN32_FIND_DATAW find_file_data;
780 result = FindNextFileW (hFindFile, &find_file_data);
782 result = convert_find_data (&find_file_data, lpFindFileData);
789CreateMutexA (LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner,
798 name = _dbus_win_utf8_to_utf16 (lpName,
NULL);
800 return INVALID_HANDLE_VALUE;
805 result = CreateMutexW (lpMutexAttributes, bInitialOwner, name);
807 err = GetLastError ();
815CreateProcessA (LPCSTR pszImageName, LPSTR pszCmdLine,
816 LPSECURITY_ATTRIBUTES psaProcess,
817 LPSECURITY_ATTRIBUTES psaThread, BOOL fInheritHandles,
818 DWORD fdwCreate, PVOID pvEnvironment, LPCSTR pszCurDir,
819 LPSTARTUPINFOA psiStartInfo,
820 LPPROCESS_INFORMATION pProcInfo)
822 wchar_t *image_name =
NULL;
823 wchar_t *cmd_line =
NULL;
836 image_name = _dbus_win_utf8_to_utf16 (pszImageName,
NULL);
842 cmd_line = _dbus_win_utf8_to_utf16 (pszCmdLine,
NULL);
851 result = CreateProcessW (image_name, cmd_line,
NULL,
NULL,
FALSE,
854 err = GetLastError ();
863RegOpenKeyExA (HKEY hKey, LPCSTR lpSubKey, DWORD ulOptions,
864 REGSAM samDesired, PHKEY phkResult)
872 subkey = _dbus_win_utf8_to_utf16 (lpSubKey,
NULL);
879 result = RegOpenKeyEx (hKey, subkey, ulOptions, samDesired, phkResult);
881 err = GetLastError ();
889RegQueryValueExA (HKEY hKey, LPCSTR lpValueName, LPDWORD lpReserved,
890 LPDWORD lpType, LPBYTE lpData, LPDWORD lpcbData)
900 name = _dbus_win_utf8_to_utf16 (lpValueName,
NULL);
902 return GetLastError ();
908 err = RegQueryValueExW (hKey, name, lpReserved, lpType,
NULL, &data_len);
909 if (err || !lpcbData)
915 data = malloc (data_len +
sizeof (
wchar_t));
919 return ERROR_NOT_ENOUGH_MEMORY;
922 err = RegQueryValueExW (hKey, name, lpReserved, &type, data, &data_len);
942 data[data_len] =
'\0';
943 data[data_len + 1] =
'\0';
947 data_c = _dbus_win_utf16_to_utf8 ((
wchar_t*) data,
NULL);
951 return GetLastError();
954 data_c_len = strlen (data_c) + 1;
955 _dbus_assert (data_c_len <= data_len +
sizeof (
wchar_t));
956 memcpy (data, data_c, data_c_len);
957 data_len = data_c_len;
964 if (data_len > *lpcbData)
965 err = ERROR_MORE_DATA;
967 memcpy (lpData, data, data_len);
970 *lpcbData = data_len;
976FormatMessageA (DWORD dwFlags, PCVOID lpSource, DWORD dwMessageId,
977 DWORD dwLanguageId, LPSTR lpBuffer, DWORD nSize,
980 LPWSTR buffer_w =
NULL;
984 DWORD buffer_new_len;
987 len = FormatMessageW (dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER,
988 lpSource, dwMessageId, dwLanguageId,
989 (LPWSTR) &buffer_w, 0, Arguments);
993 buffer_c = _dbus_win_utf16_to_utf8 (buffer_w,
NULL);
996 LocalFree (buffer_w);
1000 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER)
1003 buffer_new = (
char *) buffer_w;
1004 buffer_new_len =
sizeof (wchar_t) * (len + 1);
1005 buffer_w_free =
FALSE;
1007 memcpy (lpBuffer, &buffer_new,
sizeof (buffer_new));
1011 buffer_new = lpBuffer;
1012 buffer_new_len = nSize;
1013 buffer_w_free =
TRUE;
1016 strncpy (buffer_new, buffer_c, buffer_new_len);
1018 buffer_new[buffer_new_len - 1] =
'\0';
1020 LocalFree (buffer_w);
1024 return strlen (buffer_new);
1029GetModuleFileNameA (HINSTANCE hModule, LPSTR lpFilename, DWORD nSize)
1031 wchar_t *filename_w;
1042 filename_w = malloc (
sizeof (
wchar_t) * nSize);
1046 len = GetModuleFileNameW (hModule, filename_w, nSize);
1055 filename_w[nSize - 1] =
'\0';
1056 filename_c = _dbus_win_utf16_to_utf8 (filename_w,
NULL);
1061 strncpy (lpFilename, filename_c, nSize);
1063 lpFilename[nSize - 1] =
'\0';
1066 return strlen (lpFilename);
1071GetTempPathA (DWORD nBufferLength, LPSTR lpBuffer)
1076 len = GetTempPathW (0, dummy);
1092 buffer_w = malloc (
sizeof (
wchar_t) * len);
1096 len_w = GetTempPathW (len, buffer_w);
1098 if (len_w == 0 || len_w >= len)
1105 buffer_w[len_w] =
'\0';
1107 buffer_c = _dbus_win_utf16_to_utf8 (buffer_w,
NULL);
1114 len_c = strlen (buffer_c) + 1;
1115 if (len_c > nBufferLength)
1118 strcpy (lpBuffer, buffer_c);
1126SHGetSpecialFolderPathA (HWND hwndOwner, LPSTR lpszPath,
int nFolder,
1129 wchar_t path[MAX_PATH];
1133 path[0] = (wchar_t) 0;
1134 result = SHGetSpecialFolderPathW (hwndOwner, path, nFolder, fCreate);
1137 path[MAX_PATH - 1] = (wchar_t) 0;
1138 path_c = _dbus_win_utf16_to_utf8 (path,
NULL);
1142 strncpy (lpszPath, path_c, MAX_PATH);
1144 lpszPath[MAX_PATH - 1] =
'\0';
1150OutputDebugStringA (LPCSTR lpOutputString)
1156 str = _dbus_win_utf8_to_utf16 (lpOutputString,
NULL);
1160 OutputDebugStringW (str);
1162 err = GetLastError ();
dbus_bool_t _dbus_file_exists(const char *file)
File interface.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.