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