b2g/gaia/run-b2g.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/b2g/gaia/run-b2g.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,102 @@
     1.4 +#include <Windows.h>
     1.5 +#include <Shlwapi.h>
     1.6 +#include <strsafe.h>
     1.7 +
     1.8 +// Linker options
     1.9 +#pragma comment(lib, "User32.lib")
    1.10 +#pragma comment(lib, "shlwapi.lib")
    1.11 +#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:wmainCRTStartup")
    1.12 +
    1.13 +#define NUM_MAX_PATH_BYTES sizeof(wchar_t) * MAX_PATH + 1
    1.14 +#define PROFILE_ARG L" -profile "
    1.15 +
    1.16 +// Options that can be overridden at build time with -D
    1.17 +#ifndef B2G_NAME
    1.18 +#define B2G_NAME L"b2g.exe"
    1.19 +#endif
    1.20 +#ifndef GAIA_PATH
    1.21 +#define GAIA_PATH L"gaia\\profile"
    1.22 +#endif
    1.23 +
    1.24 +void error(wchar_t* msg){
    1.25 +    MessageBoxW(nullptr, msg, L"Error starting program", MB_OK | MB_ICONERROR);
    1.26 +}
    1.27 +
    1.28 +/* This function takes a string which represents a windows path, orig.
    1.29 + * The file portion of the path is stripped off and replaced with the
    1.30 + * path component, file.  This function returns a string which represents
    1.31 + * a windows path with the modifications requested and needs to be freed
    1.32 + * explicitly by the calling function.
    1.33 + */
    1.34 +wchar_t* make_path_with_leaf_file_name(wchar_t* orig, wchar_t* file){
    1.35 +    wchar_t* buffer = (wchar_t*) malloc(NUM_MAX_PATH_BYTES);
    1.36 +    if (!buffer) {
    1.37 +        return nullptr;
    1.38 +    }
    1.39 +    if (FAILED(StringCchCopyW(buffer, NUM_MAX_PATH_BYTES, orig))) {
    1.40 +        error(L"Error copying string");
    1.41 +        free(buffer);
    1.42 +        buffer = nullptr;
    1.43 +    }
    1.44 +    PathRemoveFileSpecW(buffer);
    1.45 +    if (!PathAppendW(buffer, file)) {
    1.46 +        error(L"Unable to append file to directory");
    1.47 +        free(buffer);
    1.48 +        buffer = nullptr;
    1.49 +    }
    1.50 +    return buffer;
    1.51 +}
    1.52 +
    1.53 +BOOL execute(wchar_t* binary_path, wchar_t* args, int cp_flags) {
    1.54 +    STARTUPINFOW si;
    1.55 +    PROCESS_INFORMATION pi;
    1.56 +
    1.57 +    ZeroMemory(&si, sizeof(si));
    1.58 +    si.cb = sizeof(si);
    1.59 +    ZeroMemory(&pi, sizeof(pi));
    1.60 +    
    1.61 +    if (!CreateProcessW(
    1.62 +        binary_path,
    1.63 +        args,
    1.64 +        nullptr,
    1.65 +        nullptr,
    1.66 +        FALSE,
    1.67 +        cp_flags,
    1.68 +        nullptr,
    1.69 +        nullptr,
    1.70 +        &si,
    1.71 +        &pi)){
    1.72 +            error(L"Could not execute program");
    1.73 +            return FALSE;
    1.74 +    }
    1.75 +
    1.76 +    WaitForInputIdle(pi.hProcess, 0);
    1.77 +    CloseHandle(pi.hProcess);
    1.78 +    CloseHandle(pi.hThread);
    1.79 +    return true;
    1.80 +}
    1.81 +
    1.82 +int wmain(int argc, wchar_t *argv[], wchar_t *envp[]){
    1.83 +    int cp_flags;
    1.84 +    wchar_t* b2g_path = make_path_with_leaf_file_name(argv[0], B2G_NAME);
    1.85 +    wchar_t* profile_path = make_path_with_leaf_file_name(argv[0], GAIA_PATH);
    1.86 +	// 10 chars for the ' -profile ' portion of the argument
    1.87 +    wchar_t* args = (wchar_t*) malloc(2 * NUM_MAX_PATH_BYTES + wcslen(PROFILE_ARG));
    1.88 +    if (FAILED(StringCchPrintfW(args, NUM_MAX_PATH_BYTES, L"\"%ws\"%ws\"%ws\"", b2g_path, PROFILE_ARG, profile_path))) {
    1.89 +        error(L"Could not create argument string");
    1.90 +        ExitProcess(1);
    1.91 +    }
    1.92 +#ifdef SHOW_CONSOLE
    1.93 +    cp_flags = 0;
    1.94 +#else
    1.95 +    cp_flags = DETACHED_PROCESS;
    1.96 +#endif
    1.97 +    if (!execute(b2g_path, args, cp_flags)) {
    1.98 +        error(L"Failed to launch program");
    1.99 +    }
   1.100 +    free(profile_path);
   1.101 +    free(b2g_path);
   1.102 +    free(args);
   1.103 +    profile_path = b2g_path = args = nullptr;
   1.104 +    
   1.105 +}

mercurial