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