VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/init-win.cpp@ 55384

Last change on this file since 55384 was 53009, checked in by vboxsync, 10 years ago

IPRT: Windows 10 OS type.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1/* $Id: init-win.cpp 53009 2014-10-09 12:16:37Z vboxsync $ */
2/** @file
3 * IPRT - Init Ring-3, Windows Specific Code.
4 */
5
6/*
7 * Copyright (C) 2006-2013 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. */
50DECLHIDDEN(RTR3WINLDRPROT) g_enmWinLdrProt = RTR3WINLDRPROT_NONE;
51/** Our simplified windows version. */
52DECLHIDDEN(RTWINOSTYPE) g_enmWinVer = kRTWinOSType_UNKNOWN;
53/** Extended windows version information. */
54DECLHIDDEN(OSVERSIONINFOEXW) g_WinOsInfoEx;
55/** The native kernel32.dll handle. */
56DECLHIDDEN(HMODULE) g_hModKernel32 = NULL;
57/** The native ntdll.dll handle. */
58DECLHIDDEN(HMODULE) g_hModNtDll = NULL;
59
60
61
62/**
63 * Translates OSVERSIONINOFEX into a Windows OS type.
64 *
65 * @returns The Windows OS type.
66 * @param pOSInfoEx The OS info returned by Windows.
67 *
68 * @remarks This table has been assembled from Usenet postings, personal
69 * observations, and reading other people's code. Please feel
70 * free to add to it or correct it.
71 * <pre>
72 dwPlatFormID dwMajorVersion dwMinorVersion dwBuildNumber
7395 1 4 0 950
7495 SP1 1 4 0 >950 && <=1080
7595 OSR2 1 4 <10 >1080
7698 1 4 10 1998
7798 SP1 1 4 10 >1998 && <2183
7898 SE 1 4 10 >=2183
79ME 1 4 90 3000
80
81NT 3.51 2 3 51 1057
82NT 4 2 4 0 1381
832000 2 5 0 2195
84XP 2 5 1 2600
852003 2 5 2 3790
86Vista 2 6 0
87
88CE 1.0 3 1 0
89CE 2.0 3 2 0
90CE 2.1 3 2 1
91CE 3.0 3 3 0
92</pre>
93 */
94static RTWINOSTYPE rtR3InitWinSimplifiedVersion(OSVERSIONINFOEXW const *pOSInfoEx)
95{
96 RTWINOSTYPE enmVer = kRTWinOSType_UNKNOWN;
97 BYTE const bProductType = pOSInfoEx->wProductType;
98 DWORD const dwPlatformId = pOSInfoEx->dwPlatformId;
99 DWORD const dwMinorVersion = pOSInfoEx->dwMinorVersion;
100 DWORD const dwMajorVersion = pOSInfoEx->dwMajorVersion;
101 DWORD const dwBuildNumber = pOSInfoEx->dwBuildNumber & 0xFFFF; /* Win 9x needs this. */
102
103 if ( dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
104 && dwMajorVersion == 4)
105 {
106 if ( dwMinorVersion < 10
107 && dwBuildNumber == 950)
108 enmVer = kRTWinOSType_95;
109 else if ( dwMinorVersion < 10
110 && dwBuildNumber > 950
111 && dwBuildNumber <= 1080)
112 enmVer = kRTWinOSType_95SP1;
113 else if ( dwMinorVersion < 10
114 && dwBuildNumber > 1080)
115 enmVer = kRTWinOSType_95OSR2;
116 else if ( dwMinorVersion == 10
117 && dwBuildNumber == 1998)
118 enmVer = kRTWinOSType_98;
119 else if ( dwMinorVersion == 10
120 && dwBuildNumber > 1998
121 && dwBuildNumber < 2183)
122 enmVer = kRTWinOSType_98SP1;
123 else if ( dwMinorVersion == 10
124 && dwBuildNumber >= 2183)
125 enmVer = kRTWinOSType_98SE;
126 else if (dwMinorVersion == 90)
127 enmVer = kRTWinOSType_ME;
128 }
129 else if (dwPlatformId == VER_PLATFORM_WIN32_NT)
130 {
131 if ( dwMajorVersion == 3
132 && dwMinorVersion == 51)
133 enmVer = kRTWinOSType_NT351;
134 else if ( dwMajorVersion == 4
135 && dwMinorVersion == 0)
136 enmVer = kRTWinOSType_NT4;
137 else if ( dwMajorVersion == 5
138 && dwMinorVersion == 0)
139 enmVer = kRTWinOSType_2K;
140 else if ( dwMajorVersion == 5
141 && dwMinorVersion == 1)
142 enmVer = kRTWinOSType_XP;
143 else if ( dwMajorVersion == 5
144 && dwMinorVersion == 2)
145 enmVer = kRTWinOSType_2003;
146 else if ( dwMajorVersion == 6
147 && dwMinorVersion == 0)
148 {
149 if (bProductType != VER_NT_WORKSTATION)
150 enmVer = kRTWinOSType_2008;
151 else
152 enmVer = kRTWinOSType_VISTA;
153 }
154 else if ( dwMajorVersion == 6
155 && dwMinorVersion == 1)
156 enmVer = kRTWinOSType_7;
157 else if ( dwMajorVersion == 6
158 && dwMinorVersion == 2)
159 enmVer = kRTWinOSType_8;
160 else if ( dwMajorVersion == 6
161 && dwMinorVersion == 3)
162 enmVer = kRTWinOSType_81;
163 else if ( dwMajorVersion == 6
164 && dwMinorVersion == 4)
165 enmVer = kRTWinOSType_10;
166 else
167 enmVer = kRTWinOSType_NT_UNKNOWN;
168 }
169
170 return enmVer;
171}
172
173
174/**
175 * Initializes the global variables related to windows version.
176 */
177static void rtR3InitWindowsVersion(void)
178{
179 Assert(g_hModNtDll != NULL);
180
181 /*
182 * ASSUMES OSVERSIONINFOEX starts with the exact same layout as OSVERSIONINFO (safe).
183 */
184 AssertCompileMembersSameSizeAndOffset(OSVERSIONINFOEX, szCSDVersion, OSVERSIONINFO, szCSDVersion);
185 AssertCompileMemberOffset(OSVERSIONINFOEX, wServicePackMajor, sizeof(OSVERSIONINFO));
186
187 /*
188 * Use the NT version of GetVersionExW so we don't get fooled by
189 * compatability shims.
190 */
191 RT_ZERO(g_WinOsInfoEx);
192 g_WinOsInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
193
194 LONG (__stdcall *pfnRtlGetVersion)(OSVERSIONINFOEXW *);
195 *(FARPROC *)&pfnRtlGetVersion = GetProcAddress(g_hModNtDll, "RtlGetVersion");
196 LONG rcNt = -1;
197 if (pfnRtlGetVersion)
198 rcNt = pfnRtlGetVersion(&g_WinOsInfoEx);
199 if (rcNt != 0)
200 {
201 /*
202 * Couldn't find it or it failed, try the windows version of the API.
203 */
204 RT_ZERO(g_WinOsInfoEx);
205 g_WinOsInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
206 if (!GetVersionExW((POSVERSIONINFOW)&g_WinOsInfoEx))
207 {
208 /*
209 * If that didn't work either, just get the basic version bits.
210 */
211 RT_ZERO(g_WinOsInfoEx);
212 g_WinOsInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
213 if (GetVersionExW((POSVERSIONINFOW)&g_WinOsInfoEx))
214 Assert(g_WinOsInfoEx.dwPlatformId != VER_PLATFORM_WIN32_NT || g_WinOsInfoEx.dwMajorVersion < 5);
215 else
216 {
217 AssertBreakpoint();
218 RT_ZERO(g_WinOsInfoEx);
219 }
220 }
221 }
222
223 if (g_WinOsInfoEx.dwOSVersionInfoSize)
224 g_enmWinVer = rtR3InitWinSimplifiedVersion(&g_WinOsInfoEx);
225}
226
227
228static int rtR3InitNativeObtrusiveWorker(void)
229{
230 /*
231 * Disable error popups.
232 */
233 UINT fOldErrMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
234 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX | fOldErrMode);
235
236 /*
237 * Restrict DLL searching for the process on windows versions which allow
238 * us to do so.
239 * - The first trick works on XP SP1+ and disables the searching of the
240 * current directory.
241 * - The second trick is W7 w/ KB2533623 and W8+, it restrict the DLL
242 * searching to the application directory and the System32 directory.
243 */
244 int rc = VINF_SUCCESS;
245
246 typedef BOOL (WINAPI *PFNSETDLLDIRECTORY)(LPCWSTR);
247 PFNSETDLLDIRECTORY pfnSetDllDir = (PFNSETDLLDIRECTORY)GetProcAddress(g_hModKernel32, "SetDllDirectoryW");
248 if (pfnSetDllDir)
249 {
250 if (pfnSetDllDir(L""))
251 g_enmWinLdrProt = RTR3WINLDRPROT_NO_CWD;
252 else
253 rc = VERR_INTERNAL_ERROR_3;
254 }
255
256 /** @bugref 6861: Observed GUI issues on Vista (32-bit and 64-bit). */
257 if (g_enmWinVer > kRTWinOSType_VISTA)
258 {
259 typedef BOOL(WINAPI *PFNSETDEFAULTDLLDIRECTORIES)(DWORD);
260 PFNSETDEFAULTDLLDIRECTORIES pfnSetDefDllDirs;
261 pfnSetDefDllDirs = (PFNSETDEFAULTDLLDIRECTORIES)GetProcAddress(g_hModKernel32, "SetDefaultDllDirectories");
262 if (pfnSetDefDllDirs)
263 {
264 if (pfnSetDefDllDirs(LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32))
265 g_enmWinLdrProt = RTR3WINLDRPROT_SAFE;
266 else if (RT_SUCCESS(rc))
267 rc = VERR_INTERNAL_ERROR_4;
268 }
269 }
270
271 return rc;
272}
273
274
275DECLHIDDEN(int) rtR3InitNativeFirst(uint32_t fFlags)
276{
277 /*
278 * Make sure we've got the handles of the two main Windows NT dlls.
279 */
280 g_hModKernel32 = GetModuleHandleW(L"kernel32.dll");
281 if (g_hModKernel32 == NULL)
282 return VERR_INTERNAL_ERROR_2;
283 g_hModNtDll = GetModuleHandleW(L"ntdll.dll");
284 if (g_hModNtDll == NULL)
285 return VERR_INTERNAL_ERROR_2;
286
287 rtR3InitWindowsVersion();
288
289 int rc = VINF_SUCCESS;
290 if (!(fFlags & RTR3INIT_FLAGS_UNOBTRUSIVE))
291 rc = rtR3InitNativeObtrusiveWorker();
292
293 return rc;
294}
295
296
297DECLHIDDEN(void) rtR3InitNativeObtrusive(void)
298{
299 rtR3InitNativeObtrusiveWorker();
300}
301
302
303DECLHIDDEN(int) rtR3InitNativeFinal(uint32_t fFlags)
304{
305 /* Nothing to do here. */
306 return VINF_SUCCESS;
307}
308
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette