1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-sysdeps-wince-glue.c Wrappers for Windows CE around system/libc features (internal to D-BUS implementation)
4 * Copyright (C) 2002, 2003 Red Hat, Inc.
5 * Copyright (C) 2003 CodeFactory AB
6 * Copyright (C) 2005 Novell, Inc.
7 * Copyright (C) 2006 Ralf Habacker <ralf.habacker@freenet.de>
8 * Copyright (C) 2006 Peter Kümmel <syntheticpp@gmx.net>
9 * Copyright (C) 2006 Christian Ehrlicher <ch.ehrlicher@gmx.de>
11 * Licensed under the Academic Free License version 2.1
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "dbus-internals.h"
31 #include "dbus-sysdeps.h"
32 #include "dbus-sysdeps-win.h"
35 /* Including shlobj.h creates trouble on some compilers. Just chicken
36 out here by defining just what we need. */
37 #ifndef CSIDL_PERSONAL
38 #define CSIDL_PERSONAL 5
42 /* Copy SRC to DEST, returning the address of the terminating '\0' in DEST. */
44 stpcpy (char *dest, const char *src)
57 /* This is special cased, because we must avoid using many dbus
58 functions (such as memory allocations): Those functions may in turn
59 cause verbose output and check the flag! */
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);
84 /* Round up to multiple of wchar_t, convert to number of wchar_t's, and add 1. */
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);
99 result_w[n1 - 1] = 0; /* Make sure it is really a string. */
101 /* NOTE: REG_MULTI_SZ and REG_EXPAND_SZ not supported, because they
102 are not needed in this module. */
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);
134 /* Return a string from the W32 Registry or NULL in case of error.
135 Caller must release the return value. A NULL for root is an alias
136 for HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE in turn. */
138 read_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))
160 return NULL; /* no need for a RegClose, so return direct */
161 /* It seems to be common practise to fall back to HKLM. */
162 if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle))
163 return NULL; /* still no need for a RegClose, so return direct */
167 if (RegQueryValueExA (key_handle, name, 0, NULL, NULL, &nbytes))
171 /* Try to fallback to HKLM also for a missing value. */
172 RegCloseKey (key_handle);
173 if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle))
174 return NULL; /* Nope. */
175 if (RegQueryValueExA (key_handle, name, 0, NULL, NULL, &nbytes))
179 result = malloc (n1);
182 if (RegQueryValueExA (key_handle, name, 0, &type, result, &n1))
188 result[nbytes] = 0; /* Make sure it is really a string. */
191 RegCloseKey (key_handle);
199 return read_w32_registry_string ("HKEY_LOCAL_MACHINE",
200 "Software\\freedesktop\\DBus",
201 "Install Directory");
206 find_env_in_registry (const char *name)
208 return read_w32_registry_string ("HKEY_LOCAL_MACHINE",
209 "Software\\freedesktop\\DBus",
215 find_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);
239 find_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);
263 find_my_documents_folder ()
265 /* One for safety, just in case. */
266 char dir[MAX_PATH + 1];
270 /* May return false even if successful. */
271 SHGetSpecialFolderPathA (0, dir, CSIDL_PERSONAL, 0);
275 result = malloc (strlen (dir) + 1);
278 strcpy (result, dir);
285 char *environ[MAX_ENV + 1];
288 getenv (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 ();
377 /* This is what windows does. */
383 GetSystemTimeAsFileTime (LPFILETIME ftp)
387 SystemTimeToFileTime (&st, ftp);
392 _mbsrchr (const unsigned char* str, unsigned int ch)
394 /* FIXME. This is not multi-byte safe. */
395 return strrchr (str, ch);
399 HANDLE 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)
417 /* Just in case Windows CE changes its behaviour, we check for
418 the right error value here. */
419 if (GetLastError () != ERROR_ALREADY_EXISTS)
421 CloseHandle(hMapping);
422 hMapping = INVALID_HANDLE_VALUE;
430 MoveFileExA (LPCSTR lpExistingFileName, LPCSTR lpNewFileName, DWORD dwFlags)
432 _dbus_assert (dwFlags == MOVEFILE_REPLACE_EXISTING);
434 if (_dbus_file_exists (lpNewFileName))
436 BOOL result = DeleteFileA (lpNewFileName);
440 return MoveFileA (lpExistingFileName, lpNewFileName);
445 SetHandleInformation (HANDLE hObject, DWORD dwMask, DWORD dwFlags)
447 _dbus_assert (dwMask == (HANDLE_FLAG_INHERIT | HANDLE_FLAG_PROTECT_FROM_CLOSE));
448 _dbus_assert (dwFlags == 0);
450 /* Not supported on Windows CE, and actually the default. So just
451 return overwhelming success. */
457 SearchPathA (LPCSTR lpPath, LPCSTR lpFileName, LPCSTR lpExtension,
458 DWORD nBufferLength, LPSTR lpBuffer, LPSTR* lpFilePart)
464 _dbus_assert (lpPath == NULL);
465 _dbus_assert (lpExtension == NULL);
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;
494 * @param sid points to sid buffer, need to be freed with LocalFree()
495 * @returns process sid
498 _dbus_getsid(char **sid)
500 /* There is nothing like this on Windows CE, so we fake it. */
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));
516 LookupAccountNameW (LPCWSTR lpSystemName, LPCWSTR lpAccountName, PSID Sid, PDWORD cbSid,
517 LPWSTR ReferencedDomainName, PDWORD cchReferencedDomainName, PSID_NAME_USE peUse)
519 /* Currently not needed. */
525 IsValidSid (PSID psid)
527 /* Currently not needed. */
533 CreateFileA (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 ();
551 dbus_free (filename);
558 DeleteFileA (LPCSTR lpFileName)
564 filename = _dbus_win_utf8_to_utf16 (lpFileName, NULL);
568 result = DeleteFileW (filename);
570 err = GetLastError ();
571 dbus_free (filename);
578 MoveFileA (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);
592 dbus_free (existing_filename);
596 result = MoveFileW (existing_filename, new_filename);
598 err = GetLastError ();
599 dbus_free (existing_filename);
600 dbus_free (new_filename);
607 GetFileAttributesA(LPCSTR lpFileName)
613 filename = _dbus_win_utf8_to_utf16 (lpFileName, NULL);
615 return INVALID_FILE_ATTRIBUTES;
617 result = GetFileAttributesW (filename);
619 err = GetLastError ();
620 dbus_free (filename);
627 GetFileAttributesExA (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 ();
641 dbus_free (filename);
648 CreateFileMappingA (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 ();
677 CreateDirectoryA (LPCSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
683 pathname = _dbus_win_utf8_to_utf16 (lpPathName, NULL);
687 result = CreateDirectoryW (pathname, lpSecurityAttributes);
689 err = GetLastError ();
690 dbus_free (pathname);
697 RemoveDirectoryA (LPCSTR lpPathName)
703 pathname = _dbus_win_utf8_to_utf16 (lpPathName, NULL);
707 result = RemoveDirectoryW (pathname);
709 err = GetLastError ();
710 dbus_free (pathname);
717 convert_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';
742 FindFirstFileA (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 ();
767 dbus_free (pathname);
774 FindNextFileA (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);
789 CreateMutexA (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 ();
815 CreateProcessA (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;
827 _dbus_assert (psaProcess == NULL);
828 _dbus_assert (psaThread == NULL);
829 _dbus_assert (fInheritHandles == FALSE);
830 _dbus_assert (pvEnvironment == NULL);
831 _dbus_assert (pszCurDir == NULL);
832 /* psiStartInfo is generally not NULL. */
836 image_name = _dbus_win_utf8_to_utf16 (pszImageName, NULL);
842 cmd_line = _dbus_win_utf8_to_utf16 (pszCmdLine, NULL);
846 dbus_free (image_name);
851 result = CreateProcessW (image_name, cmd_line, NULL, NULL, FALSE,
852 fdwCreate, NULL, NULL, NULL, pProcInfo);
854 err = GetLastError ();
855 dbus_free (image_name);
856 dbus_free (cmd_line);
863 RegOpenKeyExA (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 ();
889 RegQueryValueExA (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);
926 /* If err is ERROR_MORE_DATA, there probably was a race condition.
927 We can punt this to the caller just as well. */
934 /* NOTE: REG_MULTI_SZ and REG_EXPAND_SZ not supported, because they
935 are not needed in this module. */
941 /* This is valid since we allocated one more above. */
942 data[data_len] = '\0';
943 data[data_len + 1] = '\0';
945 /* The cast is valid because malloc guarantees alignment of
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;
961 /* DATA and DATA_LEN now contain the result. */
964 if (data_len > *lpcbData)
965 err = ERROR_MORE_DATA;
967 memcpy (lpData, data, data_len);
970 *lpcbData = data_len;
976 FormatMessageA (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)
1002 /* We need to return a buffer that's freeable with LocalFree. */
1003 buffer_new = (char *) buffer_w;
1004 buffer_new_len = sizeof (wchar_t) * (len + 1);
1005 buffer_w_free = FALSE;
1006 /* Avoid alignment issue by using memcpy. */
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);
1017 dbus_free (buffer_c);
1018 buffer_new[buffer_new_len - 1] = '\0';
1020 LocalFree (buffer_w);
1022 /* strlen is correct (not _mbstrlen), because we want storage and
1023 not string length. */
1024 return strlen (buffer_new);
1029 GetModuleFileNameA (HINSTANCE hModule, LPSTR lpFilename, DWORD nSize)
1031 wchar_t *filename_w;
1037 /* Windows XP/2000. */
1042 filename_w = malloc (sizeof (wchar_t) * nSize);
1046 len = GetModuleFileNameW (hModule, filename_w, nSize);
1049 /* Note: If we fail with ERROR_INSUFFICIENT_BUFFER, this is still
1050 (approximately) correct. */
1055 filename_w[nSize - 1] = '\0';
1056 filename_c = _dbus_win_utf16_to_utf8 (filename_w, NULL);
1061 strncpy (lpFilename, filename_c, nSize);
1062 dbus_free (filename_c);
1063 lpFilename[nSize - 1] = '\0';
1064 /* strlen is correct (not _mbstrlen), because we want storage and
1065 not string length. */
1066 return strlen (lpFilename);
1071 GetTempPathA (DWORD nBufferLength, LPSTR lpBuffer)
1076 len = GetTempPathW (0, dummy);
1080 _dbus_assert (len <= MAX_PATH);
1082 /* Better be safe than sorry. MSDN doesn't say if len is with or
1083 without terminating 0. */
1092 buffer_w = malloc (sizeof (wchar_t) * len);
1096 len_w = GetTempPathW (len, buffer_w);
1097 /* Give up if we still can't get at it. */
1098 if (len_w == 0 || len_w >= len)
1104 /* Better be really safe. */
1105 buffer_w[len_w] = '\0';
1107 buffer_c = _dbus_win_utf16_to_utf8 (buffer_w, NULL);
1112 /* strlen is correct (not _mbstrlen), because we want storage and
1113 not string length. */
1114 len_c = strlen (buffer_c) + 1;
1115 if (len_c > nBufferLength)
1118 strcpy (lpBuffer, buffer_c);
1119 dbus_free (buffer_c);
1126 SHGetSpecialFolderPathA (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);
1135 /* Note: May return false even if succeeds. */
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';
1150 OutputDebugStringA (LPCSTR lpOutputString)
1156 str = _dbus_win_utf8_to_utf16 (lpOutputString, NULL);
1160 OutputDebugStringW (str);
1162 err = GetLastError ();