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