MMSYSTEM_GetIData() now dies when no iData is present
[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 "winbase.h"
13 #include "winuser.h"
14 #include "wine/winbase16.h"
15 #include "process.h"
16 #include "options.h"
17 #include "debugtools.h"
18 #include "neexe.h"
19 #include "winversion.h"
20 #include "winerror.h"
21
22 DEFAULT_DEBUG_CHANNEL(ver)
23
24 typedef struct
25 {
26     LONG             getVersion16; 
27     LONG             getVersion32;
28     OSVERSIONINFOA getVersionEx;
29 } VERSION_DATA;
30
31
32 /* FIXME: compare values below with original and fix */
33 static VERSION_DATA VersionData[NB_WINDOWS_VERSIONS] =
34 {
35     /* WIN31 */
36     {
37         MAKELONG( 0x0a03, 0x0616 ), /* DOS 6.22 */
38         MAKELONG( 0x0a03, 0x8000 ),
39         {
40             sizeof(OSVERSIONINFOA), 3, 10, 0,
41             VER_PLATFORM_WIN32s, "Win32s 1.3" 
42         }
43     },
44     /* WIN95 */
45     {
46         0x07005F03,
47         0xC0000004,
48         {
49             sizeof(OSVERSIONINFOA), 4, 0, 0x40003B6,
50             VER_PLATFORM_WIN32_WINDOWS, "Win95"
51         }
52     },
53     /* WIN98 */
54     {
55         0x07005F03,     /* FIXME: need DOS value from real Win98 */
56         0xC0000A04,
57         {
58             sizeof(OSVERSIONINFOA), 4, 10, 0x40A07CE,
59             VER_PLATFORM_WIN32_WINDOWS, "Win98"
60         }
61     },
62     /* NT351 */
63     {
64         0x05000A03,
65         0x04213303,
66         {
67             sizeof(OSVERSIONINFOA), 3, 51, 0x421,
68             VER_PLATFORM_WIN32_NT, "Service Pack 2"
69         }
70     },
71     /* NT40 */
72     {
73         0x05000A03,
74         0x05650004,
75         {
76             sizeof(OSVERSIONINFOA), 4, 0, 0x565,
77             VER_PLATFORM_WIN32_NT, "Service Pack 3"
78         }
79     }
80 };
81
82 static const char *WinVersionNames[NB_WINDOWS_VERSIONS] =
83 {
84     "win31",
85     "win95",
86     "win98",
87     "nt351",
88     "nt40"
89 };
90
91 /* the current version has not been autodetected but forced via cmdline */
92 static BOOL versionForced = FALSE;
93 static WINDOWS_VERSION defaultWinVersion = WIN31;
94
95
96 /**********************************************************************
97  *         VERSION_ParseWinVersion
98  */
99 void VERSION_ParseWinVersion( const char *arg )
100 {
101     int i;
102     for (i = 0; i < NB_WINDOWS_VERSIONS; i++)
103     {
104         if (!strcmp( WinVersionNames[i], arg ))
105         {
106             defaultWinVersion = (WINDOWS_VERSION)i;
107             versionForced = TRUE;
108             return;
109         }
110     }
111     MESSAGE("Invalid winver value '%s' specified.\n", arg );
112     MESSAGE("Valid versions are:" );
113     for (i = 0; i < NB_WINDOWS_VERSIONS; i++)
114         MESSAGE(" '%s'%c", WinVersionNames[i],
115             (i == NB_WINDOWS_VERSIONS - 1) ? '\n' : ',' );
116 }
117
118
119 /**********************************************************************
120  *         VERSION_ParseDosVersion
121  */
122 void VERSION_ParseDosVersion( const char *arg )
123 {
124     int hi, lo;
125     if (sscanf( arg, "%d.%d", &hi, &lo ) == 2)
126     {
127         VersionData[WIN31].getVersion16 =
128             MAKELONG(LOWORD(VersionData[WIN31].getVersion16),
129                      (hi<<8) + lo);
130     }
131     else
132         fprintf( stderr, "-dosver: Wrong version format. Use \"-dosver x.xx\"\n");
133 }
134
135
136 WINDOWS_VERSION VERSION_GetImageVersion(PDB *pdb)
137 {
138     PIMAGE_NT_HEADERS peheader;
139
140         if (!pdb->exe_modref)
141         {
142                 /* HACK: if we have loaded a PE image into this address space,
143                  * we are probably using thunks, so Win95 is our best bet
144                  */
145                 if (pdb->modref_list)
146                         return WIN95;
147                 /* FIXME: hmm, do anything else ?
148                    TDB.version doesn't help here
149                    as it always holds version 3.10 */
150                         return WIN31;
151         }
152         else
153         {
154                 peheader = PE_HEADER(pdb->exe_modref->module);
155 #define OPTHD peheader->OptionalHeader
156
157                 TRACE("%02x.%02x/%02x.%02x/%02x.%02x/%02x.%02x\n",
158                           OPTHD.MajorLinkerVersion,
159                           OPTHD.MinorLinkerVersion,
160                           OPTHD.MajorOperatingSystemVersion,
161                           OPTHD.MinorOperatingSystemVersion,
162                           OPTHD.MajorImageVersion,
163                           OPTHD.MinorImageVersion,
164                           OPTHD.MajorSubsystemVersion,
165                           OPTHD.MinorSubsystemVersion);
166
167         switch (OPTHD.MajorSubsystemVersion)
168                 {
169                         case 4:
170                                 switch (OPTHD.MajorOperatingSystemVersion)
171                                 {
172                                         case 5:
173                                                 return NT40; /* FIXME: this is NT 5, isn't it ? */
174                                         case 4:
175                                                 if ((OPTHD.MajorImageVersion == 0) &&
176                                                         (OPTHD.SectionAlignment == 4096))
177                                                         return WIN95;
178                                                 else
179                                                         return NT40;
180                                         case 1:
181                                                 return WIN95;
182                                         default:
183                                                 return WIN95; /* FIXME ? */
184                                 }
185                         case 3:
186                                 /* 3.1x versions */
187                                 if (OPTHD.MinorSubsystemVersion <= 11)
188                                 {
189                                         if (OPTHD.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
190                                                 return NT351; /* FIXME: NT 3.1 */
191                                         else
192                                                 return WIN31;
193                                 }
194                                 else
195                                 {
196                                         /* NT 3.51 */
197                                         if (OPTHD.MinorSubsystemVersion == 50)
198                                                 return NT351;
199                                         else
200                                         if (OPTHD.MinorSubsystemVersion == 51)
201                                                 return NT351;
202                                 }
203                         default:
204                                 if (OPTHD.MajorSubsystemVersion)
205                                 ERR("unknown subsystem version: %04x.%04x, please report.\n",
206                                         OPTHD.MajorSubsystemVersion,
207                                         OPTHD.MinorSubsystemVersion );
208                                 return defaultWinVersion;
209                 }
210 #undef OPTHD
211         }
212 }
213
214
215 /**********************************************************************
216  *
217  * Check the version of COMDLG32, SHELL32, COMCTL32 and CTL3D32.
218  * Not functional yet.
219  */
220 DWORD VERSION_GetLinkedDllVersion(PDB *pdb)
221 {
222         WINE_MODREF *wm;
223     WORD VersionCounter[NB_WINDOWS_VERSIONS];
224
225     memset(VersionCounter, 0, sizeof(VersionCounter));
226
227         VersionCounter[WIN95] = 1;
228         for ( wm = PROCESS_Current()->modref_list; wm; wm=wm->next ) {
229             if (!(lstrncmpiA(wm->modname, "CTL3D32", 7)))
230                         VersionCounter[WIN95]++;
231         }
232
233         return MAKELONG(WIN95, VersionCounter[WIN95]);
234 }
235
236
237 /**********************************************************************
238  *         VERSION_GetVersion
239  *
240  * Some version data:
241  * linker/OS/image/subsys       Name                    Intended for
242  *
243  * 2.39/1.00/0.00/3.10          freecell.exe    Win32s (any version)
244  * 2.55/1.00/0.00/4.00          acrord32.exe    Win32s, Win95 supported (?)
245  * 
246  * 2.50/1.00/4.00/4.00          winhlp32.exe    Win32s 1.30
247  * 4.20/4.00/1.00/4.00          Asuslm.exe              Win95 (Aaargh !)
248  * 5.12/4.00/1.07/4.00      clikfixi.exe    NT 4 (service pack files)
249  * 3.10/4.00/4.00/4.00          PLUMBING.EXE    NT
250  * ?.??/4.00/97.01/4.00         sse.exe                 huh ?? (damn crackerz ;)
251  * 5.12/5.00/5.00/4.00          comctl32.dll    NT4 / IE 5.0
252  * 6.00/5.00/5.00/4.00                                          NT 4 driver update (strange numbers)
253  * 
254  * Common versions:
255  * x.xx/1.00/0.00/3.10                                          Win32s (any version ?)
256  * 2.xx/1.00/0.00/4.00                                          Win95 (Microsoft/system files) 
257  * x.xx/4.00/0.00/4.00                                          Win95 (most applications !)
258  * x.xx/4.00/4.00/4.00                                          NT 4 (most apps)
259  * x.xx/5.00/5.00/4.00                                          NT 4 newer files / NT 5 ??
260  */
261 WINDOWS_VERSION VERSION_GetVersion(void)
262 {
263     PDB *pdb = PROCESS_Current();
264     DWORD DllVer;
265
266     if (versionForced) /* user has overridden any sensible checks */
267         return defaultWinVersion;
268
269     if (pdb->winver == 0xffff) /* to be determined */
270         {
271                 DllVer = 0/*VERSION_GetLinkedDllVersion(pdb)*/;
272                 if (HIWORD(DllVer) > 1)
273                         pdb->winver = LOWORD(DllVer);
274             else
275                         pdb->winver = VERSION_GetImageVersion(pdb);
276         }
277
278         return pdb->winver;
279 }
280
281
282 /**********************************************************************
283  *         VERSION_GetVersionName
284  */
285 char *VERSION_GetVersionName()
286 {
287   WINDOWS_VERSION ver = VERSION_GetVersion();
288   switch(ver)
289     {
290     case WIN31:
291       return "Windows 3.1";
292     case WIN95:  
293       return "Windows 95";
294     case WIN98:
295       return "Windows 98";
296     case NT351:
297       return "Windows NT 3.51";
298     case NT40:
299       return "Windows NT 4.0";
300     default:
301       FIXME("Windows version %d not named",ver);
302       return "Windows <Unknown>";
303     }
304 }
305
306 /***********************************************************************
307  *         GetVersion16   (KERNEL.3)
308  */
309 LONG WINAPI GetVersion16(void)
310 {
311     WINDOWS_VERSION ver = VERSION_GetVersion();
312     return VersionData[ver].getVersion16;
313 }
314
315
316 /***********************************************************************
317  *         GetVersion32   (KERNEL32.427)
318  */
319 LONG WINAPI GetVersion(void)
320 {
321     WINDOWS_VERSION ver = VERSION_GetVersion();
322     return VersionData[ver].getVersion32;
323 }
324
325
326 /***********************************************************************
327  *         GetVersionEx16   (KERNEL.149)
328  */
329 BOOL16 WINAPI GetVersionEx16(OSVERSIONINFO16 *v)
330 {
331     WINDOWS_VERSION ver = VERSION_GetVersion();
332     if (v->dwOSVersionInfoSize != sizeof(OSVERSIONINFO16))
333     {
334         WARN("wrong OSVERSIONINFO size from app");
335         SetLastError(ERROR_INSUFFICIENT_BUFFER);
336         return FALSE;
337     }
338     v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
339     v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
340     v->dwBuildNumber  = VersionData[ver].getVersionEx.dwBuildNumber;
341     v->dwPlatformId   = VersionData[ver].getVersionEx.dwPlatformId;
342     strcpy( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
343     return TRUE;
344 }
345
346
347 /***********************************************************************
348  *         GetVersionEx32A   (KERNEL32.428)
349  */
350 BOOL WINAPI GetVersionExA(OSVERSIONINFOA *v)
351 {
352     WINDOWS_VERSION ver = VERSION_GetVersion();
353     if (v->dwOSVersionInfoSize != sizeof(OSVERSIONINFOA))
354     {
355         WARN("wrong OSVERSIONINFO size from app");
356         SetLastError(ERROR_INSUFFICIENT_BUFFER);
357         return FALSE;
358     }
359     v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
360     v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
361     v->dwBuildNumber  = VersionData[ver].getVersionEx.dwBuildNumber;
362     v->dwPlatformId   = VersionData[ver].getVersionEx.dwPlatformId;
363     strcpy( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
364     return TRUE;
365 }
366
367
368 /***********************************************************************
369  *         GetVersionEx32W   (KERNEL32.429)
370  */
371 BOOL WINAPI GetVersionExW(OSVERSIONINFOW *v)
372 {
373     WINDOWS_VERSION ver = VERSION_GetVersion();
374
375     if (v->dwOSVersionInfoSize!=sizeof(OSVERSIONINFOW))
376     {
377         WARN("wrong OSVERSIONINFO size from app");
378         SetLastError(ERROR_INSUFFICIENT_BUFFER);
379         return FALSE;
380     }
381     v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
382     v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
383     v->dwBuildNumber  = VersionData[ver].getVersionEx.dwBuildNumber;
384     v->dwPlatformId   = VersionData[ver].getVersionEx.dwPlatformId;
385     lstrcpyAtoW( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
386     return TRUE;
387 }
388
389
390 /***********************************************************************
391  *          GetWinFlags   (KERNEL.132)
392  */
393 DWORD WINAPI GetWinFlags16(void)
394 {
395   static const long cpuflags[5] =
396     { WF_CPU086, WF_CPU186, WF_CPU286, WF_CPU386, WF_CPU486 };
397   SYSTEM_INFO si;
398   OSVERSIONINFOA ovi;
399   DWORD result;
400
401   GetSystemInfo(&si);
402
403   /* There doesn't seem to be any Pentium flag.  */
404   result = cpuflags[MIN (si.wProcessorLevel, 4)];
405
406   switch(Options.mode)
407   {
408   case MODE_STANDARD:
409       result |= WF_STANDARD | WF_PMODE | WF_80x87;
410       break;
411
412   case MODE_ENHANCED:
413       result |= WF_ENHANCED | WF_PMODE | WF_80x87 | WF_PAGING;
414       break;
415
416   default:
417       ERR("Unknown mode set? This shouldn't happen. Check GetWinFlags()!\n");
418       break;
419   }
420   if (si.wProcessorLevel >= 4) result |= WF_HASCPUID;
421   ovi.dwOSVersionInfoSize = sizeof(ovi);
422   GetVersionExA(&ovi);
423   if (ovi.dwPlatformId == VER_PLATFORM_WIN32_NT)
424       result |= WF_WIN32WOW; /* undocumented WF_WINNT */
425   return result;
426 }
427
428
429 /***********************************************************************
430  *          GetWinDebugInfo   (KERNEL.355)
431  */
432 BOOL16 WINAPI GetWinDebugInfo16(WINDEBUGINFO *lpwdi, UINT16 flags)
433 {
434     FIXME("(%8lx,%d): stub returning 0\n",
435           (unsigned long)lpwdi, flags);
436     /* 0 means not in debugging mode/version */
437     /* Can this type of debugging be used in wine ? */
438     /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
439     return 0;
440 }
441
442
443 /***********************************************************************
444  *          SetWinDebugInfo   (KERNEL.356)
445  */
446 BOOL16 WINAPI SetWinDebugInfo16(WINDEBUGINFO *lpwdi)
447 {
448     FIXME("(%8lx): stub returning 0\n", (unsigned long)lpwdi);
449     /* 0 means not in debugging mode/version */
450     /* Can this type of debugging be used in wine ? */
451     /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
452     return 0;
453 }
454
455
456 /***********************************************************************
457  *           DebugFillBuffer                    (KERNEL.329)
458  *
459  * TODO:
460  * Should fill lpBuffer only if DBO_BUFFERFILL has been set by SetWinDebugInfo()
461  */
462 void WINAPI DebugFillBuffer(LPSTR lpBuffer, WORD wBytes)
463 {
464         memset(lpBuffer, DBGFILL_BUFFER, wBytes);
465 }
466
467 /***********************************************************************
468  *           DiagQuery                          (KERNEL.339)
469  *
470  * returns TRUE if Win called with "/b" (bootlog.txt)
471  */
472 BOOL16 WINAPI DiagQuery16()
473 {
474         /* perhaps implement a Wine "/b" command line flag sometime ? */
475         return FALSE;
476 }
477
478 /***********************************************************************
479  *           DiagOutput                         (KERNEL.340)
480  *
481  * writes a debug string into <windir>\bootlog.txt
482  */
483 void WINAPI DiagOutput16(LPCSTR str)
484 {
485         /* FIXME */
486         DPRINTF("DIAGOUTPUT:%s\n", debugstr_a(str));
487 }
488
489 /***********************************************************************
490  *           OaBuildVersion           [OLEAUT32.170]
491  */
492 UINT WINAPI OaBuildVersion()
493 {
494     WINDOWS_VERSION ver = VERSION_GetVersion();
495
496     FIXME("Please report to a.mohr@mailto.de if you get version error messages !\n");
497     switch(VersionData[ver].getVersion32)
498     {
499         case 0x80000a03: /* Win 3.1 */
500                 return 0x140fd1; /* from Win32s 1.1e */
501         case 0xc0000004: /* Win 95 */
502          case 0xc0000a04: /* Win 98: verified same as Win95 */
503                 return 0x1e10a9; /* some older version: 0x0a0bd3 */
504         case 0x04213303: /* NT 3.51 */
505                 FIXME("NT 3.51 version value unknown !\n");
506                 return 0x1e10a9; /* value borrowed from Win95 */
507         case 0x05650004: /* NT 4.0 */
508                 return 0x141016;
509         default:
510                 return 0x0;
511     }
512 }
513 /***********************************************************************
514  *        VERSION_OsIsUnicode   [internal]
515  *
516  * NOTES
517  *   some functions getting sometimes LPSTR sometimes LPWSTR...
518  *
519  */
520 BOOL VERSION_OsIsUnicode(void)
521 {
522     switch(VERSION_GetVersion())
523     {
524     case NT351:
525     case NT40:
526         return TRUE;
527     default:
528         return FALSE;
529     }
530 }