Added special handling for the stdio handles in CloseHandle.
[wine/multimedia.git] / misc / version.c
blob5e5161dba7189e3a5b38a68e921fd10c8b991d03
1 /*
2 * Windows and DOS version functions
4 * Copyright 1997 Alexandre Julliard
5 * Copyright 1997 Marcus Meissner
6 * Copyright 1998 Patrik Stridvall
7 * Copyright 1998 Andreas Mohr
8 */
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"
22 DEFAULT_DEBUG_CHANNEL(ver)
24 typedef struct
26 LONG getVersion16;
27 LONG getVersion32;
28 OSVERSIONINFOA getVersionEx;
29 } VERSION_DATA;
32 /* FIXME: compare values below with original and fix */
33 static VERSION_DATA VersionData[NB_WINDOWS_VERSIONS] =
35 /* WIN31 */
37 MAKELONG( 0x0a03, 0x0616 ), /* DOS 6.22 */
38 MAKELONG( 0x0a03, 0x8000 ),
40 sizeof(OSVERSIONINFOA), 3, 10, 0,
41 VER_PLATFORM_WIN32s, "Win32s 1.3"
44 /* WIN95 */
46 0x07005F03,
47 0xC0000004,
49 sizeof(OSVERSIONINFOA), 4, 0, 0x40003B6,
50 VER_PLATFORM_WIN32_WINDOWS, "Win95"
53 /* WIN98 */
55 0x07005F03, /* FIXME: need DOS value from real Win98 */
56 0xC0000A04,
58 sizeof(OSVERSIONINFOA), 4, 10, 0x40A07CE,
59 VER_PLATFORM_WIN32_WINDOWS, "Win98"
62 /* NT351 */
64 0x05000A03,
65 0x04213303,
67 sizeof(OSVERSIONINFOA), 3, 51, 0x421,
68 VER_PLATFORM_WIN32_NT, "Service Pack 2"
71 /* NT40 */
73 0x05000A03,
74 0x05650004,
76 sizeof(OSVERSIONINFOA), 4, 0, 0x565,
77 VER_PLATFORM_WIN32_NT, "Service Pack 3"
82 static const char *WinVersionNames[NB_WINDOWS_VERSIONS] =
84 "win31",
85 "win95",
86 "win98",
87 "nt351",
88 "nt40"
91 /* if one of the following dlls is importing ntdll the windows
92 version autodetection switches wine to unicode (nt 3.51 or 4.0) */
93 static char * special_dlls[] =
95 "COMDLG32",
96 "COMCTL32",
97 "SHELL32",
98 "USER32",
99 "OLE32",
100 "RPCRT4",
101 NULL
104 /* the current version has not been autodetected but forced via cmdline */
105 static BOOL versionForced = FALSE;
106 static WINDOWS_VERSION defaultWinVersion = WIN31;
108 /**********************************************************************
109 * VERSION_ParseWinVersion
111 void VERSION_ParseWinVersion( const char *arg )
113 int i;
114 for (i = 0; i < NB_WINDOWS_VERSIONS; i++)
116 if (!strcmp( WinVersionNames[i], arg ))
118 defaultWinVersion = (WINDOWS_VERSION)i;
119 versionForced = TRUE;
120 return;
123 MESSAGE("Invalid winver value '%s' specified.\n", arg );
124 MESSAGE("Valid versions are:" );
125 for (i = 0; i < NB_WINDOWS_VERSIONS; i++)
126 MESSAGE(" '%s'%c", WinVersionNames[i],
127 (i == NB_WINDOWS_VERSIONS - 1) ? '\n' : ',' );
131 /**********************************************************************
132 * VERSION_ParseDosVersion
134 void VERSION_ParseDosVersion( const char *arg )
136 int hi, lo;
137 if (sscanf( arg, "%d.%d", &hi, &lo ) == 2)
139 VersionData[WIN31].getVersion16 =
140 MAKELONG(LOWORD(VersionData[WIN31].getVersion16),
141 (hi<<8) + lo);
143 else
144 fprintf( stderr, "-dosver: Wrong version format. Use \"-dosver x.xx\"\n");
147 /**********************************************************************
148 * VERSION_GetSystemDLLVersion
150 * This function tryes to figure out if a given (native) dll comes from
151 * win95/98 or winnt. Since all values in the OptionalHeader are not a
152 * usable hint, we test if a dll imports the ntdll.
153 * This is at least working for all system-dlls like comctl32, comdlg32 and
154 * shell32.
155 * If you have a better idea to figure this out...
157 DWORD VERSION_GetSystemDLLVersion (WINE_MODREF * wm)
159 PE_MODREF * pem = &(wm->binfmt.pe);
161 if (pem->pe_import)
163 IMAGE_IMPORT_DESCRIPTOR * pe_imp;
165 for ( pe_imp = pem->pe_import; pe_imp->Name; pe_imp++)
167 char * name = (char*)((unsigned int)wm->module+(unsigned int)(pe_imp->Name));
168 TRACE("%s\n", name);
170 if (!lstrncmpiA(name, "ntdll", 5))
172 if (3 == PE_HEADER(wm->module)->OptionalHeader.MajorOperatingSystemVersion)
173 return NT351;
174 else
175 return NT40;
179 return WIN95;
181 /**********************************************************************
182 * VERSION_GetLinkedDllVersion
184 * Some version data (not reliable!):
185 * linker/OS/image/subsys
187 * x.xx/1.00/0.00/3.10 Win32s (any version ?)
188 * 2.39/1.00/0.00/3.10 Win32s freecell.exe (any version)
189 * 2.50/1.00/4.00/4.00 Win32s 1.30 winhlp32.exe
190 * 2.60/3.51/3.51/3.51 NT351SP5 system dlls
191 * 2.60/3.51/3.51/4.00 NT351SP5 comctl32 dll
192 * 2.xx/1.00/0.00/4.00 Win95 system files
193 * x.xx/4.00/0.00/4.00 Win95 most applications
194 * 3.10/4.00/0.00/4.00 Win98 notepad
195 * x.xx/5.00/5.00/4.00 Win98 system dlls
196 * x.xx/4.00/4.00/4.00 NT 4 most apps
197 * 5.12/5.00/5.00/4.00 NT4+IE5 comctl32.dll
198 * 5.12/5.00/5.00/4.00 Win98 calc
199 * x.xx/5.00/5.00/4.00 win95/win98/NT4 IE5 files
201 DWORD VERSION_GetLinkedDllVersion(PDB *pdb)
203 WINE_MODREF *wm;
204 DWORD WinVersion = NB_WINDOWS_VERSIONS;
205 PIMAGE_OPTIONAL_HEADER ophd;
207 if (!pdb->exe_modref)
209 /* winn311 progs only link to user32 */
210 if (pdb->modref_list && pdb->modref_list->next)
211 return WIN95;
212 return WIN31;
214 /* First check the native dlls provided. These have to be
215 from one windows version */
216 for ( wm = pdb->modref_list; wm; wm=wm->next )
218 ophd = &(PE_HEADER(wm->module)->OptionalHeader);
220 TRACE("%s: %02x.%02x/%02x.%02x/%02x.%02x/%02x.%02x\n",
221 wm->modname,
222 ophd->MajorLinkerVersion, ophd->MinorLinkerVersion,
223 ophd->MajorOperatingSystemVersion, ophd->MinorOperatingSystemVersion,
224 ophd->MajorImageVersion, ophd->MinorImageVersion,
225 ophd->MajorSubsystemVersion, ophd->MinorSubsystemVersion);
227 /* test if it a external dll */
228 if ( !(wm->flags & WINE_MODREF_INTERNAL) )
230 int i;
231 for (i = 0; special_dlls[i]; i++)
233 /* test if it a special dll */
234 if (!lstrncmpiA(wm->modname, special_dlls[i], strlen(special_dlls[i]) ))
236 DWORD DllVersion = VERSION_GetSystemDLLVersion(wm);
237 if (WinVersion == NB_WINDOWS_VERSIONS)
239 WinVersion = DllVersion;
241 else
243 if (WinVersion != DllVersion)
245 ERR("You mixed system dlls from different windows versions! Expect a chrash!\n");
246 return WIN31; /* this may let the exe exiting */
249 break;
255 if(WinVersion != NB_WINDOWS_VERSIONS) return WinVersion;
257 /* we are using no external system dlls, look at the exe */
258 ophd = &(PE_HEADER(pdb->exe_modref->module)->OptionalHeader);
260 TRACE("-%s: %02x.%02x/%02x.%02x/%02x.%02x/%02x.%02x\n",
261 pdb->exe_modref->modname,
262 ophd->MajorLinkerVersion, ophd->MinorLinkerVersion,
263 ophd->MajorOperatingSystemVersion, ophd->MinorOperatingSystemVersion,
264 ophd->MajorImageVersion, ophd->MinorImageVersion,
265 ophd->MajorSubsystemVersion, ophd->MinorSubsystemVersion);
267 /* special nt 3.51 */
268 if (3 == ophd->MajorOperatingSystemVersion && 51 == ophd->MinorOperatingSystemVersion)
270 return NT351;
273 /* the MajorSubsystemVersion is the only usable singn */
274 if (ophd->MajorSubsystemVersion < 4)
276 if ( ophd->MajorOperatingSystemVersion == 1
277 && ophd->MinorOperatingSystemVersion == 0)
279 return WIN31; /* win32s */
282 if (ophd->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
283 return NT351; /* FIXME: NT 3.1, not tested */
284 else
285 return WIN95;
288 return WIN95;
291 /**********************************************************************
292 * VERSION_GetVersion
294 * WARNING !!!
295 * Don't call this function too early during the Wine init,
296 * as pdb->exe_modref (required by VERSION_GetImageVersion()) might still
297 * be NULL in such cases, which causes the winver to ALWAYS be detected
298 * as WIN31.
299 * And as we cache the winver once it has been determined, this is bad.
300 * This can happen much easier than you might think, as this function
301 * is called by EVERY GetVersion*() API !
304 WINDOWS_VERSION VERSION_GetVersion(void)
306 PDB *pdb = PROCESS_Current();
308 if (versionForced) /* user has overridden any sensible checks */
309 return defaultWinVersion;
311 if (pdb->winver == 0xffff) /* to be determined */
313 pdb->winver = VERSION_GetLinkedDllVersion(pdb);
314 TRACE("Autodetected: %s\n", VERSION_GetVersionName());
317 return pdb->winver;
321 /**********************************************************************
322 * VERSION_GetVersionName
324 char *VERSION_GetVersionName()
326 WINDOWS_VERSION ver = VERSION_GetVersion();
327 switch(ver)
329 case WIN31:
330 return "Windows 3.1";
331 case WIN95:
332 return "Windows 95";
333 case WIN98:
334 return "Windows 98";
335 case NT351:
336 return "Windows NT 3.51";
337 case NT40:
338 return "Windows NT 4.0";
339 default:
340 FIXME("Windows version %d not named",ver);
341 return "Windows <Unknown>";
345 /***********************************************************************
346 * GetVersion16 (KERNEL.3)
348 LONG WINAPI GetVersion16(void)
350 WINDOWS_VERSION ver = VERSION_GetVersion();
351 return VersionData[ver].getVersion16;
355 /***********************************************************************
356 * GetVersion32 (KERNEL32.427)
358 LONG WINAPI GetVersion(void)
360 WINDOWS_VERSION ver = VERSION_GetVersion();
361 return VersionData[ver].getVersion32;
365 /***********************************************************************
366 * GetVersionEx16 (KERNEL.149)
368 BOOL16 WINAPI GetVersionEx16(OSVERSIONINFO16 *v)
370 WINDOWS_VERSION ver = VERSION_GetVersion();
371 if (v->dwOSVersionInfoSize != sizeof(OSVERSIONINFO16))
373 WARN("wrong OSVERSIONINFO size from app");
374 SetLastError(ERROR_INSUFFICIENT_BUFFER);
375 return FALSE;
377 v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
378 v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
379 v->dwBuildNumber = VersionData[ver].getVersionEx.dwBuildNumber;
380 v->dwPlatformId = VersionData[ver].getVersionEx.dwPlatformId;
381 strcpy( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
382 return TRUE;
386 /***********************************************************************
387 * GetVersionEx32A (KERNEL32.428)
389 BOOL WINAPI GetVersionExA(OSVERSIONINFOA *v)
391 WINDOWS_VERSION ver = VERSION_GetVersion();
392 if (v->dwOSVersionInfoSize != sizeof(OSVERSIONINFOA))
394 WARN("wrong OSVERSIONINFO size from app (got: %ld, expected: %d)",
395 v->dwOSVersionInfoSize, sizeof(OSVERSIONINFOA));
396 SetLastError(ERROR_INSUFFICIENT_BUFFER);
397 return FALSE;
399 v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
400 v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
401 v->dwBuildNumber = VersionData[ver].getVersionEx.dwBuildNumber;
402 v->dwPlatformId = VersionData[ver].getVersionEx.dwPlatformId;
403 strcpy( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
404 return TRUE;
408 /***********************************************************************
409 * GetVersionEx32W (KERNEL32.429)
411 BOOL WINAPI GetVersionExW(OSVERSIONINFOW *v)
413 WINDOWS_VERSION ver = VERSION_GetVersion();
415 if (v->dwOSVersionInfoSize!=sizeof(OSVERSIONINFOW))
417 WARN("wrong OSVERSIONINFO size from app (got: %ld, expected: %d)",
418 v->dwOSVersionInfoSize, sizeof(OSVERSIONINFOW));
419 SetLastError(ERROR_INSUFFICIENT_BUFFER);
420 return FALSE;
422 v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
423 v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
424 v->dwBuildNumber = VersionData[ver].getVersionEx.dwBuildNumber;
425 v->dwPlatformId = VersionData[ver].getVersionEx.dwPlatformId;
426 lstrcpyAtoW( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
427 return TRUE;
431 /***********************************************************************
432 * GetWinFlags (KERNEL.132)
434 DWORD WINAPI GetWinFlags16(void)
436 static const long cpuflags[5] =
437 { WF_CPU086, WF_CPU186, WF_CPU286, WF_CPU386, WF_CPU486 };
438 SYSTEM_INFO si;
439 OSVERSIONINFOA ovi;
440 DWORD result;
442 GetSystemInfo(&si);
444 /* There doesn't seem to be any Pentium flag. */
445 result = cpuflags[MIN (si.wProcessorLevel, 4)];
447 switch(Options.mode)
449 case MODE_STANDARD:
450 result |= WF_STANDARD | WF_PMODE | WF_80x87;
451 break;
453 case MODE_ENHANCED:
454 result |= WF_ENHANCED | WF_PMODE | WF_80x87 | WF_PAGING;
455 break;
457 default:
458 ERR("Unknown mode set? This shouldn't happen. Check GetWinFlags()!\n");
459 break;
461 if (si.wProcessorLevel >= 4) result |= WF_HASCPUID;
462 ovi.dwOSVersionInfoSize = sizeof(ovi);
463 GetVersionExA(&ovi);
464 if (ovi.dwPlatformId == VER_PLATFORM_WIN32_NT)
465 result |= WF_WIN32WOW; /* undocumented WF_WINNT */
466 return result;
470 /***********************************************************************
471 * GetWinDebugInfo (KERNEL.355)
473 BOOL16 WINAPI GetWinDebugInfo16(WINDEBUGINFO *lpwdi, UINT16 flags)
475 FIXME("(%8lx,%d): stub returning 0\n",
476 (unsigned long)lpwdi, flags);
477 /* 0 means not in debugging mode/version */
478 /* Can this type of debugging be used in wine ? */
479 /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
480 return 0;
484 /***********************************************************************
485 * SetWinDebugInfo (KERNEL.356)
487 BOOL16 WINAPI SetWinDebugInfo16(WINDEBUGINFO *lpwdi)
489 FIXME("(%8lx): stub returning 0\n", (unsigned long)lpwdi);
490 /* 0 means not in debugging mode/version */
491 /* Can this type of debugging be used in wine ? */
492 /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
493 return 0;
497 /***********************************************************************
498 * DebugFillBuffer (KERNEL.329)
500 * TODO:
501 * Should fill lpBuffer only if DBO_BUFFERFILL has been set by SetWinDebugInfo()
503 void WINAPI DebugFillBuffer(LPSTR lpBuffer, WORD wBytes)
505 memset(lpBuffer, DBGFILL_BUFFER, wBytes);
508 /***********************************************************************
509 * DiagQuery (KERNEL.339)
511 * returns TRUE if Win called with "/b" (bootlog.txt)
513 BOOL16 WINAPI DiagQuery16()
515 /* perhaps implement a Wine "/b" command line flag sometime ? */
516 return FALSE;
519 /***********************************************************************
520 * DiagOutput (KERNEL.340)
522 * writes a debug string into <windir>\bootlog.txt
524 void WINAPI DiagOutput16(LPCSTR str)
526 /* FIXME */
527 DPRINTF("DIAGOUTPUT:%s\n", debugstr_a(str));
530 /***********************************************************************
531 * OaBuildVersion [OLEAUT32.170]
533 UINT WINAPI OaBuildVersion()
535 WINDOWS_VERSION ver = VERSION_GetVersion();
537 FIXME("Please report to a.mohr@mailto.de if you get version error messages !\n");
538 switch(VersionData[ver].getVersion32)
540 case 0x80000a03: /* Win 3.1 */
541 return 0x140fd1; /* from Win32s 1.1e */
542 case 0xc0000004: /* Win 95 */
543 case 0xc0000a04: /* Win 98: verified same as Win95 */
544 return 0x1e10a9; /* some older version: 0x0a0bd3 */
545 case 0x04213303: /* NT 3.51 */
546 FIXME("NT 3.51 version value unknown !\n");
547 return 0x1e10a9; /* value borrowed from Win95 */
548 case 0x05650004: /* NT 4.0 */
549 return 0x141016;
550 default:
551 return 0x0;
554 /***********************************************************************
555 * VERSION_OsIsUnicode [internal]
557 * NOTES
558 * some functions getting sometimes LPSTR sometimes LPWSTR...
561 BOOL VERSION_OsIsUnicode(void)
563 switch(VERSION_GetVersion())
565 case NT351:
566 case NT40:
567 return TRUE;
568 default:
569 return FALSE;