summaryrefslogtreecommitdiffstats
path: root/desktop/win32/source/wrapper.h
blob: f0ff5b617f99eaebc7d876b4c5a5771f7ccd54d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#pragma once
#ifndef __cplusplus
#error Need C++ to compile
#endif

#include "main.h"

#ifndef _WINDOWS_
#if defined _MSC_VER
#pragma warning(push, 1)
#endif
#   include <windows.h>
#if defined _MSC_VER
#pragma warning(pop)
#endif
#endif


#ifndef _INC_TCHAR
#   ifdef UNICODE
#       define _UNICODE
#   endif
#   include <tchar.h>
#endif

#ifdef UNICODE
#   define Main MainW
#   define GetArgv( pArgc )         CommandLineToArgvW( GetCommandLine(), pArgc )
#   define PROCESS_CREATIONFLAGS    CREATE_UNICODE_ENVIRONMENT
#else
#   define GetArgv( pArgc )         (*pArgc = __argc, __argv)
#   define PROCESS_CREATIONFLAGS    0
#   define Main MainA
#endif

#define BIN_EXT_STR         TEXT(".bin")
#define PARAM_LIBPATH_STR   TEXT("-libpath=")
#define PARAM_LOCAL_STR     TEXT("-local")
#define PARAM_REMOTE_STR    TEXT("-remote")

#if defined( REMOTE )
#define DEFAULT_LIBPATH     TEXT("remote;")
#elif defined( LOCAL )
#define DEFAULT_LIBPATH     TEXT("local;")
#endif

extern "C" int Main()
{
    // Retreive startup info

    STARTUPINFO aStartupInfo;

    ZeroMemory( &aStartupInfo, sizeof(aStartupInfo) );
    aStartupInfo.cb = sizeof( aStartupInfo );
    GetStartupInfo( &aStartupInfo );

    // Retrieve command line

    LPTSTR  lpCommandLine = GetCommandLine();

    LPTSTR  *ppArguments = NULL;
    int     nArguments = 0;

    ppArguments = GetArgv( &nArguments );

    // Calculate application name


    TCHAR   szApplicationName[MAX_PATH];
    TCHAR   szDrive[MAX_PATH];
    TCHAR   szDir[MAX_PATH];
    TCHAR   szFileName[MAX_PATH];
    TCHAR   szExt[MAX_PATH];

    GetModuleFileName( NULL, szApplicationName, MAX_PATH );
    _tsplitpath( szApplicationName, szDrive, szDir, szFileName, szExt );
    _tmakepath( szApplicationName, szDrive, szDir, szFileName, BIN_EXT_STR );

    // Retreive actual environment

    TCHAR   szBuffer[1024];
    TCHAR   szPathValue[1024] = TEXT("");

#ifdef DEFAULT_LIBPATH
    _tmakepath( szPathValue, szDrive, szDir, DEFAULT_LIBPATH, TEXT("") );
#endif

    for ( int argn = 1; argn < nArguments; argn++ )
    {
        if ( 0 == _tcscmp( ppArguments[argn], PARAM_REMOTE_STR ) )
        {
            _tmakepath( szPathValue, szDrive, szDir, TEXT("remote;"), TEXT("") );
            break;
        }
        else if ( 0 == _tcscmp( ppArguments[argn], PARAM_LOCAL_STR ) )
        {
            _tmakepath( szPathValue, szDrive, szDir, TEXT("local;"), TEXT("") );
            break;
        }
        else if ( 0 == _tcsncmp( ppArguments[argn], PARAM_LIBPATH_STR, _tcslen(PARAM_LIBPATH_STR) ) )
        {
            LPTSTR  pFileSpec = NULL;

            GetFullPathName( ppArguments[argn] + _tcslen(PARAM_LIBPATH_STR), sizeof(szPathValue) / sizeof(TCHAR), szPathValue, &pFileSpec );
            _tcscat( szPathValue, TEXT(";") );
            break;
        }
    }

    GetEnvironmentVariable( TEXT("PATH"), szBuffer, sizeof(szBuffer) );
    _tcscat( szPathValue, szBuffer );
    SetEnvironmentVariable( TEXT("PATH"), szPathValue);

    LPVOID  lpEnvironment = GetEnvironmentStrings();


    // Retrieve current directory

    TCHAR               szCurrentDirectory[MAX_PATH];
    GetCurrentDirectory( MAX_PATH, szCurrentDirectory );

    // Set the Flags

    DWORD   dwCreationFlags = PROCESS_CREATIONFLAGS;

    PROCESS_INFORMATION aProcessInfo;

    BOOL    fSuccess = CreateProcess(
        szApplicationName,
        lpCommandLine,
        NULL,
        NULL,
        TRUE,
        dwCreationFlags,
        lpEnvironment,
        szCurrentDirectory,
        &aStartupInfo,
        &aProcessInfo );

    if ( fSuccess )
    {
        DWORD   dwExitCode;

        WaitForSingleObject( aProcessInfo.hProcess, INFINITE );
        fSuccess = GetExitCodeProcess( aProcessInfo.hProcess, &dwExitCode );

        return dwExitCode;
    }

    DWORD   dwError = GetLastError();

    LPVOID lpMsgBuf;

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,
        dwError,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
        (LPTSTR)&lpMsgBuf,
        0,
        NULL
    );

    // Display the string.
    MessageBox( NULL, (LPCTSTR)lpMsgBuf, NULL, MB_OK | MB_ICONERROR );

    // Free the buffer.
    LocalFree( lpMsgBuf );

    return GetLastError();
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */