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