6 #pragma comment(lib, "User32.lib")
7 #pragma comment(lib, "shlwapi.lib")
8 #pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:wmainCRTStartup")
10 #define NUM_MAX_PATH_BYTES sizeof(wchar_t) * MAX_PATH + 1
11 #define PROFILE_ARG L" -profile "
13 // Options that can be overridden at build time with -D
15 #define B2G_NAME L"b2g.exe"
18 #define GAIA_PATH L"gaia\\profile"
21 void error(wchar_t* msg
){
22 MessageBoxW(nullptr, msg
, L
"Error starting program", MB_OK
| MB_ICONERROR
);
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.
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
);
36 if (FAILED(StringCchCopyW(buffer
, NUM_MAX_PATH_BYTES
, orig
))) {
37 error(L
"Error copying string");
41 PathRemoveFileSpecW(buffer
);
42 if (!PathAppendW(buffer
, file
)) {
43 error(L
"Unable to append file to directory");
50 BOOL
execute(wchar_t* binary_path
, wchar_t* args
, int cp_flags
) {
52 PROCESS_INFORMATION pi
;
54 ZeroMemory(&si
, sizeof(si
));
56 ZeroMemory(&pi
, sizeof(pi
));
69 error(L
"Could not execute program");
73 WaitForInputIdle(pi
.hProcess
, 0);
74 CloseHandle(pi
.hProcess
);
75 CloseHandle(pi
.hThread
);
79 int wmain(int argc
, wchar_t *argv
[], wchar_t *envp
[]){
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");
92 cp_flags
= DETACHED_PROCESS
;
94 if (!execute(b2g_path
, args
, cp_flags
)) {
95 error(L
"Failed to launch program");
100 profile_path
= b2g_path
= args
= nullptr;