summaryrefslogtreecommitdiff
path: root/setup_native/source/win32/stwrapper/stwrapper.cxx
blob: 0a12b3a80f611bc615ecd61cfaf78cf2790139a0 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#define WIN32_LEAN_AND_MEAN

#ifdef _MSC_VER
#pragma warning(disable:4668 4917) // disable warnings for system headers
#endif

#include <windows.h>
#include <windowsx.h>
#include <shellapi.h>
#include <shlobj.h>
#include <tchar.h>

#include <stdio.h>

enum PathResult
{
    PATHRESULT_OK,
    PATHRESULT_API_NOT_SUPPORTED,
    PATHRESULT_EXE_NOT_FOUND
};

const int MAXCMDLINELEN = 32768;

static TCHAR       g_szSTInstallationPath[MAX_PATH] = TEXT("");
static TCHAR       g_szOperatingSystem[256]         = TEXT("");

static const TCHAR g_szSTExecutable[256]            = TEXT("stclient.exe");

//***************************************************************************

LONG RegReadValue( HKEY hBaseKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, LPVOID lpData, DWORD cbData )
{
    HKEY    hKey = NULL;
    LONG    lResult( 0 );

    lResult = RegOpenKeyEx( hBaseKey, lpSubKey, 0, KEY_QUERY_VALUE, &hKey );

    if ( ERROR_SUCCESS == lResult )
    {
        lResult = RegQueryValueEx( hKey, lpValueName, NULL, NULL, (LPBYTE)lpData, &cbData );
        RegCloseKey( hKey );
    }

    return lResult;
}

//***************************************************************************

static LPTSTR *GetCommandArgs( int *pArgc )
{
#ifdef UNICODE
    return CommandLineToArgvW( GetCommandLineW(), pArgc );
#else
    *pArgc = __argc;
    return __argv;
#endif
}

//***************************************************************************

static bool IsSupportedPlatform()
{
    OSVERSIONINFO aOsVersion;

    ZeroMemory( &aOsVersion, sizeof( OSVERSIONINFO ));
    aOsVersion.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );

    // Try to determine OS version
    if ( GetVersionEx( &aOsVersion ))
    {
        switch ( aOsVersion.dwPlatformId )
        {
            case VER_PLATFORM_WIN32_NT:     // Windows NT based
                return true;

            case VER_PLATFORM_WIN32_WINDOWS: // Windows Me/98/95.
            case VER_PLATFORM_WIN32s:        // Win32s
                return false;

            default:
                return false;
        }
    }

    return false;
}

//***************************************************************************

static LPCTSTR GetOperatingSystemString()
{
    OSVERSIONINFO aOsVersion;

    ZeroMemory( &aOsVersion, sizeof( OSVERSIONINFO ));
    aOsVersion.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );

    _tcscpy( g_szOperatingSystem, TEXT( "Microsoft Windows" ));

    // Try to determine OS version
    if ( GetVersionEx( &aOsVersion ))
    {
        switch ( aOsVersion.dwPlatformId )
        {
            // Test for the Windows NT product family.
            case VER_PLATFORM_WIN32_NT:
            {
                if ( aOsVersion.dwMajorVersion == 3 )
                {
                    _tcscat( g_szOperatingSystem, TEXT( " NT 3." ));
                    if ( aOsVersion.dwMinorVersion == 0 )
                        _tcscat( g_szOperatingSystem, TEXT( "0" ));
                    else if ( aOsVersion.dwMinorVersion == 5 )
                        _tcscat( g_szOperatingSystem, TEXT( "5" ));
                    else if ( aOsVersion.dwMinorVersion == 51 )
                        _tcscat( g_szOperatingSystem, TEXT( "51" ));
                }
                else if ( aOsVersion.dwMajorVersion == 4 )
                    _tcscat( g_szOperatingSystem, TEXT( " NT 4.0" ));
                else if ( aOsVersion.dwMajorVersion == 5 )
                {
                    if ( aOsVersion.dwMinorVersion == 0 )
                        _tcscat( g_szOperatingSystem, TEXT( " 2000" ));
                    else if ( aOsVersion.dwMinorVersion == 1 )
                        _tcscat( g_szOperatingSystem, TEXT( " XP" ));
                    else if ( aOsVersion.dwMinorVersion == 2 )
                        _tcscat( g_szOperatingSystem, TEXT( " Server 2003" ));
                }
                else if ( aOsVersion.dwMajorVersion == 6 )
                {
                    if ( aOsVersion.dwMinorVersion == 0 )
                        _tcscat( g_szOperatingSystem, " Vista" );
                }
            }
            break;

            // Test for the Windows Me/98/95.
            case VER_PLATFORM_WIN32_WINDOWS:
            {
                if ( aOsVersion.dwMinorVersion == 0 )
                  _tcscat( g_szOperatingSystem, TEXT( " 95" ));
                else if ( aOsVersion.dwMinorVersion == 10 )
                  _tcscat( g_szOperatingSystem, TEXT( " 98" ));
                else if ( aOsVersion.dwMinorVersion == 90 )
                  _tcscat( g_szOperatingSystem, TEXT( " Me" ));
            }
            break;
        }
    }

    return g_szOperatingSystem;
}

//***************************************************************************

static bool FileExists( LPCTSTR lpPathToFile )
{
    bool            bResult = false;
    HANDLE          hFind;
    WIN32_FIND_DATA FindFileData;

    hFind = FindFirstFile( lpPathToFile, &FindFileData );

    if ( hFind != INVALID_HANDLE_VALUE )
    {
        FindClose( hFind );
        bResult = true;
    }

    return bResult;
}

//***************************************************************************

static bool GetProgramFilesFolder( LPTSTR strPath )
{
    bool      bRet = false;
    HINSTANCE hLibrary;

    if (( hLibrary = LoadLibrary( "shell32.dll" )) != NULL )
    {
        BOOL (WINAPI *pSHGetSpecialFolderPathA)( HWND, LPSTR, int, BOOL );

        pSHGetSpecialFolderPathA = (BOOL (WINAPI *)(HWND, LPSTR, int, BOOL))GetProcAddress( hLibrary, "SHGetSpecialFolderPathA" );

        if ( pSHGetSpecialFolderPathA )
        {
            if ( pSHGetSpecialFolderPathA( NULL, strPath, CSIDL_PROGRAM_FILES, TRUE ))
                bRet = true;
        }
    }

    FreeLibrary( hLibrary );

    return ( bRet );
}

//***************************************************************************

static PathResult RetrieveExecutablePath( LPTSTR szExecutablePath )
{
    PathResult eRet = PATHRESULT_API_NOT_SUPPORTED;
    TCHAR szProgramFilesFolder[MAX_PATH];

    if ( GetProgramFilesFolder( szProgramFilesFolder ))
    {
        size_t nLen = _tcslen( szProgramFilesFolder );
        if ( nLen > 0 )
        {
            _tcscpy( szExecutablePath, szProgramFilesFolder );
            if ( szProgramFilesFolder[nLen-1] != '\\' )
                _tcscat( szExecutablePath, TEXT( "\\" ));
            _tcscat( szExecutablePath, TEXT( "Sun\\servicetag\\" ));
            _tcscat( szExecutablePath, g_szSTExecutable );
            eRet = FileExists( szExecutablePath ) ? PATHRESULT_OK : PATHRESULT_EXE_NOT_FOUND;
        }
    }

    return eRet;
}

//***************************************************************************

static void SafeCopy( LPTSTR lpTarget, LPCSTR lpSource, size_t nMaxLen )
{
    size_t nLen  = _tcslen( lpSource );
    size_t nCopy = ( nLen < size_t( nMaxLen-1 )) ? nLen : nMaxLen-1;
    _tcsncpy( lpTarget, lpSource, nMaxLen-1 );
    *(lpTarget+nCopy) = 0;
}

//***************************************************************************

int WINAPI _tWinMain( HINSTANCE /*hInstance*/, HINSTANCE, LPTSTR, int )
{
    const DWORD ERR_NO_RECORDS_FOUND = 225;
    const DWORD ERR_DUP_RECORD       = 226;

    DWORD dwExitCode = (DWORD)1;

    int     nArgs  = 0;
    LPTSTR* lpArgs = GetCommandArgs( &nArgs );

    if ( !IsSupportedPlatform() )
    {
        // Return 0 for a successful run on not supported platforms
        // We don't want that the Office tries to start us forever.
        return 0;
    }

    if ( nArgs >= 11 )
    {
        TCHAR szTargetURN[1024]         = {0};
        TCHAR szProductName[1024]       = {0};
        TCHAR szProductVersion[1024]    = {0};
        TCHAR szParentProductName[1024] = {0};
        TCHAR szProductSource[1024]     = {0};
        TCHAR szInstanceURN[1024]       = {0};

//      -i)  INSTANCE_URN="$2"; shift;;
//      -t)  TARGET_URN="$2"; shift;;
//      -p)  PRODUCT_NAME="$2"; shift;;
//      -e)  PRODUCT_VERSION="$2"; shift;;
//      -P)  PARENT_PRODUCT_NAME="$2"; shift;;
//      -S)  PRODUCT_SOURCE="$2"; shift;;
//      "usage: $0 [-i <instance urn>] -p <product name> -e <product version> -t <urn> -S <source> -P <parent product name>"

        int i = 1;
        while ( i < nArgs )
        {
            LPTSTR lpArg = lpArgs[i];
            if ( _tcslen( lpArg ) >= 2 )
            {
                if ( lpArg[0] == '-' )
                {
                    switch ( lpArg[1] )
                    {
                        case 'i':
                        {
                            if ( i < nArgs )
                                ++i;
                            SafeCopy( szInstanceURN, lpArgs[i], SAL_N_ELEMENTS( szInstanceURN ));
                            break;
                        }

                        case 't':
                        {
                            if ( i < nArgs )
                                ++i;
                            SafeCopy( szTargetURN, lpArgs[i], SAL_N_ELEMENTS( szTargetURN ));
                            break;
                        }
                        case 'p':
                        {
                            if ( i < nArgs )
                                ++i;
                            SafeCopy( szProductName, lpArgs[i], SAL_N_ELEMENTS( szProductName ));
                            break;
                        }
                        case 'e':
                        {
                            if ( i < nArgs )
                                ++i;
                            SafeCopy( szProductVersion, lpArgs[i], SAL_N_ELEMENTS( szProductVersion ));
                            break;
                        }
                        case 'P':
                        {
                            if ( i < nArgs )
                                ++i;
                            SafeCopy( szParentProductName, lpArgs[i], SAL_N_ELEMENTS( szParentProductName ));
                            break;
                        }
                        case 'S':
                        {
                            if ( i < nArgs )
                                ++i;
                            SafeCopy( szProductSource, lpArgs[i], SAL_N_ELEMENTS( szProductSource ));
                            break;
                        }

                        default:
                            break;
                    } // switch
                }
            }

            ++i;
        }

        if ( RetrieveExecutablePath( g_szSTInstallationPath ) == PATHRESULT_OK )
        {
            BOOL bSuccess = TRUE;
            BOOL bProcessStarted = FALSE;

            STARTUPINFO         aStartupInfo;
            PROCESS_INFORMATION aProcessInfo;
            LPTSTR              lpCommandLine = 0;

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

            if ( _tcslen( szInstanceURN ) == 0 )
            {
                // TEST=`${STCLIENT} -f -t ${TARGET_URN}`
                lpCommandLine = new TCHAR[MAXCMDLINELEN];

                _tcscpy( lpCommandLine, TEXT( "\"" ));
                _tcscat( lpCommandLine, g_szSTInstallationPath );
                _tcscat( lpCommandLine, TEXT( "\"" ));
                _tcscat( lpCommandLine, TEXT( " -f" ));
                _tcscat( lpCommandLine, TEXT( " -t "));
                _tcscat( lpCommandLine, TEXT( "\"" ));
                _tcscat( lpCommandLine, szTargetURN );
                _tcscat( lpCommandLine, TEXT( "\"" ));

                bSuccess = CreateProcess(
                                   NULL,
                                   lpCommandLine,
                                   NULL,
                                   NULL,
                                   TRUE,
                                   CREATE_NO_WINDOW,
                                   NULL,
                                   NULL,
                                   &aStartupInfo,
                                   &aProcessInfo );

                bProcessStarted = TRUE;

                // wait until process ends to receive exit code
                WaitForSingleObject( aProcessInfo.hProcess, INFINITE );

                delete []lpCommandLine;
            }

            if ( bSuccess )
            {
                DWORD dwSTClientExitCode( ERR_NO_RECORDS_FOUND );
                if ( bProcessStarted )
                {
                    GetExitCodeProcess( aProcessInfo.hProcess, &dwSTClientExitCode );
                    dwSTClientExitCode &= 0x000000ff;

                    CloseHandle( aProcessInfo.hProcess );
                    CloseHandle( aProcessInfo.hThread );
                }

                if ( dwSTClientExitCode == ERR_NO_RECORDS_FOUND )
                {
                    // output=`${STCLIENT} -a [-i "${INSTANCE_URN}"] -p "${PRODUCT_NAME}" -e "${PRODUCT_VERSION}" -t "${TARGET_URN}" -S "${PRODUCT_SOURCE}" -P "${PARENT_PRODUCT_NAME}" -m "Sun Microsystems, Inc." -A ${uname} -z global`
                    lpCommandLine = new TCHAR[MAXCMDLINELEN];

                    _tcscpy( lpCommandLine, TEXT( "\"" ));
                    _tcscat( lpCommandLine, g_szSTInstallationPath );
                    _tcscat( lpCommandLine, TEXT( "\"" ));
                    _tcscat( lpCommandLine, TEXT( " -a" ));
                    if ( _tcslen( szInstanceURN ) > 0 )
                    {
                        _tcscat( lpCommandLine, TEXT( " -i " ));
                        _tcscat( lpCommandLine, TEXT( "\"" ));
                        _tcscat( lpCommandLine, szInstanceURN );
                        _tcscat( lpCommandLine, TEXT( "\"" ));
                    }
                    _tcscat( lpCommandLine, TEXT( " -p " ));
                    _tcscat( lpCommandLine, TEXT( "\"" ));
                    _tcscat( lpCommandLine, szProductName );
                    _tcscat( lpCommandLine, TEXT( "\"" ));
                    _tcscat( lpCommandLine, TEXT( " -e " ));
                    _tcscat( lpCommandLine, TEXT( "\"" ));
                    _tcscat( lpCommandLine, szProductVersion );
                    _tcscat( lpCommandLine, TEXT( "\"" ));
                    _tcscat( lpCommandLine, TEXT( " -t " ));
                    _tcscat( lpCommandLine, TEXT( "\"" ));
                    _tcscat( lpCommandLine, szTargetURN );
                    _tcscat( lpCommandLine, TEXT( "\"" ));
                    _tcscat( lpCommandLine, TEXT( " -S " ));
                    _tcscat( lpCommandLine, TEXT( "\"" ));
                    _tcscat( lpCommandLine, szProductSource );
                    _tcscat( lpCommandLine, TEXT( "\"" ));
                    _tcscat( lpCommandLine, TEXT( " -P " ));
                    _tcscat( lpCommandLine, TEXT( "\"" ));
                    _tcscat( lpCommandLine, szParentProductName );
                    _tcscat( lpCommandLine, TEXT( "\"" ));
                    _tcscat( lpCommandLine, TEXT( " -m \"The Document Foundation\"" ));
                    _tcscat( lpCommandLine, TEXT( " -A " ));
                    _tcscat( lpCommandLine, TEXT( "\"" ));
                    _tcscat( lpCommandLine, GetOperatingSystemString() );
                    _tcscat( lpCommandLine, TEXT( "\"" ));
                    _tcscat( lpCommandLine, TEXT( " -z global" ));

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

                    bSuccess = CreateProcess(
                                       NULL,
                                       lpCommandLine,
                                       NULL,
                                       NULL,
                                       TRUE,
                                       CREATE_NO_WINDOW,
                                       NULL,
                                       NULL,
                                       &aStartupInfo,
                                       &aProcessInfo );

                    delete []lpCommandLine;

                    // wait until process ends to receive exit code
                    WaitForSingleObject( aProcessInfo.hProcess, INFINITE );

                    dwSTClientExitCode = 0;
                    GetExitCodeProcess( aProcessInfo.hProcess, &dwSTClientExitCode );
                    dwSTClientExitCode &= 0x000000ff;

                    CloseHandle( aProcessInfo.hProcess );
                    CloseHandle( aProcessInfo.hThread );

                    if ( !bSuccess )
                        dwExitCode = 1; // couldn't start stclient process
                    else
                    {
                        if ( _tcslen( szInstanceURN ) > 0 )
                        {
                            // don't register again if we registered in a previous run
                            // or we called stclient successfully.
                            if (( dwSTClientExitCode == ERR_DUP_RECORD ) ||
                                ( dwSTClientExitCode == 0 ))
                                dwExitCode = 0;
                            else
                                dwExitCode = 1; // other errors
                        }
                        else
                            dwExitCode = ( dwSTClientExitCode == 0 ) ? 0 : 1;
                    }
                }
                else if ( dwSTClientExitCode == 0 )
                    dwExitCode = 0; // already registered
                else
                    dwExitCode = 1; // other errors
            }
            else
                dwExitCode = 1; // couldn't start stclient
        }
        else
            dwExitCode = 1; // no executable found
    }
    else
        dwExitCode = 0; // wrong number of arguments

    return dwExitCode;
}

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