1 | /* $Id: init-win.cpp 57865 2015-09-23 01:42:40Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Init Ring-3, Windows Specific Code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_DEFAULT
|
---|
32 | #include <Windows.h>
|
---|
33 | #ifndef LOAD_LIBRARY_SEARCH_APPLICATION_DIR
|
---|
34 | # define LOAD_LIBRARY_SEARCH_APPLICATION_DIR 0x200
|
---|
35 | # define LOAD_LIBRARY_SEARCH_SYSTEM32 0x800
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | #include "internal-r3-win.h"
|
---|
39 | #include <iprt/initterm.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/err.h>
|
---|
42 | #include <iprt/string.h>
|
---|
43 | #include "../init.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | /*********************************************************************************************************************************
|
---|
47 | * Global Variables *
|
---|
48 | *********************************************************************************************************************************/
|
---|
49 | /** Windows DLL loader protection level. */
|
---|
50 | DECLHIDDEN(RTR3WINLDRPROT) g_enmWinLdrProt = RTR3WINLDRPROT_NONE;
|
---|
51 | /** Our simplified windows version. */
|
---|
52 | DECLHIDDEN(RTWINOSTYPE) g_enmWinVer = kRTWinOSType_UNKNOWN;
|
---|
53 | /** Extended windows version information. */
|
---|
54 | DECLHIDDEN(OSVERSIONINFOEXW) g_WinOsInfoEx;
|
---|
55 | /** The native kernel32.dll handle. */
|
---|
56 | DECLHIDDEN(HMODULE) g_hModKernel32 = NULL;
|
---|
57 | /** The native ntdll.dll handle. */
|
---|
58 | DECLHIDDEN(HMODULE) g_hModNtDll = NULL;
|
---|
59 | /** GetSystemWindowsDirectoryW or GetWindowsDirectoryW (NT4). */
|
---|
60 | DECLHIDDEN(PFNGETWINSYSDIR) g_pfnGetSystemWindowsDirectoryW = NULL;
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Translates OSVERSIONINOFEX into a Windows OS type.
|
---|
66 | *
|
---|
67 | * @returns The Windows OS type.
|
---|
68 | * @param pOSInfoEx The OS info returned by Windows.
|
---|
69 | *
|
---|
70 | * @remarks This table has been assembled from Usenet postings, personal
|
---|
71 | * observations, and reading other people's code. Please feel
|
---|
72 | * free to add to it or correct it.
|
---|
73 | * <pre>
|
---|
74 | dwPlatFormID dwMajorVersion dwMinorVersion dwBuildNumber
|
---|
75 | 95 1 4 0 950
|
---|
76 | 95 SP1 1 4 0 >950 && <=1080
|
---|
77 | 95 OSR2 1 4 <10 >1080
|
---|
78 | 98 1 4 10 1998
|
---|
79 | 98 SP1 1 4 10 >1998 && <2183
|
---|
80 | 98 SE 1 4 10 >=2183
|
---|
81 | ME 1 4 90 3000
|
---|
82 |
|
---|
83 | NT 3.51 2 3 51 1057
|
---|
84 | NT 4 2 4 0 1381
|
---|
85 | 2000 2 5 0 2195
|
---|
86 | XP 2 5 1 2600
|
---|
87 | 2003 2 5 2 3790
|
---|
88 | Vista 2 6 0
|
---|
89 |
|
---|
90 | CE 1.0 3 1 0
|
---|
91 | CE 2.0 3 2 0
|
---|
92 | CE 2.1 3 2 1
|
---|
93 | CE 3.0 3 3 0
|
---|
94 | </pre>
|
---|
95 | */
|
---|
96 | static RTWINOSTYPE rtR3InitWinSimplifiedVersion(OSVERSIONINFOEXW const *pOSInfoEx)
|
---|
97 | {
|
---|
98 | RTWINOSTYPE enmVer = kRTWinOSType_UNKNOWN;
|
---|
99 | BYTE const bProductType = pOSInfoEx->wProductType;
|
---|
100 | DWORD const dwPlatformId = pOSInfoEx->dwPlatformId;
|
---|
101 | DWORD const dwMinorVersion = pOSInfoEx->dwMinorVersion;
|
---|
102 | DWORD const dwMajorVersion = pOSInfoEx->dwMajorVersion;
|
---|
103 | DWORD const dwBuildNumber = pOSInfoEx->dwBuildNumber & 0xFFFF; /* Win 9x needs this. */
|
---|
104 |
|
---|
105 | if ( dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
|
---|
106 | && dwMajorVersion == 4)
|
---|
107 | {
|
---|
108 | if ( dwMinorVersion < 10
|
---|
109 | && dwBuildNumber == 950)
|
---|
110 | enmVer = kRTWinOSType_95;
|
---|
111 | else if ( dwMinorVersion < 10
|
---|
112 | && dwBuildNumber > 950
|
---|
113 | && dwBuildNumber <= 1080)
|
---|
114 | enmVer = kRTWinOSType_95SP1;
|
---|
115 | else if ( dwMinorVersion < 10
|
---|
116 | && dwBuildNumber > 1080)
|
---|
117 | enmVer = kRTWinOSType_95OSR2;
|
---|
118 | else if ( dwMinorVersion == 10
|
---|
119 | && dwBuildNumber == 1998)
|
---|
120 | enmVer = kRTWinOSType_98;
|
---|
121 | else if ( dwMinorVersion == 10
|
---|
122 | && dwBuildNumber > 1998
|
---|
123 | && dwBuildNumber < 2183)
|
---|
124 | enmVer = kRTWinOSType_98SP1;
|
---|
125 | else if ( dwMinorVersion == 10
|
---|
126 | && dwBuildNumber >= 2183)
|
---|
127 | enmVer = kRTWinOSType_98SE;
|
---|
128 | else if (dwMinorVersion == 90)
|
---|
129 | enmVer = kRTWinOSType_ME;
|
---|
130 | }
|
---|
131 | else if (dwPlatformId == VER_PLATFORM_WIN32_NT)
|
---|
132 | {
|
---|
133 | if ( dwMajorVersion == 3
|
---|
134 | && dwMinorVersion == 51)
|
---|
135 | enmVer = kRTWinOSType_NT351;
|
---|
136 | else if ( dwMajorVersion == 4
|
---|
137 | && dwMinorVersion == 0)
|
---|
138 | enmVer = kRTWinOSType_NT4;
|
---|
139 | else if ( dwMajorVersion == 5
|
---|
140 | && dwMinorVersion == 0)
|
---|
141 | enmVer = kRTWinOSType_2K;
|
---|
142 | else if ( dwMajorVersion == 5
|
---|
143 | && dwMinorVersion == 1)
|
---|
144 | enmVer = kRTWinOSType_XP;
|
---|
145 | else if ( dwMajorVersion == 5
|
---|
146 | && dwMinorVersion == 2)
|
---|
147 | enmVer = kRTWinOSType_2003;
|
---|
148 | else if ( dwMajorVersion == 6
|
---|
149 | && dwMinorVersion == 0)
|
---|
150 | {
|
---|
151 | if (bProductType != VER_NT_WORKSTATION)
|
---|
152 | enmVer = kRTWinOSType_2008;
|
---|
153 | else
|
---|
154 | enmVer = kRTWinOSType_VISTA;
|
---|
155 | }
|
---|
156 | else if ( dwMajorVersion == 6
|
---|
157 | && dwMinorVersion == 1)
|
---|
158 | enmVer = kRTWinOSType_7;
|
---|
159 | else if ( dwMajorVersion == 6
|
---|
160 | && dwMinorVersion == 2)
|
---|
161 | enmVer = kRTWinOSType_8;
|
---|
162 | else if ( dwMajorVersion == 6
|
---|
163 | && dwMinorVersion == 3)
|
---|
164 | enmVer = kRTWinOSType_81;
|
---|
165 | else if ( ( dwMajorVersion == 6
|
---|
166 | && dwMinorVersion == 4)
|
---|
167 | || ( dwMajorVersion == 10
|
---|
168 | && dwMinorVersion == 0))
|
---|
169 | enmVer = kRTWinOSType_10;
|
---|
170 | else
|
---|
171 | enmVer = kRTWinOSType_NT_UNKNOWN;
|
---|
172 | }
|
---|
173 |
|
---|
174 | return enmVer;
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Initializes the global variables related to windows version.
|
---|
180 | */
|
---|
181 | static void rtR3InitWindowsVersion(void)
|
---|
182 | {
|
---|
183 | Assert(g_hModNtDll != NULL);
|
---|
184 |
|
---|
185 | /*
|
---|
186 | * ASSUMES OSVERSIONINFOEX starts with the exact same layout as OSVERSIONINFO (safe).
|
---|
187 | */
|
---|
188 | AssertCompileMembersSameSizeAndOffset(OSVERSIONINFOEX, szCSDVersion, OSVERSIONINFO, szCSDVersion);
|
---|
189 | AssertCompileMemberOffset(OSVERSIONINFOEX, wServicePackMajor, sizeof(OSVERSIONINFO));
|
---|
190 |
|
---|
191 | /*
|
---|
192 | * Use the NT version of GetVersionExW so we don't get fooled by
|
---|
193 | * compatability shims.
|
---|
194 | */
|
---|
195 | RT_ZERO(g_WinOsInfoEx);
|
---|
196 | g_WinOsInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
|
---|
197 |
|
---|
198 | LONG (__stdcall *pfnRtlGetVersion)(OSVERSIONINFOEXW *);
|
---|
199 | *(FARPROC *)&pfnRtlGetVersion = GetProcAddress(g_hModNtDll, "RtlGetVersion");
|
---|
200 | LONG rcNt = -1;
|
---|
201 | if (pfnRtlGetVersion)
|
---|
202 | rcNt = pfnRtlGetVersion(&g_WinOsInfoEx);
|
---|
203 | if (rcNt != 0)
|
---|
204 | {
|
---|
205 | /*
|
---|
206 | * Couldn't find it or it failed, try the windows version of the API.
|
---|
207 | */
|
---|
208 | RT_ZERO(g_WinOsInfoEx);
|
---|
209 | g_WinOsInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
|
---|
210 | if (!GetVersionExW((POSVERSIONINFOW)&g_WinOsInfoEx))
|
---|
211 | {
|
---|
212 | /*
|
---|
213 | * If that didn't work either, just get the basic version bits.
|
---|
214 | */
|
---|
215 | RT_ZERO(g_WinOsInfoEx);
|
---|
216 | g_WinOsInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
|
---|
217 | if (GetVersionExW((POSVERSIONINFOW)&g_WinOsInfoEx))
|
---|
218 | Assert(g_WinOsInfoEx.dwPlatformId != VER_PLATFORM_WIN32_NT || g_WinOsInfoEx.dwMajorVersion < 5);
|
---|
219 | else
|
---|
220 | {
|
---|
221 | AssertBreakpoint();
|
---|
222 | RT_ZERO(g_WinOsInfoEx);
|
---|
223 | }
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | if (g_WinOsInfoEx.dwOSVersionInfoSize)
|
---|
228 | g_enmWinVer = rtR3InitWinSimplifiedVersion(&g_WinOsInfoEx);
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | static int rtR3InitNativeObtrusiveWorker(void)
|
---|
233 | {
|
---|
234 | /*
|
---|
235 | * Disable error popups.
|
---|
236 | */
|
---|
237 | UINT fOldErrMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
|
---|
238 | SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX | fOldErrMode);
|
---|
239 |
|
---|
240 | /*
|
---|
241 | * Restrict DLL searching for the process on windows versions which allow
|
---|
242 | * us to do so.
|
---|
243 | * - The first trick works on XP SP1+ and disables the searching of the
|
---|
244 | * current directory.
|
---|
245 | * - The second trick is W7 w/ KB2533623 and W8+, it restrict the DLL
|
---|
246 | * searching to the application directory and the System32 directory.
|
---|
247 | */
|
---|
248 | int rc = VINF_SUCCESS;
|
---|
249 |
|
---|
250 | typedef BOOL (WINAPI *PFNSETDLLDIRECTORY)(LPCWSTR);
|
---|
251 | PFNSETDLLDIRECTORY pfnSetDllDir = (PFNSETDLLDIRECTORY)GetProcAddress(g_hModKernel32, "SetDllDirectoryW");
|
---|
252 | if (pfnSetDllDir)
|
---|
253 | {
|
---|
254 | if (pfnSetDllDir(L""))
|
---|
255 | g_enmWinLdrProt = RTR3WINLDRPROT_NO_CWD;
|
---|
256 | else
|
---|
257 | rc = VERR_INTERNAL_ERROR_3;
|
---|
258 | }
|
---|
259 |
|
---|
260 | /** @bugref 6861: Observed GUI issues on Vista (32-bit and 64-bit). */
|
---|
261 | if (g_enmWinVer > kRTWinOSType_VISTA)
|
---|
262 | {
|
---|
263 | typedef BOOL(WINAPI *PFNSETDEFAULTDLLDIRECTORIES)(DWORD);
|
---|
264 | PFNSETDEFAULTDLLDIRECTORIES pfnSetDefDllDirs;
|
---|
265 | pfnSetDefDllDirs = (PFNSETDEFAULTDLLDIRECTORIES)GetProcAddress(g_hModKernel32, "SetDefaultDllDirectories");
|
---|
266 | if (pfnSetDefDllDirs)
|
---|
267 | {
|
---|
268 | if (pfnSetDefDllDirs(LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32))
|
---|
269 | g_enmWinLdrProt = RTR3WINLDRPROT_SAFE;
|
---|
270 | else if (RT_SUCCESS(rc))
|
---|
271 | rc = VERR_INTERNAL_ERROR_4;
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | return rc;
|
---|
276 | }
|
---|
277 |
|
---|
278 |
|
---|
279 | DECLHIDDEN(int) rtR3InitNativeFirst(uint32_t fFlags)
|
---|
280 | {
|
---|
281 | /*
|
---|
282 | * Make sure we've got the handles of the two main Windows NT dlls.
|
---|
283 | */
|
---|
284 | g_hModKernel32 = GetModuleHandleW(L"kernel32.dll");
|
---|
285 | if (g_hModKernel32 == NULL)
|
---|
286 | return VERR_INTERNAL_ERROR_2;
|
---|
287 | g_hModNtDll = GetModuleHandleW(L"ntdll.dll");
|
---|
288 | if (g_hModNtDll == NULL)
|
---|
289 | return VERR_INTERNAL_ERROR_2;
|
---|
290 |
|
---|
291 | rtR3InitWindowsVersion();
|
---|
292 |
|
---|
293 | int rc = VINF_SUCCESS;
|
---|
294 | if (!(fFlags & RTR3INIT_FLAGS_UNOBTRUSIVE))
|
---|
295 | rc = rtR3InitNativeObtrusiveWorker();
|
---|
296 |
|
---|
297 | /*
|
---|
298 | * Resolve some kernel32.dll APIs we may need but aren't necessarily
|
---|
299 | * present in older windows versions.
|
---|
300 | */
|
---|
301 | g_pfnGetSystemWindowsDirectoryW = (PFNGETWINSYSDIR)GetProcAddress(g_hModKernel32, "GetSystemWindowsDirectoryW");
|
---|
302 | if (g_pfnGetSystemWindowsDirectoryW)
|
---|
303 | g_pfnGetSystemWindowsDirectoryW = (PFNGETWINSYSDIR)GetProcAddress(g_hModKernel32, "GetWindowsDirectoryW");
|
---|
304 |
|
---|
305 | return rc;
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | DECLHIDDEN(void) rtR3InitNativeObtrusive(void)
|
---|
310 | {
|
---|
311 | rtR3InitNativeObtrusiveWorker();
|
---|
312 | }
|
---|
313 |
|
---|
314 |
|
---|
315 | DECLHIDDEN(int) rtR3InitNativeFinal(uint32_t fFlags)
|
---|
316 | {
|
---|
317 | /* Nothing to do here. */
|
---|
318 | return VINF_SUCCESS;
|
---|
319 | }
|
---|
320 |
|
---|