32#include "dbus-internals.h"
33#include "dbus-sysdeps.h"
34#include "dbus-sysdeps-win.h"
40#define CSIDL_PERSONAL 5
46stpcpy (
char *dest,
const char *src)
65 const wchar_t dir[] = L
"Software\\freedesktop\\DBus";
66 const wchar_t name[] = L
"Verbose";
72 wchar_t *result_w =
NULL;
76 root_key = HKEY_LOCAL_MACHINE;
77 if (RegOpenKeyExW (root_key, dir, 0, KEY_READ, &key_handle))
81 if (RegQueryValueExW (key_handle, name, 0,
NULL,
NULL, &nbytes))
83 RegCloseKey (key_handle);
87 n1 = ((nbytes +
sizeof(wchar_t) - 1) /
sizeof (wchar_t)) + 1;
88 result_w = malloc (n1 *
sizeof (
wchar_t));
91 RegCloseKey (key_handle);
94 if (RegQueryValueExW (key_handle, name, 0, &type, (LPBYTE) result_w, &nbytes))
96 RegCloseKey (key_handle);
100 RegCloseKey (key_handle);
101 result_w[n1 - 1] = 0;
111 len = WideCharToMultiByte (CP_UTF8, 0, result_w, -1,
NULL, 0,
NULL,
NULL);
118 result = malloc (len + 1);
125 len = WideCharToMultiByte (CP_UTF8, 0, result_w, -1, result, len,
NULL,
NULL);
140read_w32_registry_string (
const char *root,
const char *dir,
const char *name)
142 HKEY root_key, key_handle;
143 DWORD n1, nbytes, type;
147 root_key = HKEY_CURRENT_USER;
148 else if ( !strcmp( root,
"HKEY_CLASSES_ROOT" ) )
149 root_key = HKEY_CLASSES_ROOT;
150 else if ( !strcmp( root,
"HKEY_CURRENT_USER" ) )
151 root_key = HKEY_CURRENT_USER;
152 else if ( !strcmp( root,
"HKEY_LOCAL_MACHINE" ) )
153 root_key = HKEY_LOCAL_MACHINE;
154 else if ( !strcmp( root,
"HKEY_USERS" ) )
155 root_key = HKEY_USERS;
159 if (RegOpenKeyExA (root_key, dir, 0, KEY_READ, &key_handle))
164 if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle))
169 if (RegQueryValueExA (key_handle, name, 0,
NULL,
NULL, &nbytes))
174 RegCloseKey (key_handle);
175 if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle))
177 if (RegQueryValueExA (key_handle, name, 0,
NULL,
NULL, &nbytes))
181 result = malloc (n1);
184 if (RegQueryValueExA (key_handle, name, 0, &type, result, &n1))
193 RegCloseKey (key_handle);
201 return read_w32_registry_string (
"HKEY_LOCAL_MACHINE",
202 "Software\\freedesktop\\DBus",
203 "Install Directory");
208find_env_in_registry (
const char *name)
210 return read_w32_registry_string (
"HKEY_LOCAL_MACHINE",
211 "Software\\freedesktop\\DBus",
217find_program_in_inst_dir (
const char *name)
222 tmp = find_inst_dir ();
226 result = malloc (strlen (tmp) + 5 + strlen (name) + 1);
233 strcpy (stpcpy (stpcpy (result, tmp),
"\\bin\\"), name);
241find_inst_subdir (
const char *name)
246 tmp = find_inst_dir ();
250 result = malloc (strlen (tmp) + 1 + strlen (name) + 1);
257 strcpy (stpcpy (stpcpy (result, tmp),
"\\"), name);
265find_my_documents_folder ()
268 char dir[MAX_PATH + 1];
273 SHGetSpecialFolderPathA (0, dir, CSIDL_PERSONAL, 0);
277 result = malloc (strlen (dir) + 1);
280 strcpy (result, dir);
287char *environ[MAX_ENV + 1];
290getenv (
const char *name)
292 static char *past_result;
302 if (! strcmp (name,
"DBUS_VERBOSE"))
303 return past_result = get_verbose_setting ();
304 else if (! strcmp (name,
"HOMEPATH"))
305 return past_result = find_my_documents_folder ();
306 else if (! strcmp (name,
"DBUS_DATADIR"))
307 return past_result = find_inst_subdir (
"share");
309 for (envp = environ; *envp != 0; envp++)
311 const char *varp = name;
315 while (*varp == *ep && *varp !=
'\0')
321 if (*varp ==
'\0' && *ep ==
'=')
334 for (envp = environ; *envp != 0; envp++)
340 while (*varp == *ep && *varp !=
'\0')
348 if (*varp == *ep && *varp ==
'\0')
357 idx = envp - environ;
360 _dbus_win_set_errno (ENOMEM);
372 return GetTickCount ();
385GetSystemTimeAsFileTime (LPFILETIME ftp)
389 SystemTimeToFileTime (&st, ftp);
394_mbsrchr (
const unsigned char* str,
unsigned int ch)
397 return strrchr (str, ch);
401HANDLE OpenFileMappingA(DWORD dwDesiredAccess,
408 if (dwDesiredAccess & FILE_MAP_READ)
409 flProtect |= PAGE_READONLY;
411 if (dwDesiredAccess & FILE_MAP_WRITE)
412 flProtect |= PAGE_READWRITE;
415 hMapping = CreateFileMappingA(INVALID_HANDLE_VALUE,
416 NULL, flProtect, 0, 0, lpName);
417 if (hMapping != INVALID_HANDLE_VALUE)
421 if (GetLastError () != ERROR_ALREADY_EXISTS)
423 CloseHandle(hMapping);
424 hMapping = INVALID_HANDLE_VALUE;
432MoveFileExA (LPCSTR lpExistingFileName, LPCSTR lpNewFileName, DWORD dwFlags)
438 BOOL result = DeleteFileA (lpNewFileName);
442 return MoveFileA (lpExistingFileName, lpNewFileName);
447SetHandleInformation (HANDLE hObject, DWORD dwMask, DWORD dwFlags)
449 _dbus_assert (dwMask == (HANDLE_FLAG_INHERIT | HANDLE_FLAG_PROTECT_FROM_CLOSE));
459SearchPathA (LPCSTR lpPath, LPCSTR lpFileName, LPCSTR lpExtension,
460 DWORD nBufferLength, LPSTR lpBuffer, LPSTR* lpFilePart)
469 filename = find_program_in_inst_dir (lpFileName);
472 SetLastError (ERROR_FILE_NOT_FOUND);
476 filename_len = strlen (filename) + 1;
477 if (filename_len > nBufferLength)
483 strcpy (lpBuffer, filename);
486 filepart = _mbsrchr (lpBuffer,
'\\');
489 *lpFilePart = filepart;
491 return filename_len - 1;
500_dbus_getsid(
char **sid)
503 static const char asid[] =
"S-1-5-21-515967899-920026266-1708537768-1000";
504 char *buf = LocalAlloc (LMEM_FIXED,
sizeof (asid));
507 _dbus_win_warn_win_error (
"LocalAlloc failed", GetLastError ());
511 memcpy (buf, asid,
sizeof (asid));
518LookupAccountNameW (LPCWSTR lpSystemName, LPCWSTR lpAccountName, PSID Sid, PDWORD cbSid,
519 LPWSTR ReferencedDomainName, PDWORD cchReferencedDomainName, PSID_NAME_USE peUse)
527IsValidSid (PSID psid)
535CreateFileA (LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwSharedMode,
536 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
537 DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes,
538 HANDLE hTemplateFile)
544 filename = _dbus_win_utf8_to_utf16 (lpFileName,
NULL);
546 return INVALID_HANDLE_VALUE;
548 result = CreateFileW (filename, dwDesiredAccess, dwSharedMode,
549 lpSecurityAttributes, dwCreationDisposition,
550 dwFlagsAndAttributes, hTemplateFile);
552 err = GetLastError ();
560DeleteFileA (LPCSTR lpFileName)
566 filename = _dbus_win_utf8_to_utf16 (lpFileName,
NULL);
570 result = DeleteFileW (filename);
572 err = GetLastError ();
580MoveFileA (LPCSTR lpExistingFileName, LPCSTR lpNewFileName)
582 wchar_t *existing_filename;
583 wchar_t *new_filename;
587 existing_filename = _dbus_win_utf8_to_utf16 (lpExistingFileName,
NULL);
588 if (! existing_filename)
591 new_filename = _dbus_win_utf8_to_utf16 (lpNewFileName,
NULL);
598 result = MoveFileW (existing_filename, new_filename);
600 err = GetLastError ();
609GetFileAttributesA(LPCSTR lpFileName)
615 filename = _dbus_win_utf8_to_utf16 (lpFileName,
NULL);
617 return INVALID_FILE_ATTRIBUTES;
619 result = GetFileAttributesW (filename);
621 err = GetLastError ();
629GetFileAttributesExA (LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
630 PVOID lpFileInformation)
636 filename = _dbus_win_utf8_to_utf16 (lpFileName,
NULL);
638 return INVALID_FILE_ATTRIBUTES;
640 result = GetFileAttributesExW (filename, fInfoLevelId, lpFileInformation);
642 err = GetLastError ();
650CreateFileMappingA (HANDLE hFile, LPSECURITY_ATTRIBUTES lpAttributes,
651 DWORD flProtect, DWORD dwMaximumSizeHigh,
652 DWORD dwMaximumSizeLow, LPCSTR lpName)
660 name = _dbus_win_utf8_to_utf16 (lpName,
NULL);
662 return INVALID_HANDLE_VALUE;
667 result = CreateFileMappingW (hFile, lpAttributes, flProtect,
668 dwMaximumSizeHigh, dwMaximumSizeLow,
671 err = GetLastError ();
679CreateDirectoryA (LPCSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
685 pathname = _dbus_win_utf8_to_utf16 (lpPathName,
NULL);
689 result = CreateDirectoryW (pathname, lpSecurityAttributes);
691 err = GetLastError ();
699RemoveDirectoryA (LPCSTR lpPathName)
705 pathname = _dbus_win_utf8_to_utf16 (lpPathName,
NULL);
709 result = RemoveDirectoryW (pathname);
711 err = GetLastError ();
719convert_find_data (LPWIN32_FIND_DATAW fdw, LPWIN32_FIND_DATAA fda)
724 fda->dwFileAttributes = fdw->dwFileAttributes;
725 fda->ftCreationTime = fdw->ftCreationTime;
726 fda->ftLastAccessTime = fdw->ftLastAccessTime;
727 fda->ftLastWriteTime = fdw->ftLastWriteTime;
728 fda->nFileSizeHigh = fdw->nFileSizeHigh;
729 fda->nFileSizeLow = fdw->nFileSizeLow;
731 filename = _dbus_win_utf16_to_utf8 (fdw->cFileName,
NULL);
735 len =
sizeof (fda->cFileName);
736 strncpy (fda->cFileName, filename, len);
737 fda->cFileName[len - 1] =
'\0';
744FindFirstFileA (LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData)
747 WIN32_FIND_DATAW find_file_data;
751 pathname = _dbus_win_utf8_to_utf16 (lpFileName,
NULL);
753 return INVALID_HANDLE_VALUE;
755 result = FindFirstFileW (pathname, &find_file_data);
756 if (result != INVALID_HANDLE_VALUE)
758 BOOL res = convert_find_data (&find_file_data, lpFindFileData);
761 err = GetLastError ();
764 result = INVALID_HANDLE_VALUE;
768 err = GetLastError ();
776FindNextFileA (HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData)
778 WIN32_FIND_DATAW find_file_data;
782 result = FindNextFileW (hFindFile, &find_file_data);
784 result = convert_find_data (&find_file_data, lpFindFileData);
791CreateMutexA (LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner,
800 name = _dbus_win_utf8_to_utf16 (lpName,
NULL);
802 return INVALID_HANDLE_VALUE;
807 result = CreateMutexW (lpMutexAttributes, bInitialOwner, name);
809 err = GetLastError ();
817CreateProcessA (LPCSTR pszImageName, LPSTR pszCmdLine,
818 LPSECURITY_ATTRIBUTES psaProcess,
819 LPSECURITY_ATTRIBUTES psaThread, BOOL fInheritHandles,
820 DWORD fdwCreate, PVOID pvEnvironment, LPCSTR pszCurDir,
821 LPSTARTUPINFOA psiStartInfo,
822 LPPROCESS_INFORMATION pProcInfo)
824 wchar_t *image_name =
NULL;
825 wchar_t *cmd_line =
NULL;
838 image_name = _dbus_win_utf8_to_utf16 (pszImageName,
NULL);
844 cmd_line = _dbus_win_utf8_to_utf16 (pszCmdLine,
NULL);
853 result = CreateProcessW (image_name, cmd_line,
NULL,
NULL,
FALSE,
856 err = GetLastError ();
865RegOpenKeyExA (HKEY hKey, LPCSTR lpSubKey, DWORD ulOptions,
866 REGSAM samDesired, PHKEY phkResult)
874 subkey = _dbus_win_utf8_to_utf16 (lpSubKey,
NULL);
881 result = RegOpenKeyEx (hKey, subkey, ulOptions, samDesired, phkResult);
883 err = GetLastError ();
891RegQueryValueExA (HKEY hKey, LPCSTR lpValueName, LPDWORD lpReserved,
892 LPDWORD lpType, LPBYTE lpData, LPDWORD lpcbData)
902 name = _dbus_win_utf8_to_utf16 (lpValueName,
NULL);
904 return GetLastError ();
910 err = RegQueryValueExW (hKey, name, lpReserved, lpType,
NULL, &data_len);
911 if (err || !lpcbData)
917 data = malloc (data_len +
sizeof (
wchar_t));
921 return ERROR_NOT_ENOUGH_MEMORY;
924 err = RegQueryValueExW (hKey, name, lpReserved, &type, data, &data_len);
944 data[data_len] =
'\0';
945 data[data_len + 1] =
'\0';
949 data_c = _dbus_win_utf16_to_utf8 ((
wchar_t*) data,
NULL);
953 return GetLastError();
956 data_c_len = strlen (data_c) + 1;
957 _dbus_assert (data_c_len <= data_len +
sizeof (
wchar_t));
958 memcpy (data, data_c, data_c_len);
959 data_len = data_c_len;
966 if (data_len > *lpcbData)
967 err = ERROR_MORE_DATA;
969 memcpy (lpData, data, data_len);
972 *lpcbData = data_len;
978FormatMessageA (DWORD dwFlags, PCVOID lpSource, DWORD dwMessageId,
979 DWORD dwLanguageId, LPSTR lpBuffer, DWORD nSize,
982 LPWSTR buffer_w =
NULL;
986 DWORD buffer_new_len;
989 len = FormatMessageW (dwFlags | FORMAT_MESSAGE_ALLOCATE_BUFFER,
990 lpSource, dwMessageId, dwLanguageId,
991 (LPWSTR) &buffer_w, 0, Arguments);
995 buffer_c = _dbus_win_utf16_to_utf8 (buffer_w,
NULL);
998 LocalFree (buffer_w);
1002 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER)
1005 buffer_new = (
char *) buffer_w;
1006 buffer_new_len =
sizeof (wchar_t) * (len + 1);
1007 buffer_w_free =
FALSE;
1009 memcpy (lpBuffer, &buffer_new,
sizeof (buffer_new));
1013 buffer_new = lpBuffer;
1014 buffer_new_len = nSize;
1015 buffer_w_free =
TRUE;
1018 strncpy (buffer_new, buffer_c, buffer_new_len);
1020 buffer_new[buffer_new_len - 1] =
'\0';
1022 LocalFree (buffer_w);
1026 return strlen (buffer_new);
1031GetModuleFileNameA (HINSTANCE hModule, LPSTR lpFilename, DWORD nSize)
1033 wchar_t *filename_w;
1044 filename_w = malloc (
sizeof (
wchar_t) * nSize);
1048 len = GetModuleFileNameW (hModule, filename_w, nSize);
1057 filename_w[nSize - 1] =
'\0';
1058 filename_c = _dbus_win_utf16_to_utf8 (filename_w,
NULL);
1063 strncpy (lpFilename, filename_c, nSize);
1065 lpFilename[nSize - 1] =
'\0';
1068 return strlen (lpFilename);
1073GetTempPathA (DWORD nBufferLength, LPSTR lpBuffer)
1078 len = GetTempPathW (0, dummy);
1094 buffer_w = malloc (
sizeof (
wchar_t) * len);
1098 len_w = GetTempPathW (len, buffer_w);
1100 if (len_w == 0 || len_w >= len)
1107 buffer_w[len_w] =
'\0';
1109 buffer_c = _dbus_win_utf16_to_utf8 (buffer_w,
NULL);
1116 len_c = strlen (buffer_c) + 1;
1117 if (len_c > nBufferLength)
1120 strcpy (lpBuffer, buffer_c);
1128SHGetSpecialFolderPathA (HWND hwndOwner, LPSTR lpszPath,
int nFolder,
1131 wchar_t path[MAX_PATH];
1135 path[0] = (wchar_t) 0;
1136 result = SHGetSpecialFolderPathW (hwndOwner, path, nFolder, fCreate);
1139 path[MAX_PATH - 1] = (wchar_t) 0;
1140 path_c = _dbus_win_utf16_to_utf8 (path,
NULL);
1144 strncpy (lpszPath, path_c, MAX_PATH);
1146 lpszPath[MAX_PATH - 1] =
'\0';
1152OutputDebugStringA (LPCSTR lpOutputString)
1158 str = _dbus_win_utf8_to_utf16 (lpOutputString,
NULL);
1162 OutputDebugStringW (str);
1164 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().