From 087ad42625d311310449fd4dd972af33a0108331 Mon Sep 17 00:00:00 2001 From: Marcus Brinkmann Date: Tue, 16 Mar 2010 18:13:11 +0100 Subject: [PATCH] Port dbus-launch to Windows CE. --- tools/dbus-launch-win.c | 203 +++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 188 insertions(+), 15 deletions(-) diff --git a/tools/dbus-launch-win.c b/tools/dbus-launch-win.c index 140238e..ba71ff9 100644 --- a/tools/dbus-launch-win.c +++ b/tools/dbus-launch-win.c @@ -32,19 +32,20 @@ */ #define errno_t int -errno_t strcat_s(char *dest, size_t size, char *src) +static errno_t strcat_s(char *dest, size_t size, char *src) { assert(strlen(dest) + strlen(src) +1 <= size); strcat(dest,src); return 0; } -errno_t strcpy_s(char *dest, size_t size, char *src) +static errno_t strcpy_s(char *dest, size_t size, char *src) { assert(strlen(src) +1 <= size); strcpy(dest,src); return 0; } + #endif /* TODO: Use Unicode APIs */ @@ -72,7 +73,157 @@ errno_t strcpy_s(char *dest, size_t size, char *src) #define AUTO_ACTIVATE_CONSOLE_WHEN_VERBOSE_MODE 1 -int main(int argc,char **argv) +#ifdef DBUS_WINCE +#define _mbsrchr strrchr +#define CreateProcessA _dbus_wince_CreateProcessA +BOOL CreateProcessA(LPCSTR,LPSTR,LPSECURITY_ATTRIBUTES,LPSECURITY_ATTRIBUTES,BOOL,DWORD,PVOID,LPCSTR,LPSTARTUPINFOA,LPPROCESS_INFORMATION); +#define GetModuleFileNameA _dbus_wince_GetModuleFileNameA +DWORD GetModuleFileNameA(HINSTANCE,LPSTR,DWORD); + + +/* Return a malloced string encoded in UTF-8 from the wide char input + string STRING. Caller must free this value. Returns NULL on + failure. Caller may use GetLastError to get the actual error + number. The result of calling this function with STRING set to + NULL is not defined. */ +static char * +wchar_to_utf8 (const wchar_t *string) +{ + int n; + char *result; + + /* Note, that CP_UTF8 is not defined in Windows versions earlier + than NT. */ + n = WideCharToMultiByte (CP_UTF8, 0, string, -1, NULL, 0, NULL, NULL); + if (n < 0) + return NULL; + + result = malloc (n+1); + if (result) + { + n = WideCharToMultiByte (CP_UTF8, 0, string, -1, result, n, NULL, NULL); + if (n < 0) + { + free (result); + result = NULL; + } + } + return result; +} + + +/* Return a malloced wide char string from an UTF-8 encoded input + string STRING. Caller must free this value. Returns NULL on + failure. Caller may use GetLastError to get the actual error + number. The result of calling this function with STRING set to + NULL is not defined. */ +static wchar_t * +utf8_to_wchar (const char *string) +{ + int n; + size_t length = strlen (string); + wchar_t *result; + size_t nbytes; + + n = MultiByteToWideChar (CP_UTF8, 0, string, length, NULL, 0); + if (n < 0 || (n+1) <= 0) + { + SetLastError (ERROR_INVALID_PARAMETER); + return NULL; + } + + nbytes = (size_t)(n+1) * sizeof(*result); + if (nbytes / sizeof(*result) != (n+1)) + { + SetLastError (ERROR_INVALID_PARAMETER); + return NULL; + } + result = malloc (nbytes); + n = MultiByteToWideChar (CP_UTF8, 0, string, length, result, n); + if (n < 0) + return NULL; + result[n] = 0; + + return result; +} + + +BOOL +CreateProcessA (LPCSTR pszImageName, LPSTR pszCmdLine, + LPSECURITY_ATTRIBUTES psaProcess, + LPSECURITY_ATTRIBUTES psaThread, BOOL fInheritHandles, + DWORD fdwCreate, PVOID pvEnvironment, LPCSTR pszCurDir, + LPSTARTUPINFOA psiStartInfo, + LPPROCESS_INFORMATION pProcInfo) +{ + wchar_t *image_name = NULL; + wchar_t *cmd_line = NULL; + BOOL result; + int err; + + assert (psaProcess == NULL); + assert (psaThread == NULL); + assert (fInheritHandles == FALSE); + assert (pvEnvironment == NULL); + assert (pszCurDir == NULL); + /* psiStartInfo is generally not NULL. */ + + if (pszImageName) + { + image_name = utf8_to_wchar (pszImageName); + if (!image_name) + return 0; + } + if (pszCmdLine) + { + cmd_line = utf8_to_wchar (pszCmdLine); + if (!cmd_line) + { + if (image_name) + free (image_name); + return 0; + } + } + + result = CreateProcessW (image_name, cmd_line, NULL, NULL, FALSE, + fdwCreate, NULL, NULL, NULL, pProcInfo); + + err = GetLastError (); + free (image_name); + free (cmd_line); + SetLastError (err); + return result; +} + + +DWORD +GetModuleFileNameA (HINSTANCE hModule, LPSTR lpFilename, DWORD nSize) +{ + wchar_t filename_w[nSize]; + char *filename_c; + DWORD len; + + len = GetModuleFileNameW (hModule, filename_w, nSize); + if (len == 0) + return 0; + + filename_w[nSize - 1] = '\0'; + filename_c = wchar_to_utf8 (filename_w); + if (! filename_c) + return 0; + + strncpy (lpFilename, filename_c, nSize); + free (filename_c); + lpFilename[nSize - 1] = '\0'; + /* strlen is correct (not _mbstrlen), because we want storage and + not string length. */ + return strlen (lpFilename); +} +#endif + + +int +main (int argc, char **argv) { char dbusDaemonPath[MAX_PATH*2+1]; char command[MAX_PATH*2+1]; @@ -80,17 +231,23 @@ int main(int argc,char **argv) char *daemon_name; int result; int showConsole = 0; +#ifdef DBUS_WINCE + char *s = NULL; +#else char *s = getenv("DBUS_VERBOSE"); +#endif int verbose = s && *s != '\0' ? 1 : 0; + PROCESS_INFORMATION pi; - STARTUPINFO si; - HANDLE h; - + STARTUPINFOA si; + BOOL inherit = TRUE; + DWORD flags; + #ifdef AUTO_ACTIVATE_CONSOLE_WHEN_VERBOSE_MODE if (verbose) showConsole = 1; #endif - GetModuleFileName(NULL,dbusDaemonPath,sizeof(dbusDaemonPath)); + GetModuleFileNameA(NULL,dbusDaemonPath,sizeof(dbusDaemonPath)); daemon_name = DBUS_DAEMON_NAME ".exe"; @@ -105,22 +262,38 @@ int main(int argc,char **argv) fprintf(stderr,"error: could not extract path from current applications module filename\n"); return 1; } - - strcpy_s(command,sizeof(command),dbusDaemonPath); - strcat_s(command,sizeof(command)," --session"); - if (verbose) - fprintf(stderr,"%s\n",command); + +#ifdef DBUS_WINCE + /* Windows CE has a different interpretation of cmdline: Start with argv[1]. */ + strcpy_s(command,sizeof(command),"--session"); + if (verbose) + fprintf(stderr,"%s %s\n",dbusDaemonPath,command); +#else + command[0] = '\0'; + /* Windows CE has a different interpretation of cmdline: Start with argv[1]. */ + strcpy_s(command,sizeof(command),dbusDaemonPath); + strcat_s(command,sizeof(command)," --session"); + if (verbose) + fprintf(stderr,"%s\n",command); +#endif memset(&si, 0, sizeof(si)); memset(&pi, 0, sizeof(pi)); si.cb = sizeof(si); + + flags = showConsole ? CREATE_NEW_CONSOLE : 0; +#ifdef DBUS_WINCE + inherit = FALSE; +#else + flags |= NORMAL_PRIORITY_CLASS | DETACHED_PROCESS; +#endif - result = CreateProcess(NULL, + result = CreateProcessA(dbusDaemonPath, command, 0, 0, - TRUE, - (showConsole ? CREATE_NEW_CONSOLE : 0) | NORMAL_PRIORITY_CLASS, + inherit, + flags, 0, 0, &si, -- 1.6.3.3