|
1 #include <Windows.h> |
|
2 #include <Shlwapi.h> |
|
3 #include <strsafe.h> |
|
4 |
|
5 // Linker options |
|
6 #pragma comment(lib, "User32.lib") |
|
7 #pragma comment(lib, "shlwapi.lib") |
|
8 #pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:wmainCRTStartup") |
|
9 |
|
10 #define NUM_MAX_PATH_BYTES sizeof(wchar_t) * MAX_PATH + 1 |
|
11 #define PROFILE_ARG L" -profile " |
|
12 |
|
13 // Options that can be overridden at build time with -D |
|
14 #ifndef B2G_NAME |
|
15 #define B2G_NAME L"b2g.exe" |
|
16 #endif |
|
17 #ifndef GAIA_PATH |
|
18 #define GAIA_PATH L"gaia\\profile" |
|
19 #endif |
|
20 |
|
21 void error(wchar_t* msg){ |
|
22 MessageBoxW(nullptr, msg, L"Error starting program", MB_OK | MB_ICONERROR); |
|
23 } |
|
24 |
|
25 /* This function takes a string which represents a windows path, orig. |
|
26 * The file portion of the path is stripped off and replaced with the |
|
27 * path component, file. This function returns a string which represents |
|
28 * a windows path with the modifications requested and needs to be freed |
|
29 * explicitly by the calling function. |
|
30 */ |
|
31 wchar_t* make_path_with_leaf_file_name(wchar_t* orig, wchar_t* file){ |
|
32 wchar_t* buffer = (wchar_t*) malloc(NUM_MAX_PATH_BYTES); |
|
33 if (!buffer) { |
|
34 return nullptr; |
|
35 } |
|
36 if (FAILED(StringCchCopyW(buffer, NUM_MAX_PATH_BYTES, orig))) { |
|
37 error(L"Error copying string"); |
|
38 free(buffer); |
|
39 buffer = nullptr; |
|
40 } |
|
41 PathRemoveFileSpecW(buffer); |
|
42 if (!PathAppendW(buffer, file)) { |
|
43 error(L"Unable to append file to directory"); |
|
44 free(buffer); |
|
45 buffer = nullptr; |
|
46 } |
|
47 return buffer; |
|
48 } |
|
49 |
|
50 BOOL execute(wchar_t* binary_path, wchar_t* args, int cp_flags) { |
|
51 STARTUPINFOW si; |
|
52 PROCESS_INFORMATION pi; |
|
53 |
|
54 ZeroMemory(&si, sizeof(si)); |
|
55 si.cb = sizeof(si); |
|
56 ZeroMemory(&pi, sizeof(pi)); |
|
57 |
|
58 if (!CreateProcessW( |
|
59 binary_path, |
|
60 args, |
|
61 nullptr, |
|
62 nullptr, |
|
63 FALSE, |
|
64 cp_flags, |
|
65 nullptr, |
|
66 nullptr, |
|
67 &si, |
|
68 &pi)){ |
|
69 error(L"Could not execute program"); |
|
70 return FALSE; |
|
71 } |
|
72 |
|
73 WaitForInputIdle(pi.hProcess, 0); |
|
74 CloseHandle(pi.hProcess); |
|
75 CloseHandle(pi.hThread); |
|
76 return true; |
|
77 } |
|
78 |
|
79 int wmain(int argc, wchar_t *argv[], wchar_t *envp[]){ |
|
80 int cp_flags; |
|
81 wchar_t* b2g_path = make_path_with_leaf_file_name(argv[0], B2G_NAME); |
|
82 wchar_t* profile_path = make_path_with_leaf_file_name(argv[0], GAIA_PATH); |
|
83 // 10 chars for the ' -profile ' portion of the argument |
|
84 wchar_t* args = (wchar_t*) malloc(2 * NUM_MAX_PATH_BYTES + wcslen(PROFILE_ARG)); |
|
85 if (FAILED(StringCchPrintfW(args, NUM_MAX_PATH_BYTES, L"\"%ws\"%ws\"%ws\"", b2g_path, PROFILE_ARG, profile_path))) { |
|
86 error(L"Could not create argument string"); |
|
87 ExitProcess(1); |
|
88 } |
|
89 #ifdef SHOW_CONSOLE |
|
90 cp_flags = 0; |
|
91 #else |
|
92 cp_flags = DETACHED_PROCESS; |
|
93 #endif |
|
94 if (!execute(b2g_path, args, cp_flags)) { |
|
95 error(L"Failed to launch program"); |
|
96 } |
|
97 free(profile_path); |
|
98 free(b2g_path); |
|
99 free(args); |
|
100 profile_path = b2g_path = args = nullptr; |
|
101 |
|
102 } |