- added winver settings nt2k, win30 and win20 (yes, some rare programs need this)
[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
10 #include <string.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include "windef.h"
14 #include "winbase.h"
15 #include "wingdi.h"
16 #include "winuser.h"
17 #include "wine/winbase16.h"
18 #include "wine/winestring.h"
19 #include "process.h"
20 #include "options.h"
21 #include "debugtools.h"
22 #include "neexe.h"
23 #include "winerror.h"
24
25 DEFAULT_DEBUG_CHANNEL(ver);
26
27 typedef enum
28 {
29     WIN20,   /* Windows 2.0 */
30     WIN30,   /* Windows 3.0 */
31     WIN31,   /* Windows 3.1 */
32     WIN95,   /* Windows 95 */
33     WIN98,   /* Windows 98 */
34  /* insert Windows ME here as WINME if needed */
35     NT351,   /* Windows NT 3.51 */
36     NT40,    /* Windows NT 4.0 */  
37     NT2K,    /* Windows 2000 */
38     NB_WINDOWS_VERSIONS
39 } WINDOWS_VERSION;
40
41 typedef struct
42 {
43     LONG             getVersion16; 
44     LONG             getVersion32;
45     OSVERSIONINFOA getVersionEx;
46 } VERSION_DATA;
47
48 /* FIXME: compare values below with original and fix */
49 static VERSION_DATA VersionData[NB_WINDOWS_VERSIONS] =
50 {
51     /* WIN20 FIXME: verify values */
52     {
53         MAKELONG( 0x0002, 0x0303 ), /* assume DOS 3.3 */
54         MAKELONG( 0x0003, 0x8000 ),
55         {
56             sizeof(OSVERSIONINFOA), 2, 0, 0,
57             VER_PLATFORM_WIN32s, "Win32s 1.3" 
58         }
59     },
60     /* WIN30 FIXME: verify values */
61     {
62         MAKELONG( 0x0003, 0x0500 ), /* assume DOS 5.00 */
63         MAKELONG( 0x0003, 0x8000 ),
64         {
65             sizeof(OSVERSIONINFOA), 3, 0, 0,
66             VER_PLATFORM_WIN32s, "Win32s 1.3" 
67         }
68     },
69     /* WIN31 */
70     {
71         MAKELONG( 0x0a03, 0x0616 ), /* DOS 6.22 */
72         MAKELONG( 0x0a03, 0x8000 ),
73         {
74             sizeof(OSVERSIONINFOA), 3, 10, 0,
75             VER_PLATFORM_WIN32s, "Win32s 1.3" 
76         }
77     },
78     /* WIN95 */
79     {
80         0x07005F03,
81         0xC0000004,
82         {
83             sizeof(OSVERSIONINFOA), 4, 0, 0x40003B6,
84             VER_PLATFORM_WIN32_WINDOWS, "Win95"
85         }
86     },
87     /* WIN98 */
88     {
89         0x070A5F03,
90         0xC0000A04,
91         {
92             sizeof(OSVERSIONINFOA), 4, 10, 0x40A07CE,
93             VER_PLATFORM_WIN32_WINDOWS, "Win98"
94         }
95     },
96     /* NT351 */
97     {
98         0x05000A03,
99         0x04213303,
100         {
101             sizeof(OSVERSIONINFOA), 3, 51, 0x421,
102             VER_PLATFORM_WIN32_NT, "Service Pack 2"
103         }
104     },
105     /* NT40 */
106     {
107         0x05000A03,
108         0x05650004,
109         {
110             sizeof(OSVERSIONINFOA), 4, 0, 0x565,
111             VER_PLATFORM_WIN32_NT, "Service Pack 6"
112         }
113     },
114     /* NT2K FIXME: verify values */
115     {
116         0x05000A03, /* ? */
117         0x08930005, /* better build ? using 2195 (final) for now */
118         {
119             sizeof(OSVERSIONINFOA), 5, 0, 0x893,
120             VER_PLATFORM_WIN32_NT, "FIXME: OS version string not known yet"
121         }
122     }
123 };
124
125 static const char *WinVersionNames[NB_WINDOWS_VERSIONS] =
126 {
127     "win20",
128     "win30",
129     "win31",
130     "win95",
131     "win98",
132     "nt351",
133     "nt40",
134     "nt2k"
135 };
136
137 /* if one of the following dlls is importing ntdll the windows
138 version autodetection switches wine to unicode (nt 3.51 or 4.0) */
139 static char * special_dlls[] =
140 {
141         "COMDLG32",
142         "COMCTL32",
143         "SHELL32",
144         "OLE32",
145         "RPCRT4",
146         NULL
147 };
148
149 /* the current version has not been autodetected but forced via cmdline */
150 static BOOL versionForced = FALSE;
151 static WINDOWS_VERSION defaultWinVersion = WIN31;
152
153 /**********************************************************************
154  *         VERSION_ParseWinVersion
155  */
156 void VERSION_ParseWinVersion( const char *arg )
157 {
158     int i;
159     for (i = 0; i < NB_WINDOWS_VERSIONS; i++)
160     {
161         if (!strcmp( WinVersionNames[i], arg ))
162         {
163             defaultWinVersion = (WINDOWS_VERSION)i;
164             versionForced = TRUE;
165             return;
166         }
167     }
168     MESSAGE("Invalid winver value '%s' specified.\n", arg );
169     MESSAGE("Valid versions are:" );
170     for (i = 0; i < NB_WINDOWS_VERSIONS; i++)
171         MESSAGE(" '%s'%c", WinVersionNames[i],
172             (i == NB_WINDOWS_VERSIONS - 1) ? '\n' : ',' );
173     ExitProcess(1);
174 }
175
176
177 /**********************************************************************
178  *         VERSION_ParseDosVersion
179  */
180 void VERSION_ParseDosVersion( const char *arg )
181 {
182     int hi, lo;
183     if (sscanf( arg, "%d.%d", &hi, &lo ) == 2)
184     {
185         VersionData[WIN31].getVersion16 =
186             MAKELONG(LOWORD(VersionData[WIN31].getVersion16),
187                      (hi<<8) + lo);
188     }
189     else
190     {
191         MESSAGE("--dosver: Wrong version format. Use \"--dosver x.xx\"\n");
192         ExitProcess(1);
193     }
194 }
195
196 /**********************************************************************
197  *      VERSION_GetSystemDLLVersion
198  *
199  * This function tries to figure out if a given (native) dll comes from
200  * win95/98 or winnt. Since all values in the OptionalHeader are not a 
201  * usable hint, we test if a dll imports the ntdll.
202  * This is at least working for all system dlls like comctl32, comdlg32 and
203  * shell32.
204  * If you have a better idea to figure this out...
205  */
206 static DWORD VERSION_GetSystemDLLVersion( HMODULE hmod )
207 {
208     IMAGE_DATA_DIRECTORY *dir = PE_HEADER(hmod)->OptionalHeader.DataDirectory
209                                 + IMAGE_DIRECTORY_ENTRY_IMPORT;
210     if (dir->Size && dir->VirtualAddress)
211     {
212         IMAGE_IMPORT_DESCRIPTOR *pe_imp = (IMAGE_IMPORT_DESCRIPTOR *)((char *)hmod + dir->VirtualAddress);
213         for ( ; pe_imp->Name; pe_imp++)
214         {
215             char * name = (char *)hmod + (unsigned int)pe_imp->Name;
216             TRACE("%s\n", name);
217             
218             if (!strncasecmp(name, "ntdll", 5))
219             {
220               if (3 == PE_HEADER(hmod)->OptionalHeader.MajorOperatingSystemVersion)
221                 return NT351;
222               else
223                 return NT40;
224             }
225         }
226     }
227     return WIN95;
228 }
229 /**********************************************************************
230  *      VERSION_GetLinkedDllVersion
231  *
232  * Some version data (not reliable!):
233  * linker/OS/image/subsys
234  *
235  * x.xx/1.00/0.00/3.10  Win32s          (any version ?)
236  * 2.39/1.00/0.00/3.10  Win32s          freecell.exe (any version)
237  * 2.50/1.00/4.00/4.00  Win32s 1.30     winhlp32.exe    
238  * 2.60/3.51/3.51/3.51  NT351SP5        system dlls 
239  * 2.60/3.51/3.51/4.00  NT351SP5        comctl32 dll
240  * 2.xx/1.00/0.00/4.00  Win95           system files
241  * x.xx/4.00/0.00/4.00  Win95           most applications
242  * 3.10/4.00/0.00/4.00  Win98           notepad
243  * x.xx/5.00/5.00/4.00  Win98           system dlls
244  * x.xx/4.00/4.00/4.00  NT 4            most apps
245  * 5.12/5.00/5.00/4.00  NT4+IE5         comctl32.dll
246  * 5.12/5.00/5.00/4.00  Win98           calc
247  * x.xx/5.00/5.00/4.00  win95/win98/NT4 IE5 files
248  */
249 DWORD VERSION_GetLinkedDllVersion(PDB *pdb)
250 {
251         WINE_MODREF *wm;
252         DWORD WinVersion = NB_WINDOWS_VERSIONS;
253         PIMAGE_OPTIONAL_HEADER ophd;
254
255         if (!pdb->exe_modref)
256         {
257           if (!pdb->modref_list)
258             return WIN31;
259
260           /* FIXME: The above condition will never trigger, since all our
261            * standard dlls load their win32 equivalents. We have usually at
262            * this point: kernel32.dll and ntdll.dll.
263            */
264           return WIN95;
265         }
266         /* First check the native dlls provided. These have to be
267         from one windows version */
268         for ( wm = pdb->modref_list; wm; wm=wm->next )
269         {
270           ophd = &(PE_HEADER(wm->module)->OptionalHeader);
271
272           TRACE("%s: %02x.%02x/%02x.%02x/%02x.%02x/%02x.%02x\n",
273             wm->modname,
274             ophd->MajorLinkerVersion, ophd->MinorLinkerVersion,
275             ophd->MajorOperatingSystemVersion, ophd->MinorOperatingSystemVersion,
276             ophd->MajorImageVersion, ophd->MinorImageVersion,
277             ophd->MajorSubsystemVersion, ophd->MinorSubsystemVersion);
278
279           /* test if it is an external (native) dll */
280           if (!(wm->flags & WINE_MODREF_INTERNAL))
281           {
282             int i;
283             for (i = 0; special_dlls[i]; i++)
284             {
285               /* test if it is a special dll */
286               if (!strncasecmp(wm->modname, special_dlls[i], strlen(special_dlls[i]) ))
287               {
288                 DWORD DllVersion = VERSION_GetSystemDLLVersion(wm->module);
289                 if (WinVersion == NB_WINDOWS_VERSIONS) 
290                   WinVersion = DllVersion;
291                 else {
292                   if (WinVersion != DllVersion) {
293                     ERR("You mixed system dlls from different windows versions! Expect a crash! (%s: expected version '%s', but is '%s')\n",
294                         wm->modname,
295                         VersionData[WinVersion].getVersionEx.szCSDVersion,
296                         VersionData[DllVersion].getVersionEx.szCSDVersion);
297                     return WIN31; /* this may let the exe exiting */
298                   }
299                 }
300                 break;
301               }
302             }
303           }
304         }
305         
306         if(WinVersion != NB_WINDOWS_VERSIONS) return WinVersion;
307         
308         /* we are using no external system dlls, look at the exe */
309         ophd = &(PE_HEADER(pdb->exe_modref->module)->OptionalHeader);
310         
311         TRACE("-%s: %02x.%02x/%02x.%02x/%02x.%02x/%02x.%02x\n",
312             pdb->exe_modref->modname,
313             ophd->MajorLinkerVersion, ophd->MinorLinkerVersion,
314             ophd->MajorOperatingSystemVersion, ophd->MinorOperatingSystemVersion,
315             ophd->MajorImageVersion, ophd->MinorImageVersion,
316             ophd->MajorSubsystemVersion, ophd->MinorSubsystemVersion);
317
318         /* special nt 3.51 */
319         if (3 == ophd->MajorOperatingSystemVersion && 51 == ophd->MinorOperatingSystemVersion)
320         {
321             return NT351;
322         }
323
324         /* the MajorSubsystemVersion is the only usable sign */
325         if (ophd->MajorSubsystemVersion < 4)
326         {
327           if ( ophd->MajorOperatingSystemVersion == 1 
328             && ophd->MinorOperatingSystemVersion == 0)
329           {
330             return WIN31; /* win32s */
331           }
332           
333           if (ophd->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
334             return NT351; /* FIXME: NT 3.1, not tested */
335           else
336             return WIN95;
337         }       
338
339         return WIN95;
340 }
341
342 /**********************************************************************
343  *         VERSION_GetVersion
344  *
345  * WARNING !!!
346  * Don't call this function too early during the Wine init,
347  * as pdb->exe_modref (required by VERSION_GetImageVersion()) might still
348  * be NULL in such cases, which causes the winver to ALWAYS be detected
349  * as WIN31.
350  * And as we cache the winver once it has been determined, this is bad.
351  * This can happen much easier than you might think, as this function
352  * is called by EVERY GetVersion*() API !
353  *
354  */
355 static WINDOWS_VERSION VERSION_GetVersion(void)
356 {
357         static WORD winver = 0xffff;
358
359         if (versionForced) /* user has overridden any sensible checks */
360           return defaultWinVersion;
361
362         if (winver == 0xffff) /* to be determined */ {
363           WINDOWS_VERSION retver = VERSION_GetLinkedDllVersion( PROCESS_Current() );
364
365           if (retver != WIN31) winver = retver;
366           return retver;
367         }
368         return winver;
369 }
370
371
372 /***********************************************************************
373  *         GetVersion16   (KERNEL.3)
374  */
375 LONG WINAPI GetVersion16(void)
376 {
377     WINDOWS_VERSION ver = VERSION_GetVersion();
378     return VersionData[ver].getVersion16;
379 }
380
381
382 /***********************************************************************
383  *         GetVersion   (KERNEL32.427)
384  */
385 LONG WINAPI GetVersion(void)
386 {
387     WINDOWS_VERSION ver = VERSION_GetVersion();
388     return VersionData[ver].getVersion32;
389 }
390
391
392 /***********************************************************************
393  *         GetVersionEx16   (KERNEL.149)
394  */
395 BOOL16 WINAPI GetVersionEx16(OSVERSIONINFO16 *v)
396 {
397     WINDOWS_VERSION ver = VERSION_GetVersion();
398     if (v->dwOSVersionInfoSize != sizeof(OSVERSIONINFO16))
399     {
400         WARN("wrong OSVERSIONINFO size from app");
401         SetLastError(ERROR_INSUFFICIENT_BUFFER);
402         return FALSE;
403     }
404     v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
405     v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
406     v->dwBuildNumber  = VersionData[ver].getVersionEx.dwBuildNumber;
407     v->dwPlatformId   = VersionData[ver].getVersionEx.dwPlatformId;
408     strcpy( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
409     return TRUE;
410 }
411
412
413 /***********************************************************************
414  *         GetVersionExA   (KERNEL32.428)
415  */
416 BOOL WINAPI GetVersionExA(OSVERSIONINFOA *v)
417 {
418     WINDOWS_VERSION ver = VERSION_GetVersion();
419     if (v->dwOSVersionInfoSize != sizeof(OSVERSIONINFOA))
420     {
421         WARN("wrong OSVERSIONINFO size from app (got: %ld, expected: %d)",
422                         v->dwOSVersionInfoSize, sizeof(OSVERSIONINFOA));
423         SetLastError(ERROR_INSUFFICIENT_BUFFER);
424         return FALSE;
425     }
426     v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
427     v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
428     v->dwBuildNumber  = VersionData[ver].getVersionEx.dwBuildNumber;
429     v->dwPlatformId   = VersionData[ver].getVersionEx.dwPlatformId;
430     strcpy( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
431     return TRUE;
432 }
433
434
435 /***********************************************************************
436  *         GetVersionExW   (KERNEL32.429)
437  */
438 BOOL WINAPI GetVersionExW(OSVERSIONINFOW *v)
439 {
440     WINDOWS_VERSION ver = VERSION_GetVersion();
441
442     if (v->dwOSVersionInfoSize!=sizeof(OSVERSIONINFOW))
443     {
444         WARN("wrong OSVERSIONINFO size from app (got: %ld, expected: %d)",
445                         v->dwOSVersionInfoSize, sizeof(OSVERSIONINFOW));
446         SetLastError(ERROR_INSUFFICIENT_BUFFER);
447         return FALSE;
448     }
449     v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
450     v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
451     v->dwBuildNumber  = VersionData[ver].getVersionEx.dwBuildNumber;
452     v->dwPlatformId   = VersionData[ver].getVersionEx.dwPlatformId;
453     lstrcpyAtoW( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
454     return TRUE;
455 }
456
457
458 /***********************************************************************
459  *          GetWinFlags   (KERNEL.132)
460  */
461 DWORD WINAPI GetWinFlags16(void)
462 {
463   static const long cpuflags[5] =
464     { WF_CPU086, WF_CPU186, WF_CPU286, WF_CPU386, WF_CPU486 };
465   SYSTEM_INFO si;
466   OSVERSIONINFOA ovi;
467   DWORD result;
468
469   GetSystemInfo(&si);
470
471   /* There doesn't seem to be any Pentium flag.  */
472   result = cpuflags[min(si.wProcessorLevel, 4)] | WF_ENHANCED | WF_PMODE | WF_80x87 | WF_PAGING;
473   if (si.wProcessorLevel >= 4) result |= WF_HASCPUID;
474   ovi.dwOSVersionInfoSize = sizeof(ovi);
475   GetVersionExA(&ovi);
476   if (ovi.dwPlatformId == VER_PLATFORM_WIN32_NT)
477       result |= WF_WIN32WOW; /* undocumented WF_WINNT */
478   return result;
479 }
480
481
482 /***********************************************************************
483  *          GetWinDebugInfo   (KERNEL.355)
484  */
485 BOOL16 WINAPI GetWinDebugInfo16(WINDEBUGINFO *lpwdi, UINT16 flags)
486 {
487     FIXME("(%8lx,%d): stub returning 0\n",
488           (unsigned long)lpwdi, flags);
489     /* 0 means not in debugging mode/version */
490     /* Can this type of debugging be used in wine ? */
491     /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
492     return 0;
493 }
494
495
496 /***********************************************************************
497  *          SetWinDebugInfo   (KERNEL.356)
498  */
499 BOOL16 WINAPI SetWinDebugInfo16(WINDEBUGINFO *lpwdi)
500 {
501     FIXME("(%8lx): stub returning 0\n", (unsigned long)lpwdi);
502     /* 0 means not in debugging mode/version */
503     /* Can this type of debugging be used in wine ? */
504     /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
505     return 0;
506 }
507
508
509 /***********************************************************************
510  *           DebugFillBuffer                    (KERNEL.329)
511  *
512  * TODO:
513  * Should fill lpBuffer only if DBO_BUFFERFILL has been set by SetWinDebugInfo()
514  */
515 void WINAPI DebugFillBuffer(LPSTR lpBuffer, WORD wBytes)
516 {
517         memset(lpBuffer, DBGFILL_BUFFER, wBytes);
518 }
519
520 /***********************************************************************
521  *           DiagQuery                          (KERNEL.339)
522  *
523  * returns TRUE if Win called with "/b" (bootlog.txt)
524  */
525 BOOL16 WINAPI DiagQuery16()
526 {
527         /* perhaps implement a Wine "/b" command line flag sometime ? */
528         return FALSE;
529 }
530
531 /***********************************************************************
532  *           DiagOutput                         (KERNEL.340)
533  *
534  * writes a debug string into <windir>\bootlog.txt
535  */
536 void WINAPI DiagOutput16(LPCSTR str)
537 {
538         /* FIXME */
539         DPRINTF("DIAGOUTPUT:%s\n", debugstr_a(str));
540 }