1 | /* $Id: VBoxWindowsAdditions.cpp 31634 2010-08-13 11:17:20Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxWindowsAdditions - The Windows Guest Additions Loader
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
8 | *
|
---|
9 | * Oracle Corporation confidential
|
---|
10 | * All rights reserved
|
---|
11 | */
|
---|
12 |
|
---|
13 | #ifndef UNICODE
|
---|
14 | #define UNICODE
|
---|
15 | #endif
|
---|
16 |
|
---|
17 | #include <windows.h>
|
---|
18 | #include <stdio.h>
|
---|
19 | #include <tchar.h>
|
---|
20 |
|
---|
21 | typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
|
---|
22 |
|
---|
23 | LPFN_ISWOW64PROCESS fnIsWow64Process;
|
---|
24 |
|
---|
25 | BOOL IsWow64 ()
|
---|
26 | {
|
---|
27 | BOOL bIsWow64 = FALSE;
|
---|
28 | fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
|
---|
29 |
|
---|
30 | if (NULL != fnIsWow64Process)
|
---|
31 | {
|
---|
32 | if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64))
|
---|
33 | {
|
---|
34 | _tprintf(_T("ERROR: Could not determine process type!\n"));
|
---|
35 |
|
---|
36 | /* Error in retrieving process type - assume that we're running on 32bit. */
|
---|
37 | return FALSE;
|
---|
38 | }
|
---|
39 | }
|
---|
40 | return bIsWow64;
|
---|
41 | }
|
---|
42 |
|
---|
43 | int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
|
---|
44 | {
|
---|
45 | int iRet = 0;
|
---|
46 |
|
---|
47 | LPWSTR *pszArgList = NULL;
|
---|
48 | int nArgs = 0;
|
---|
49 |
|
---|
50 | TCHAR szCurDir[_MAX_PATH + 1] = { 0 };
|
---|
51 | GetCurrentDirectory(_MAX_PATH, szCurDir);
|
---|
52 |
|
---|
53 | SHELLEXECUTEINFOW TempInfo = { 0 };
|
---|
54 | TempInfo.cbSize = sizeof(SHELLEXECUTEINFOW);
|
---|
55 |
|
---|
56 | TCHAR szModule[_MAX_PATH + 1] = { 0 };
|
---|
57 | TCHAR szApp[_MAX_PATH + 1] = { 0 };
|
---|
58 | TCHAR szProg[_MAX_PATH + 1] = { 0 };
|
---|
59 |
|
---|
60 | pszArgList = CommandLineToArgvW(GetCommandLineW(), &nArgs);
|
---|
61 | if (0 == GetModuleFileName(NULL, szModule, _MAX_PATH))
|
---|
62 | {
|
---|
63 | /* Module not found, use a default name. */
|
---|
64 | _stprintf(szModule, _T("%ws"), (pszArgList != NULL) ? pszArgList[0] : _T("VBoxWindowsAdditions.exe"));
|
---|
65 | }
|
---|
66 |
|
---|
67 | TCHAR* pcExt = wcsrchr(szModule, _T('.'));
|
---|
68 | if (NULL != pcExt)
|
---|
69 | wcsncpy(szApp, szModule, (pcExt - szModule));
|
---|
70 |
|
---|
71 | if (IsWow64()) /* 64bit Windows. */
|
---|
72 | {
|
---|
73 | _stprintf(szProg, _T("%ws-amd64.exe"), szApp);
|
---|
74 | }
|
---|
75 | else /* 32bit Windows. */
|
---|
76 | {
|
---|
77 | _stprintf(szProg, _T("%ws-x86.exe"), szApp);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /* Construct parameter list. */
|
---|
81 | TCHAR *pszParams = (TCHAR*)LocalAlloc(LPTR, _MAX_PATH*sizeof(TCHAR));
|
---|
82 | TCHAR szDelim = _T(' ');
|
---|
83 |
|
---|
84 | if (pszParams)
|
---|
85 | {
|
---|
86 | wcsncat(pszParams, szProg,
|
---|
87 | __min(wcslen(szProg),_MAX_PATH-wcslen(pszParams)));
|
---|
88 | wcsncat(pszParams, &szDelim,
|
---|
89 | __min(1,_MAX_PATH-wcslen(pszParams)));
|
---|
90 |
|
---|
91 | if (nArgs > 1)
|
---|
92 | {
|
---|
93 | for (int i=0; i<nArgs-1; i++)
|
---|
94 | {
|
---|
95 | if (i > 0)
|
---|
96 | {
|
---|
97 | wcsncat(pszParams, &szDelim,
|
---|
98 | __min(1,_MAX_PATH-wcslen(pszParams)));
|
---|
99 | }
|
---|
100 | wcsncat(pszParams, pszArgList[i+1],
|
---|
101 | __min(wcslen(pszArgList[i+1]),_MAX_PATH-wcslen(pszParams)));
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | /* Struct for ShellExecute. */
|
---|
107 | TempInfo.fMask = 0;
|
---|
108 | TempInfo.hwnd = NULL;
|
---|
109 | TempInfo.lpVerb =L"runas" ;
|
---|
110 | TempInfo.lpFile = szProg;
|
---|
111 | TempInfo.lpParameters = pszParams;
|
---|
112 | TempInfo.lpDirectory = szCurDir;
|
---|
113 | TempInfo.nShow = SW_NORMAL;
|
---|
114 |
|
---|
115 | STARTUPINFOW si;
|
---|
116 | PROCESS_INFORMATION pi;
|
---|
117 | DWORD dwRes = 0;
|
---|
118 |
|
---|
119 | ZeroMemory(&si, sizeof(si));
|
---|
120 | si.cb = sizeof(si);
|
---|
121 | ZeroMemory(&pi, sizeof(pi));
|
---|
122 |
|
---|
123 | dwRes = CreateProcessW(szProg, pszParams, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
|
---|
124 | if (!dwRes && GetLastError() == 740) /* 740 = ERROR_ELEVATION_REQUIRED -> Only for Windows Vista. */
|
---|
125 | {
|
---|
126 | if (FALSE == ::ShellExecuteExW(&TempInfo))
|
---|
127 | {
|
---|
128 | _tprintf (_T("ERROR: Could not launch program! Code: %ld\n"), GetLastError());
|
---|
129 | iRet = 1;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | if (pszParams)
|
---|
134 | LocalFree(pszParams);
|
---|
135 |
|
---|
136 | if (pszArgList)
|
---|
137 | LocalFree(pszArgList);
|
---|
138 |
|
---|
139 | return iRet;
|
---|
140 | }
|
---|
141 |
|
---|