Grab the thread object in case the thread terminates with pending
[wine] / misc / version.c
1 /*
2  * Windows and DOS version functions
3  *
4  * Copyright 1997 Alexandre Julliard
5  * Copyright 1997 Marcus Meissner
6  * Copyright 1998 Patrik Stridvall
7  * Copyright 1998 Andreas Mohr
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <string.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "winternl.h"
35 #include "winerror.h"
36 #include "wine/winbase16.h"
37 #include "module.h"
38 #include "wine/unicode.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(ver);
42
43 typedef enum
44 {
45     WIN20,   /* Windows 2.0 */
46     WIN30,   /* Windows 3.0 */
47     WIN31,   /* Windows 3.1 */
48     WIN95,   /* Windows 95 */
49     WIN98,   /* Windows 98 */
50     WINME,   /* Windows Me */
51     NT351,   /* Windows NT 3.51 */
52     NT40,    /* Windows NT 4.0 */
53     NT2K,    /* Windows 2000 */
54     WINXP,   /* Windows XP */
55     NB_WINDOWS_VERSIONS
56 } WINDOWS_VERSION;
57
58 typedef struct
59 {
60     LONG             getVersion16;
61     LONG             getVersion32;
62     OSVERSIONINFOEXA getVersionEx;
63 } VERSION_DATA;
64
65 /* FIXME: compare values below with original and fix.
66  * An *excellent* win9x version page (ALL versions !)
67  * can be found at members.aol.com/axcel216/ver.htm */
68 static VERSION_DATA VersionData[NB_WINDOWS_VERSIONS] =
69 {
70     /* WIN20 FIXME: verify values */
71     {
72         MAKELONG( 0x0002, 0x0303 ), /* assume DOS 3.3 */
73         MAKELONG( 0x0002, 0x8000 ),
74         {
75             sizeof(OSVERSIONINFOA), 2, 0, 0,
76             VER_PLATFORM_WIN32s, "Win32s 1.3",
77             0, 0, 0, 0, 0
78         }
79     },
80     /* WIN30 FIXME: verify values */
81     {
82         MAKELONG( 0x0003, 0x0500 ), /* assume DOS 5.00 */
83         MAKELONG( 0x0003, 0x8000 ),
84         {
85             sizeof(OSVERSIONINFOA), 3, 0, 0,
86             VER_PLATFORM_WIN32s, "Win32s 1.3",
87             0, 0, 0, 0, 0
88         }
89     },
90     /* WIN31 */
91     {
92         MAKELONG( 0x0a03, 0x0616 ), /* DOS 6.22 */
93         MAKELONG( 0x0a03, 0x8000 ),
94         {
95             sizeof(OSVERSIONINFOA), 3, 10, 0,
96             VER_PLATFORM_WIN32s, "Win32s 1.3",
97             0, 0, 0, 0, 0
98         }
99     },
100     /* WIN95 */
101     {
102         0x07005F03,
103         0xC0000004,
104         {
105             /* Win95:       4, 0, 0x40003B6, ""
106              * Win95sp1:    4, 0, 0x40003B6, " A " (according to doc)
107              * Win95osr2:   4, 0, 0x4000457, " B " (according to doc)
108              * Win95osr2.1: 4, 3, 0x40304BC, " B " (according to doc)
109              * Win95osr2.5: 4, 3, 0x40304BE, " C " (according to doc)
110              * Win95a/b can be discerned via regkey SubVersionNumber
111              * See also:
112              * http://support.microsoft.com/support/kb/articles/q158/2/38.asp
113              */
114             sizeof(OSVERSIONINFOA), 4, 0, 0x40003B6,
115             VER_PLATFORM_WIN32_WINDOWS, "",
116             0, 0, 0, 0, 0
117         }
118     },
119     /* WIN98 (second edition) */
120     {
121         0x070A5F03,
122         0xC0000A04,
123         {
124             /* Win98:   4, 10, 0x40A07CE, " "   4.10.1998
125              * Win98SE: 4, 10, 0x40A08AE, " A " 4.10.2222
126              */
127             sizeof(OSVERSIONINFOA), 4, 10, 0x40A08AE,
128             VER_PLATFORM_WIN32_WINDOWS, " A ",
129             0, 0, 0, 0, 0
130         }
131     },
132     /* WINME */
133     {
134         0x08005F03,
135         0xC0005A04,
136         {
137             sizeof(OSVERSIONINFOA), 4, 90, 0x45A0BB8,
138             VER_PLATFORM_WIN32_WINDOWS, " ",
139             0, 0, 0, 0, 0
140         }
141     },
142     /* NT351 */
143     {
144         0x05000A03,
145         0x04213303,
146         {
147             sizeof(OSVERSIONINFOA), 3, 51, 0x421,
148             VER_PLATFORM_WIN32_NT, "Service Pack 2",
149             0, 0, 0, 0, 0
150         }
151     },
152     /* NT40 */
153     {
154         0x05000A03,
155         0x05650004,
156         {
157             sizeof(OSVERSIONINFOA), 4, 0, 0x565,
158             VER_PLATFORM_WIN32_NT, "Service Pack 6",
159             6, 0, 0, VER_NT_WORKSTATION, 0
160         }
161     },
162     /* NT2K */
163     {
164         0x05005F03,
165         0x08930005,
166         {
167             sizeof(OSVERSIONINFOA), 5, 0, 0x893,
168             VER_PLATFORM_WIN32_NT, "Service Pack 2",
169             2, 0, 0, VER_NT_WORKSTATION, 30 /* FIXME: Great, a reserved field with a value! */
170         }
171     },
172     /* WINXP */
173     {
174         0x05005F03, /* Assuming DOS 5 like the other NT */
175         0x0A280105,
176         {
177             sizeof(OSVERSIONINFOA), 5, 1, 0xA28,
178             VER_PLATFORM_WIN32_NT, "",
179             0, 0, 0, VER_NT_WORKSTATION, 0 /* FIXME: Verify last 5 values */
180         }
181     }
182 };
183
184 static const char *WinVersionNames[NB_WINDOWS_VERSIONS] =
185 { /* no spaces in here ! */
186     "win20",
187     "win30",
188     "win31",
189     "win95",
190     "win98",
191     "winme",
192     "nt351",
193     "nt40",
194     "win2000,win2k,nt2k,nt2000",
195     "winxp"
196 };
197
198 /* if one of the following dlls is importing ntdll the windows
199 version autodetection switches wine to unicode (nt 3.51 or 4.0) */
200 static char * special_dlls[] =
201 {
202         "comdlg32.dll",
203         "comctl32.dll",
204         "shell32.dll",
205         "ole32.dll",
206         "rpcrt4.dll",
207         NULL
208 };
209
210 /* the current version has not been autodetected but forced via cmdline */
211 static BOOL versionForced = FALSE;
212 static WINDOWS_VERSION forcedWinVersion = WIN31; /* init value irrelevant */
213
214 /**********************************************************************
215  *         VERSION_ParseWinVersion
216  */
217 static void VERSION_ParseWinVersion( const char *arg )
218 {
219     int i, len;
220     const char *pCurr, *p;
221     for (i = 0; i < NB_WINDOWS_VERSIONS; i++)
222     {
223         pCurr = WinVersionNames[i];
224         /* iterate through all winver aliases separated by comma */
225         do {
226             p = strchr(pCurr, ',');
227             len = p ? (int)p - (int)pCurr : strlen(pCurr);
228             if ( (!strncmp( pCurr, arg, len )) && (arg[len] == '\0') )
229             {
230                 forcedWinVersion = (WINDOWS_VERSION)i;
231                 versionForced = TRUE;
232                 return;
233             }
234             pCurr = p+1;
235         } while (p);
236     }
237     MESSAGE("Invalid Windows version value '%s' specified in config file.\n", arg );
238     MESSAGE("Valid versions are:" );
239     for (i = 0; i < NB_WINDOWS_VERSIONS; i++)
240     {
241         /* only list the first, "official" alias in case of aliases */
242         pCurr = WinVersionNames[i];
243         p = strchr(pCurr, ',');
244         len = (p) ? (int)p - (int)pCurr : strlen(pCurr);
245
246         MESSAGE(" '%.*s'%c", len, pCurr,
247                 (i == NB_WINDOWS_VERSIONS - 1) ? '\n' : ',' );
248     }
249     ExitProcess(1);
250 }
251
252
253 /**********************************************************************
254  *         VERSION_ParseDosVersion
255  */
256 static void VERSION_ParseDosVersion( const char *arg )
257 {
258     int hi, lo;
259     if (sscanf( arg, "%d.%d", &hi, &lo ) == 2)
260     {
261         VersionData[WIN31].getVersion16 =
262             MAKELONG(LOWORD(VersionData[WIN31].getVersion16),
263                      (hi<<8) + lo);
264     }
265     else
266     {
267         MESSAGE("Wrong format for DOS version in config file. Use \"x.xx\"\n");
268         ExitProcess(1);
269     }
270 }
271
272
273 /**********************************************************************
274  *         VERSION_ParseVersion
275  *
276  * Parse the contents of the Version key.
277  */
278 static void VERSION_ParseVersion( HKEY hkey, BOOL *got_win_ver, BOOL *got_dos_ver )
279 {
280     static const WCHAR WindowsW[] = {'W','i','n','d','o','w','s',0};
281     static const WCHAR DosW[] = {'D','O','S',0};
282
283     UNICODE_STRING valueW;
284     char tmp[64], buffer[50];
285     KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)tmp;
286     DWORD count, len;
287
288     if (!*got_win_ver)
289     {
290         RtlInitUnicodeString( &valueW, WindowsW );
291         if (!NtQueryValueKey( hkey, &valueW, KeyValuePartialInformation, tmp, sizeof(tmp), &count ))
292         {
293             RtlUnicodeToMultiByteN( buffer, sizeof(buffer)-1, &len,
294                                     (WCHAR *)info->Data, info->DataLength );
295             buffer[len] = 0;
296             VERSION_ParseWinVersion( buffer );
297             TRACE( "got win version %s\n", WinVersionNames[forcedWinVersion] );
298             *got_win_ver = TRUE;
299         }
300     }
301     if (!*got_dos_ver)
302     {
303         RtlInitUnicodeString( &valueW, DosW );
304         if (!NtQueryValueKey( hkey, &valueW, KeyValuePartialInformation, tmp, sizeof(tmp), &count ))
305         {
306             RtlUnicodeToMultiByteN( buffer, sizeof(buffer)-1, &len,
307                                     (WCHAR *)info->Data, info->DataLength );
308             buffer[len] = 0;
309             VERSION_ParseDosVersion( buffer );
310             TRACE( "got dos version %lx\n", VersionData[WIN31].getVersion16 );
311             *got_dos_ver = TRUE;
312         }
313     }
314 }
315
316
317 /**********************************************************************
318  *         VERSION_Init
319  */
320 static void VERSION_Init(void)
321 {
322     OBJECT_ATTRIBUTES attr;
323     UNICODE_STRING nameW;
324     HKEY hkey, config_key;
325     BOOL got_win_ver = FALSE, got_dos_ver = FALSE;
326     WCHAR buffer[MAX_PATH], appversion[MAX_PATH+20], *appname, *p;
327     static BOOL init_done;
328     static const WCHAR configW[] = {'M','a','c','h','i','n','e','\\',
329                                     'S','o','f','t','w','a','r','e','\\',
330                                     'W','i','n','e','\\',
331                                     'W','i','n','e','\\',
332                                     'C','o','n','f','i','g',0};
333     static const WCHAR appdefaultsW[] = {'A','p','p','D','e','f','a','u','l','t','s','\\',0};
334     static const WCHAR versionW[] = {'\\','V','e','r','s','i','o','n',0};
335
336     if (init_done) return;
337     if (!GetModuleFileNameW( 0, buffer, MAX_PATH ))
338     {
339         WARN( "could not get module file name\n" );
340         return;
341     }
342     init_done = TRUE;
343     appname = buffer;
344     if ((p = strrchrW( appname, '/' ))) appname = p + 1;
345     if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
346
347     attr.Length = sizeof(attr);
348     attr.RootDirectory = 0;
349     attr.ObjectName = &nameW;
350     attr.Attributes = 0;
351     attr.SecurityDescriptor = NULL;
352     attr.SecurityQualityOfService = NULL;
353     RtlInitUnicodeString( &nameW, configW );
354
355     if (NtOpenKey( &config_key, KEY_ALL_ACCESS, &attr )) return;
356     attr.RootDirectory = config_key;
357
358     /* open AppDefaults\\appname\\Version key */
359
360     strcpyW( appversion, appdefaultsW );
361     strcatW( appversion, appname );
362     strcatW( appversion, versionW );
363     RtlInitUnicodeString( &nameW, appversion );
364
365     if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
366     {
367         VERSION_ParseVersion( hkey, &got_win_ver, &got_dos_ver );
368         NtClose( hkey );
369     }
370
371     if (got_win_ver && got_dos_ver) goto done;
372
373     RtlInitUnicodeString( &nameW, versionW + 1 );
374     if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
375     {
376         VERSION_ParseVersion( hkey, &got_win_ver, &got_dos_ver );
377         NtClose( hkey );
378     }
379
380  done:
381     NtClose( config_key );
382 }
383
384
385 /**********************************************************************
386  *      VERSION_GetSystemDLLVersion
387  *
388  * This function tries to figure out if a given (native) dll comes from
389  * win95/98 or winnt. Since all values in the OptionalHeader are not a
390  * usable hint, we test if a dll imports the ntdll.
391  * This is at least working for all system dlls like comctl32, comdlg32 and
392  * shell32.
393  * If you have a better idea to figure this out...
394  */
395 static DWORD VERSION_GetSystemDLLVersion( HMODULE hmod )
396 {
397     DWORD size;
398     IMAGE_IMPORT_DESCRIPTOR *pe_imp;
399
400     if ((pe_imp = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &size )))
401     {
402         for ( ; pe_imp->Name; pe_imp++)
403         {
404             char * name = (char *)hmod + (unsigned int)pe_imp->Name;
405             TRACE("%s\n", name);
406
407             if (!strncasecmp(name, "ntdll", 5))
408             {
409               switch(RtlImageNtHeader(hmod)->OptionalHeader.MajorOperatingSystemVersion) {
410                   case 3:
411                           MESSAGE("WARNING: very old native DLL (NT 3.x) used, might cause instability.\n");
412                           return NT351;
413                   case 4: return NT40;
414                   case 5: return NT2K;
415                   case 6: return WINXP;
416                   default:
417                           FIXME("Unknown DLL OS version, please report !!\n");
418                           return WINXP;
419               }
420             }
421         }
422     }
423     return WIN95;
424 }
425 /**********************************************************************
426  *      VERSION_GetLinkedDllVersion
427  *
428  * Some version data (not reliable!):
429  * linker/OS/image/subsys
430  *
431  * x.xx/1.00/0.00/3.10  Win32s          (any version ?)
432  * 2.39/1.00/0.00/3.10  Win32s          freecell.exe (any version)
433  * 2.50/1.00/4.00/4.00  Win32s 1.30     winhlp32.exe
434  * 2.60/3.51/3.51/3.51  NT351SP5        system dlls
435  * 2.60/3.51/3.51/4.00  NT351SP5        comctl32 dll
436  * 2.xx/1.00/0.00/4.00  Win95           system files
437  * x.xx/4.00/0.00/4.00  Win95           most applications
438  * 3.10/4.00/0.00/4.00  Win98           notepad
439  * x.xx/5.00/5.00/4.00  Win98           system dlls (e.g. comctl32.dll)
440  * x.xx/4.00/4.00/4.00  NT 4            most apps
441  * 5.12/5.00/5.00/4.00  NT4+IE5         comctl32.dll
442  * 5.12/5.00/5.00/4.00  Win98           calc
443  * x.xx/5.00/5.00/4.00  win95/win98/NT4 IE5 files
444  */
445 static DWORD VERSION_GetLinkedDllVersion(void)
446 {
447         WINE_MODREF *wm;
448         DWORD WinVersion = NB_WINDOWS_VERSIONS;
449         PIMAGE_OPTIONAL_HEADER ophd;
450         IMAGE_NT_HEADERS *nt;
451
452         /* First check the native dlls provided. These have to be
453         from one windows version */
454         for ( wm = MODULE_modref_list; wm; wm=wm->next )
455         {
456           nt = RtlImageNtHeader(wm->module);
457           ophd = &nt->OptionalHeader;
458
459           TRACE("%s: %02x.%02x/%02x.%02x/%02x.%02x/%02x.%02x\n",
460             wm->modname,
461             ophd->MajorLinkerVersion, ophd->MinorLinkerVersion,
462             ophd->MajorOperatingSystemVersion, ophd->MinorOperatingSystemVersion,
463             ophd->MajorImageVersion, ophd->MinorImageVersion,
464             ophd->MajorSubsystemVersion, ophd->MinorSubsystemVersion);
465
466           /* test if it is an external (native) dll */
467           if (!(wm->flags & WINE_MODREF_INTERNAL))
468           {
469             int i;
470             for (i = 0; special_dlls[i]; i++)
471             {
472               /* test if it is a special dll */
473               if (!strcasecmp(wm->modname, special_dlls[i]))
474               {
475                 DWORD DllVersion = VERSION_GetSystemDLLVersion(wm->module);
476                 if (WinVersion == NB_WINDOWS_VERSIONS)
477                   WinVersion = DllVersion;
478                 else {
479                   if (WinVersion != DllVersion) {
480                     ERR("You mixed system DLLs from different windows versions! Expect a crash! (%s: expected version '%s', but is '%s')\n",
481                         wm->modname,
482                         VersionData[WinVersion].getVersionEx.szCSDVersion,
483                         VersionData[DllVersion].getVersionEx.szCSDVersion);
484                     return WIN20; /* this may let the exe exiting */
485                   }
486                 }
487                 break;
488               }
489             }
490           }
491         }
492
493         if(WinVersion != NB_WINDOWS_VERSIONS) return WinVersion;
494
495         /* we are using no external system dlls, look at the exe */
496         nt = RtlImageNtHeader(GetModuleHandleA(NULL));
497         ophd = &nt->OptionalHeader;
498
499         TRACE("%02x.%02x/%02x.%02x/%02x.%02x/%02x.%02x\n",
500             ophd->MajorLinkerVersion, ophd->MinorLinkerVersion,
501             ophd->MajorOperatingSystemVersion, ophd->MinorOperatingSystemVersion,
502             ophd->MajorImageVersion, ophd->MinorImageVersion,
503             ophd->MajorSubsystemVersion, ophd->MinorSubsystemVersion);
504
505         /* special nt 3.51 */
506         if (3 == ophd->MajorOperatingSystemVersion && 51 == ophd->MinorOperatingSystemVersion)
507         {
508             return NT351;
509         }
510
511         /* the MajorSubsystemVersion is the only usable sign */
512         if (ophd->MajorSubsystemVersion < 4)
513         {
514           if ( ophd->MajorOperatingSystemVersion == 1
515             && ophd->MinorOperatingSystemVersion == 0)
516           {
517             return WIN31; /* win32s */
518           }
519
520           if (ophd->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
521             return NT351; /* FIXME: NT 3.1, not tested */
522           else
523             return WIN95;
524         }
525
526         return WIN95;
527 }
528
529 /**********************************************************************
530  *         VERSION_GetVersion
531  *
532  * WARNING !!!
533  * Don't call this function too early during the Wine init,
534  * as pdb->exe_modref (required by VERSION_GetImageVersion()) might still
535  * be NULL in such cases, which causes the winver to ALWAYS be detected
536  * as WIN31.
537  * And as we cache the winver once it has been determined, this is bad.
538  * This can happen much easier than you might think, as this function
539  * is called by EVERY GetVersion*() API !
540  *
541  */
542 static WINDOWS_VERSION VERSION_GetVersion(void)
543 {
544     static WORD winver = 0xffff;
545
546     if (winver == 0xffff) /* to be determined */
547     {
548         WINDOWS_VERSION retver;
549
550         VERSION_Init();
551         if (versionForced) /* user has overridden any sensible checks */
552             winver = forcedWinVersion;
553         else
554         {
555             retver = VERSION_GetLinkedDllVersion();
556
557             /* cache determined value, but do not store in case of WIN31 */
558             if (retver != WIN31) winver = retver;
559
560             return retver;
561         }
562     }
563
564     return winver;
565 }
566
567
568 /***********************************************************************
569  *         GetVersion   (KERNEL.3)
570  */
571 LONG WINAPI GetVersion16(void)
572 {
573     WINDOWS_VERSION ver = VERSION_GetVersion();
574     return VersionData[ver].getVersion16;
575 }
576
577
578 /***********************************************************************
579  *         GetVersion   (KERNEL32.@)
580  */
581 LONG WINAPI GetVersion(void)
582 {
583     WINDOWS_VERSION ver = VERSION_GetVersion();
584     return VersionData[ver].getVersion32;
585 }
586
587
588 /***********************************************************************
589  *         GetVersionEx   (KERNEL.149)
590  */
591 BOOL16 WINAPI GetVersionEx16(OSVERSIONINFO16 *v)
592 {
593     WINDOWS_VERSION ver = VERSION_GetVersion();
594     if (v->dwOSVersionInfoSize < sizeof(OSVERSIONINFO16))
595     {
596         WARN("wrong OSVERSIONINFO size from app\n");
597         SetLastError(ERROR_INSUFFICIENT_BUFFER);
598         return FALSE;
599     }
600     v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
601     v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
602     v->dwBuildNumber  = VersionData[ver].getVersionEx.dwBuildNumber;
603     v->dwPlatformId   = VersionData[ver].getVersionEx.dwPlatformId;
604     strcpy( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
605     return TRUE;
606 }
607
608
609 /***********************************************************************
610  *         GetVersionExA   (KERNEL32.@)
611  */
612 BOOL WINAPI GetVersionExA(OSVERSIONINFOA *v)
613 {
614     WINDOWS_VERSION ver = VERSION_GetVersion();
615     LPOSVERSIONINFOEXA vex;
616
617     if (v->dwOSVersionInfoSize != sizeof(OSVERSIONINFOA) &&
618         v->dwOSVersionInfoSize != sizeof(OSVERSIONINFOEXA))
619     {
620         WARN("wrong OSVERSIONINFO size from app (got: %ld, expected: %d or %d)\n",
621                         v->dwOSVersionInfoSize, sizeof(OSVERSIONINFOA),
622                         sizeof(OSVERSIONINFOEXA));
623         SetLastError(ERROR_INSUFFICIENT_BUFFER);
624         return FALSE;
625     }
626     v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
627     v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
628     v->dwBuildNumber  = VersionData[ver].getVersionEx.dwBuildNumber;
629     v->dwPlatformId   = VersionData[ver].getVersionEx.dwPlatformId;
630     strcpy( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
631     if(v->dwOSVersionInfoSize == sizeof(OSVERSIONINFOEXA)) {
632         vex = (LPOSVERSIONINFOEXA) v;
633         vex->wServicePackMajor = VersionData[ver].getVersionEx.wServicePackMajor;
634         vex->wServicePackMinor = VersionData[ver].getVersionEx.wServicePackMinor;
635         vex->wSuiteMask = VersionData[ver].getVersionEx.wSuiteMask;
636         vex->wProductType = VersionData[ver].getVersionEx.wProductType;
637     }
638     return TRUE;
639 }
640
641
642 /***********************************************************************
643  *         GetVersionExW   (KERNEL32.@)
644  */
645 BOOL WINAPI GetVersionExW(OSVERSIONINFOW *v)
646 {
647     WINDOWS_VERSION ver = VERSION_GetVersion();
648     LPOSVERSIONINFOEXW vex;
649
650     if (v->dwOSVersionInfoSize != sizeof(OSVERSIONINFOW) &&
651         v->dwOSVersionInfoSize != sizeof(OSVERSIONINFOEXW))
652     {
653         WARN("wrong OSVERSIONINFO size from app (got: %ld, expected: %d or %d)\n",
654                         v->dwOSVersionInfoSize, sizeof(OSVERSIONINFOW),
655                         sizeof(OSVERSIONINFOEXW));
656         SetLastError(ERROR_INSUFFICIENT_BUFFER);
657         return FALSE;
658     }
659     v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
660     v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
661     v->dwBuildNumber  = VersionData[ver].getVersionEx.dwBuildNumber;
662     v->dwPlatformId   = VersionData[ver].getVersionEx.dwPlatformId;
663     MultiByteToWideChar( CP_ACP, 0, VersionData[ver].getVersionEx.szCSDVersion, -1,
664                          v->szCSDVersion, sizeof(v->szCSDVersion)/sizeof(WCHAR) );
665     if(v->dwOSVersionInfoSize == sizeof(OSVERSIONINFOEXW)) {
666         vex = (LPOSVERSIONINFOEXW) v;
667         vex->wServicePackMajor = VersionData[ver].getVersionEx.wServicePackMajor;
668         vex->wServicePackMinor = VersionData[ver].getVersionEx.wServicePackMinor;
669         vex->wSuiteMask = VersionData[ver].getVersionEx.wSuiteMask;
670         vex->wProductType = VersionData[ver].getVersionEx.wProductType;
671     }
672     return TRUE;
673 }
674
675
676 /******************************************************************************
677  *        VerifyVersionInfoA   (KERNEL32.@)
678  */
679 BOOL WINAPI VerifyVersionInfoA( LPOSVERSIONINFOEXA lpVersionInfo, DWORD dwTypeMask,
680                                 DWORDLONG dwlConditionMask)
681 {
682     OSVERSIONINFOEXW verW;
683
684     verW.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
685     verW.dwMajorVersion = lpVersionInfo->dwMajorVersion;
686     verW.dwMinorVersion = lpVersionInfo->dwMinorVersion;
687     verW.dwBuildNumber = lpVersionInfo->dwBuildNumber;
688     verW.dwPlatformId = lpVersionInfo->dwPlatformId;
689     verW.wServicePackMajor = lpVersionInfo->wServicePackMajor;
690     verW.wServicePackMinor = lpVersionInfo->wServicePackMinor;
691     verW.wSuiteMask = lpVersionInfo->wSuiteMask;
692     verW.wProductType = lpVersionInfo->wProductType;
693     verW.wReserved = lpVersionInfo->wReserved;
694
695     return VerifyVersionInfoW(&verW, dwTypeMask, dwlConditionMask);
696 }
697
698
699 /******************************************************************************
700  *        VerifyVersionInfoW   (KERNEL32.@)
701  */
702 BOOL WINAPI VerifyVersionInfoW( LPOSVERSIONINFOEXW lpVersionInfo, DWORD dwTypeMask,
703                                 DWORDLONG dwlConditionMask)
704 {
705     OSVERSIONINFOEXW ver;
706     BOOL res, error_set;
707
708     FIXME("(%p,%lu,%llx): Not all cases correctly implemented yet\n", lpVersionInfo, dwTypeMask, dwlConditionMask);
709     /* FIXME:
710         - Check the following special case on Windows (various versions):
711           o lp->wSuiteMask == 0 and ver.wSuiteMask != 0 and VER_AND/VER_OR
712           o lp->dwOSVersionInfoSize != sizeof(OSVERSIONINFOEXW)
713         - MSDN talks about some tests being impossible. Check what really happens.
714      */
715
716     ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
717     if(!GetVersionExW((LPOSVERSIONINFOW) &ver))
718         return FALSE;
719
720     res = TRUE;
721     error_set = FALSE;
722     if(!(dwTypeMask && dwlConditionMask)) {
723         res = FALSE;
724         SetLastError(ERROR_BAD_ARGUMENTS);
725         error_set = TRUE;
726     }
727     if(dwTypeMask & VER_PRODUCT_TYPE)
728         switch(dwlConditionMask >> 7*3 & 0x07) {
729             case VER_EQUAL:
730                 if(ver.wProductType != lpVersionInfo->wProductType)
731                     res = FALSE;
732                 break;
733             case VER_GREATER:
734                 if(ver.wProductType <= lpVersionInfo->wProductType)
735                     res = FALSE;
736                 break;
737             case VER_GREATER_EQUAL:
738                 if(ver.wProductType < lpVersionInfo->wProductType)
739                     res = FALSE;
740                 break;
741             case VER_LESS:
742                 if(ver.wProductType >= lpVersionInfo->wProductType)
743                     res = FALSE;
744                 break;
745             case VER_LESS_EQUAL:
746                 if(ver.wProductType > lpVersionInfo->wProductType)
747                     res = FALSE;
748                 break;
749             default:
750                 res = FALSE;
751                 SetLastError(ERROR_BAD_ARGUMENTS);
752                 error_set = TRUE;
753         }
754     if(dwTypeMask & VER_SUITENAME && res)
755         switch(dwlConditionMask >> 6*3 & 0x07) {
756             case VER_AND:
757                 if((lpVersionInfo->wSuiteMask & ver.wSuiteMask) != lpVersionInfo->wSuiteMask)
758                     res = FALSE;
759                 break;
760             case VER_OR:
761                 if(!(lpVersionInfo->wSuiteMask & ver.wSuiteMask) && lpVersionInfo->wSuiteMask)
762                     res = FALSE;
763                 break;
764             default:
765                 res = FALSE;
766                 SetLastError(ERROR_BAD_ARGUMENTS);
767                 error_set = TRUE;
768         }
769     if(dwTypeMask & VER_PLATFORMID && res)
770         switch(dwlConditionMask >> 3*3 & 0x07) {
771             case VER_EQUAL:
772                 if(ver.dwPlatformId != lpVersionInfo->dwPlatformId)
773                     res = FALSE;
774                 break;
775             case VER_GREATER:
776                 if(ver.dwPlatformId <= lpVersionInfo->dwPlatformId)
777                     res = FALSE;
778                 break;
779             case VER_GREATER_EQUAL:
780                 if(ver.dwPlatformId < lpVersionInfo->dwPlatformId)
781                     res = FALSE;
782                 break;
783             case VER_LESS:
784                 if(ver.dwPlatformId >= lpVersionInfo->dwPlatformId)
785                     res = FALSE;
786                 break;
787             case VER_LESS_EQUAL:
788                 if(ver.dwPlatformId > lpVersionInfo->dwPlatformId)
789                     res = FALSE;
790                 break;
791             default:
792                 res = FALSE;
793                 SetLastError(ERROR_BAD_ARGUMENTS);
794                 error_set = TRUE;
795         }
796     if(dwTypeMask & VER_BUILDNUMBER && res)
797         switch(dwlConditionMask >> 2*3 & 0x07) {
798             case VER_EQUAL:
799                 if(ver.dwBuildNumber != lpVersionInfo->dwBuildNumber)
800                     res = FALSE;
801                 break;
802             case VER_GREATER:
803                 if(ver.dwBuildNumber <= lpVersionInfo->dwBuildNumber)
804                     res = FALSE;
805                 break;
806             case VER_GREATER_EQUAL:
807                 if(ver.dwBuildNumber < lpVersionInfo->dwBuildNumber)
808                     res = FALSE;
809                 break;
810             case VER_LESS:
811                 if(ver.dwBuildNumber >= lpVersionInfo->dwBuildNumber)
812                     res = FALSE;
813                 break;
814             case VER_LESS_EQUAL:
815                 if(ver.dwBuildNumber > lpVersionInfo->dwBuildNumber)
816                     res = FALSE;
817                 break;
818             default:
819                 res = FALSE;
820                 SetLastError(ERROR_BAD_ARGUMENTS);
821                 error_set = TRUE;
822         }
823     if(dwTypeMask & VER_MAJORVERSION && res)
824         switch(dwlConditionMask >> 1*3 & 0x07) {
825             case VER_EQUAL:
826                 if(ver.dwMajorVersion != lpVersionInfo->dwMajorVersion)
827                     res = FALSE;
828                 break;
829             case VER_GREATER:
830                 if(ver.dwMajorVersion <= lpVersionInfo->dwMajorVersion)
831                     res = FALSE;
832                 break;
833             case VER_GREATER_EQUAL:
834                 if(ver.dwMajorVersion < lpVersionInfo->dwMajorVersion)
835                     res = FALSE;
836                 break;
837             case VER_LESS:
838                 if(ver.dwMajorVersion >= lpVersionInfo->dwMajorVersion)
839                     res = FALSE;
840                 break;
841             case VER_LESS_EQUAL:
842                 if(ver.dwMajorVersion > lpVersionInfo->dwMajorVersion)
843                     res = FALSE;
844                 break;
845             default:
846                 res = FALSE;
847                 SetLastError(ERROR_BAD_ARGUMENTS);
848                 error_set = TRUE;
849         }
850     if(dwTypeMask & VER_MINORVERSION && res)
851         switch(dwlConditionMask >> 0*3 & 0x07) {
852             case VER_EQUAL:
853                 if(ver.dwMinorVersion != lpVersionInfo->dwMinorVersion)
854                     res = FALSE;
855                 break;
856             case VER_GREATER:
857                 if(ver.dwMinorVersion <= lpVersionInfo->dwMinorVersion)
858                     res = FALSE;
859                 break;
860             case VER_GREATER_EQUAL:
861                 if(ver.dwMinorVersion < lpVersionInfo->dwMinorVersion)
862                     res = FALSE;
863                 break;
864             case VER_LESS:
865                 if(ver.dwMinorVersion >= lpVersionInfo->dwMinorVersion)
866                     res = FALSE;
867                 break;
868             case VER_LESS_EQUAL:
869                 if(ver.dwMinorVersion > lpVersionInfo->dwMinorVersion)
870                     res = FALSE;
871                 break;
872             default:
873                 res = FALSE;
874                 SetLastError(ERROR_BAD_ARGUMENTS);
875                 error_set = TRUE;
876         }
877     if(dwTypeMask & VER_SERVICEPACKMAJOR && res)
878         switch(dwlConditionMask >> 5*3 & 0x07) {
879             case VER_EQUAL:
880                 if(ver.wServicePackMajor != lpVersionInfo->wServicePackMajor)
881                     res = FALSE;
882                 break;
883             case VER_GREATER:
884                 if(ver.wServicePackMajor <= lpVersionInfo->wServicePackMajor)
885                     res = FALSE;
886                 break;
887             case VER_GREATER_EQUAL:
888                 if(ver.wServicePackMajor < lpVersionInfo->wServicePackMajor)
889                     res = FALSE;
890                 break;
891             case VER_LESS:
892                 if(ver.wServicePackMajor >= lpVersionInfo->wServicePackMajor)
893                     res = FALSE;
894                 break;
895             case VER_LESS_EQUAL:
896                 if(ver.wServicePackMajor > lpVersionInfo->wServicePackMajor)
897                     res = FALSE;
898                 break;
899             default:
900                 res = FALSE;
901                 SetLastError(ERROR_BAD_ARGUMENTS);
902                 error_set = TRUE;
903         }
904     if(dwTypeMask & VER_SERVICEPACKMINOR && res)
905         switch(dwlConditionMask >> 4*3 & 0x07) {
906             case VER_EQUAL:
907                 if(ver.wServicePackMinor != lpVersionInfo->wServicePackMinor)
908                     res = FALSE;
909                 break;
910             case VER_GREATER:
911                 if(ver.wServicePackMinor <= lpVersionInfo->wServicePackMinor)
912                     res = FALSE;
913                 break;
914             case VER_GREATER_EQUAL:
915                 if(ver.wServicePackMinor < lpVersionInfo->wServicePackMinor)
916                     res = FALSE;
917                 break;
918             case VER_LESS:
919                 if(ver.wServicePackMinor >= lpVersionInfo->wServicePackMinor)
920                     res = FALSE;
921                 break;
922             case VER_LESS_EQUAL:
923                 if(ver.wServicePackMinor > lpVersionInfo->wServicePackMinor)
924                     res = FALSE;
925                 break;
926             default:
927                 res = FALSE;
928                 SetLastError(ERROR_BAD_ARGUMENTS);
929                 error_set = TRUE;
930         }
931
932     if(!(res || error_set))
933         SetLastError(ERROR_OLD_WIN_VERSION);
934     return res;
935 }
936
937
938 /***********************************************************************
939  *          GetWinFlags   (KERNEL.132)
940  */
941 DWORD WINAPI GetWinFlags16(void)
942 {
943   static const long cpuflags[5] =
944     { WF_CPU086, WF_CPU186, WF_CPU286, WF_CPU386, WF_CPU486 };
945   SYSTEM_INFO si;
946   OSVERSIONINFOA ovi;
947   DWORD result;
948
949   GetSystemInfo(&si);
950
951   /* There doesn't seem to be any Pentium flag.  */
952   result = cpuflags[min(si.wProcessorLevel, 4)] | WF_ENHANCED | WF_PMODE | WF_80x87 | WF_PAGING;
953   if (si.wProcessorLevel >= 4) result |= WF_HASCPUID;
954   ovi.dwOSVersionInfoSize = sizeof(ovi);
955   GetVersionExA(&ovi);
956   if (ovi.dwPlatformId == VER_PLATFORM_WIN32_NT)
957       result |= WF_WIN32WOW; /* undocumented WF_WINNT */
958   return result;
959 }
960
961
962 #if 0
963 /* Not used at this time. This is here for documentation only */
964
965 /* WINDEBUGINFO flags values */
966 #define WDI_OPTIONS         0x0001
967 #define WDI_FILTER          0x0002
968 #define WDI_ALLOCBREAK      0x0004
969
970 /* dwOptions values */
971 #define DBO_CHECKHEAP       0x0001
972 #define DBO_BUFFERFILL      0x0004
973 #define DBO_DISABLEGPTRAPPING 0x0010
974 #define DBO_CHECKFREE       0x0020
975
976 #define DBO_SILENT          0x8000
977
978 #define DBO_TRACEBREAK      0x2000
979 #define DBO_WARNINGBREAK    0x1000
980 #define DBO_NOERRORBREAK    0x0800
981 #define DBO_NOFATALBREAK    0x0400
982 #define DBO_INT3BREAK       0x0100
983
984 /* DebugOutput flags values */
985 #define DBF_TRACE           0x0000
986 #define DBF_WARNING         0x4000
987 #define DBF_ERROR           0x8000
988 #define DBF_FATAL           0xc000
989
990 /* dwFilter values */
991 #define DBF_KERNEL          0x1000
992 #define DBF_KRN_MEMMAN      0x0001
993 #define DBF_KRN_LOADMODULE  0x0002
994 #define DBF_KRN_SEGMENTLOAD 0x0004
995 #define DBF_USER            0x0800
996 #define DBF_GDI             0x0400
997 #define DBF_MMSYSTEM        0x0040
998 #define DBF_PENWIN          0x0020
999 #define DBF_APPLICATION     0x0008
1000 #define DBF_DRIVER          0x0010
1001
1002 #endif /* NOLOGERROR */
1003
1004
1005 /***********************************************************************
1006  *          GetWinDebugInfo   (KERNEL.355)
1007  */
1008 BOOL16 WINAPI GetWinDebugInfo16(WINDEBUGINFO16 *lpwdi, UINT16 flags)
1009 {
1010     FIXME("(%8lx,%d): stub returning 0\n",
1011           (unsigned long)lpwdi, flags);
1012     /* 0 means not in debugging mode/version */
1013     /* Can this type of debugging be used in wine ? */
1014     /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
1015     return 0;
1016 }
1017
1018
1019 /***********************************************************************
1020  *          SetWinDebugInfo   (KERNEL.356)
1021  */
1022 BOOL16 WINAPI SetWinDebugInfo16(WINDEBUGINFO16 *lpwdi)
1023 {
1024     FIXME("(%8lx): stub returning 0\n", (unsigned long)lpwdi);
1025     /* 0 means not in debugging mode/version */
1026     /* Can this type of debugging be used in wine ? */
1027     /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
1028     return 0;
1029 }
1030
1031
1032 /***********************************************************************
1033  *           K329                    (KERNEL.329)
1034  *
1035  * TODO:
1036  * Should fill lpBuffer only if DBO_BUFFERFILL has been set by SetWinDebugInfo()
1037  */
1038 void WINAPI DebugFillBuffer(LPSTR lpBuffer, WORD wBytes)
1039 {
1040         memset(lpBuffer, DBGFILL_BUFFER, wBytes);
1041 }
1042
1043 /***********************************************************************
1044  *           DiagQuery                          (KERNEL.339)
1045  *
1046  * returns TRUE if Win called with "/b" (bootlog.txt)
1047  */
1048 BOOL16 WINAPI DiagQuery16(void)
1049 {
1050         /* perhaps implement a Wine "/b" command line flag sometime ? */
1051         return FALSE;
1052 }
1053
1054 /***********************************************************************
1055  *           DiagOutput                         (KERNEL.340)
1056  *
1057  * writes a debug string into <windir>\bootlog.txt
1058  */
1059 void WINAPI DiagOutput16(LPCSTR str)
1060 {
1061         /* FIXME */
1062         DPRINTF("DIAGOUTPUT:%s\n", debugstr_a(str));
1063 }