1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-launch-win.c dbus-launch utility
4 * Copyright (C) 2007 Ralf Habacker <ralf.habacker@freenet.de>
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 /* Save string functions. Instead of figuring out the exact _MSC_VER
34 that work, override for everybody. */
37 #define wcscat_s my_wcscat_s
38 #define wcscpy_s my_wcscpy_s
41 wcscat_s (wchar_t *dest, size_t size, wchar_t *src)
43 assert (sizeof (wchar_t) * (wcslen (dest) + wcslen (src) + 1) <= size);
50 wcscpy_s (wchar_t *dest, size_t size, wchar_t *src)
52 assert (sizeof (wchar_t) * (wcslen (src) + 1) <= size);
57 /* TODO (tl): This Windows version of dbus-launch is curretly rather
58 * pointless as it doesn't take the same command-line options as the
59 * UNIX dbus-launch does. A main point of the dbus-launch command is
60 * to pass it for instance a --config-file option to make the started
61 * dbus-daemon use that config file.
63 * This version also doesn't print out any information, which is a
64 * main point of the UNIX one. It should at least support the
65 * --sh-syntax option, and maybe also a --cmd-syntax to print out the
66 * variable settings in cmd.exe syntax?
68 * NOTE (rh): The main task of dbus-launch is (from the man page) to start
69 * a session bus and this is archieved by the current implemention.
71 * Additional on windows the session bus starting in not integrated
72 * into the logon process, so there is no need for any --syntax option.
73 * In fact (at least for kde on windows) the session bus is autostarted
74 * with the first application requesting a session bus.
78 #define AUTO_ACTIVATE_CONSOLE_WHEN_VERBOSE_MODE 1
80 #define DIM(x) (sizeof(x) / sizeof(x[0]))
81 #define WCSTRINGIFY_(x) L ## x
82 #define WCSTRINGIFY(x) WCSTRINGIFY_(x)
85 main (int argc, char **argv)
87 wchar_t dbusDaemonPath[MAX_PATH * 2 + 1];
88 wchar_t command[MAX_PATH * 2 + 1];
96 char *s = getenv("DBUS_VERBOSE");
98 int verbose = s && *s != '\0' ? 1 : 0;
100 PROCESS_INFORMATION pi;
105 GetModuleFileNameW (NULL, dbusDaemonPath, DIM (dbusDaemonPath));
107 daemon_name = WCSTRINGIFY(DBUS_DAEMON_NAME) L".exe";
109 if ((p = wcsrchr (dbusDaemonPath, L'\\')))
112 wcscat_s (dbusDaemonPath, sizeof (dbusDaemonPath), daemon_name);
117 fprintf (stderr, "error: could not extract path from current "
118 "applications module filename\n");
123 /* Windows CE has a different interpretation of cmdline: Start with argv[1]. */
124 wcscpy_s (command, sizeof (command), L"--session");
126 fprintf (stderr, "%ls %ls\n", dbusDaemonPath, command);
129 /* Windows cmdline starts with path, which can contain spaces. */
130 wcscpy_s (command, sizeof (command), L"\"");
131 wcscat_s (command, sizeof (command), dbusDaemonPath);
132 wcscat_s (command, sizeof (command), L"\" --session");
134 fprintf (stderr, "%ls\n", command);
137 memset (&si, 0, sizeof (si));
138 memset (&pi, 0, sizeof (pi));
142 flags |= CREATE_NEW_CONSOLE;
147 flags |= NORMAL_PRIORITY_CLASS;
149 flags |= DETACHED_PROCESS;
152 result = CreateProcessW (dbusDaemonPath, command, 0, 0,
153 inherit, flags, 0, 0, &si, &pi);
158 fprintf (stderr, "Could not start " DBUS_DAEMON_NAME ". error=%u\n",
159 (unsigned)GetLastError ());
163 CloseHandle (pi.hProcess);
164 CloseHandle (pi.hThread);