Now C tests are able to access argc/argv as passed to the program.
[wine.git] / misc / version.c
blob0925d74197f72a346cc6a11f03351ee4010a766a
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
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <string.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "wine/winbase16.h"
32 #include "module.h"
33 #include "options.h"
34 #include "wine/debug.h"
35 #include "winerror.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ver);
39 typedef enum
41 WIN20, /* Windows 2.0 */
42 WIN30, /* Windows 3.0 */
43 WIN31, /* Windows 3.1 */
44 WIN95, /* Windows 95 */
45 WIN98, /* Windows 98 */
46 WINME, /* Windows Me */
47 NT351, /* Windows NT 3.51 */
48 NT40, /* Windows NT 4.0 */
49 NT2K, /* Windows 2000 */
50 WINXP, /* Windows XP */
51 NB_WINDOWS_VERSIONS
52 } WINDOWS_VERSION;
54 typedef struct
56 LONG getVersion16;
57 LONG getVersion32;
58 OSVERSIONINFOA getVersionEx;
59 } VERSION_DATA;
61 /* FIXME: compare values below with original and fix */
62 static VERSION_DATA VersionData[NB_WINDOWS_VERSIONS] =
64 /* WIN20 FIXME: verify values */
66 MAKELONG( 0x0002, 0x0303 ), /* assume DOS 3.3 */
67 MAKELONG( 0x0003, 0x8000 ),
69 sizeof(OSVERSIONINFOA), 2, 0, 0,
70 VER_PLATFORM_WIN32s, "Win32s 1.3"
73 /* WIN30 FIXME: verify values */
75 MAKELONG( 0x0003, 0x0500 ), /* assume DOS 5.00 */
76 MAKELONG( 0x0003, 0x8000 ),
78 sizeof(OSVERSIONINFOA), 3, 0, 0,
79 VER_PLATFORM_WIN32s, "Win32s 1.3"
82 /* WIN31 */
84 MAKELONG( 0x0a03, 0x0616 ), /* DOS 6.22 */
85 MAKELONG( 0x0a03, 0x8000 ),
87 sizeof(OSVERSIONINFOA), 3, 10, 0,
88 VER_PLATFORM_WIN32s, "Win32s 1.3"
91 /* WIN95 */
93 0x07005F03,
94 0xC0000004,
96 /* Win95: 4, 0, 0x40003B6, ""
97 * Win95sp1: 4, 0, 0x40003B6, " A " (according to doc)
98 * Win95osr2: 4, 0, 0x4000457, " B " (according to doc)
99 * Win95osr2.1: 4, 3, 0x40304BC, " B " (according to doc)
100 * Win95osr2.5: 4, 3, 0x40304BE, " C " (according to doc)
101 * Win95a/b can be discerned via regkey SubVersionNumber
102 * See also:
103 * http://support.microsoft.com/support/kb/articles/q158/2/38.asp
105 sizeof(OSVERSIONINFOA), 4, 0, 0x40003B6,
106 VER_PLATFORM_WIN32_WINDOWS, ""
109 /* WIN98 */
111 0x070A5F03,
112 0xC0000A04,
114 /* Win98: 4, 10, 0x40A07CE, " "
115 * Win98SE: 4, 10, 0x40A08AE, " A "
117 sizeof(OSVERSIONINFOA), 4, 10, 0x40A07CE,
118 VER_PLATFORM_WIN32_WINDOWS, " "
121 /* WINME */
123 0x07005F03, /* Assuming DOS 7 like the other Win9x */
124 0xC0005A04,
126 sizeof(OSVERSIONINFOA), 4, 90, 0x45A0BB8,
127 VER_PLATFORM_WIN32_WINDOWS, " "
130 /* NT351 */
132 0x05000A03,
133 0x04213303,
135 sizeof(OSVERSIONINFOA), 3, 51, 0x421,
136 VER_PLATFORM_WIN32_NT, "Service Pack 2"
139 /* NT40 */
141 0x05000A03,
142 0x05650004,
144 sizeof(OSVERSIONINFOA), 4, 0, 0x565,
145 VER_PLATFORM_WIN32_NT, "Service Pack 6"
148 /* NT2K */
150 0x05005F03,
151 0x08930005,
153 sizeof(OSVERSIONINFOA), 5, 0, 0x893,
154 VER_PLATFORM_WIN32_NT, "Service Pack 2"
157 /* WINXP */
159 0x05005F03, /* Assuming DOS 5 like the other NT */
160 0x0A280105,
162 sizeof(OSVERSIONINFOA), 5, 1, 0xA28,
163 VER_PLATFORM_WIN32_NT, ""
168 static const char *WinVersionNames[NB_WINDOWS_VERSIONS] =
169 { /* no spaces in here ! */
170 "win20",
171 "win30",
172 "win31",
173 "win95",
174 "win98",
175 "winme",
176 "nt351",
177 "nt40",
178 "win2000,win2k,nt2k,nt2000",
179 "winxp"
182 /* if one of the following dlls is importing ntdll the windows
183 version autodetection switches wine to unicode (nt 3.51 or 4.0) */
184 static char * special_dlls[] =
186 "COMDLG32",
187 "COMCTL32",
188 "SHELL32",
189 "OLE32",
190 "RPCRT4",
191 NULL
194 /* the current version has not been autodetected but forced via cmdline */
195 static BOOL versionForced = FALSE;
196 static WINDOWS_VERSION defaultWinVersion = WIN31;
198 /**********************************************************************
199 * VERSION_ParseWinVersion
201 void VERSION_ParseWinVersion( const char *arg )
203 int i, len;
204 const char *pCurr, *p;
205 for (i = 0; i < NB_WINDOWS_VERSIONS; i++)
207 pCurr = WinVersionNames[i];
208 /* iterate through all winver aliases separated by comma */
209 do {
210 p = strchr(pCurr, ',');
211 len = p ? (int)p - (int)pCurr : strlen(pCurr);
212 if ( (!strncmp( pCurr, arg, len )) && (arg[len] == '\0') )
214 defaultWinVersion = (WINDOWS_VERSION)i;
215 versionForced = TRUE;
216 return;
218 pCurr = p+1;
219 } while (p);
221 MESSAGE("Invalid winver value '%s' specified.\n", arg );
222 MESSAGE("Valid versions are:" );
223 for (i = 0; i < NB_WINDOWS_VERSIONS; i++)
225 /* only list the first, "official" alias in case of aliases */
226 pCurr = WinVersionNames[i];
227 p = strchr(pCurr, ',');
228 len = (p) ? (int)p - (int)pCurr : strlen(pCurr);
230 MESSAGE(" '%.*s'%c", len, pCurr,
231 (i == NB_WINDOWS_VERSIONS - 1) ? '\n' : ',' );
233 ExitProcess(1);
237 /**********************************************************************
238 * VERSION_ParseDosVersion
240 void VERSION_ParseDosVersion( const char *arg )
242 int hi, lo;
243 if (sscanf( arg, "%d.%d", &hi, &lo ) == 2)
245 VersionData[WIN31].getVersion16 =
246 MAKELONG(LOWORD(VersionData[WIN31].getVersion16),
247 (hi<<8) + lo);
249 else
251 MESSAGE("--dosver: Wrong version format. Use \"--dosver x.xx\"\n");
252 ExitProcess(1);
256 /**********************************************************************
257 * VERSION_GetSystemDLLVersion
259 * This function tries to figure out if a given (native) dll comes from
260 * win95/98 or winnt. Since all values in the OptionalHeader are not a
261 * usable hint, we test if a dll imports the ntdll.
262 * This is at least working for all system dlls like comctl32, comdlg32 and
263 * shell32.
264 * If you have a better idea to figure this out...
266 static DWORD VERSION_GetSystemDLLVersion( HMODULE hmod )
268 IMAGE_DATA_DIRECTORY *dir = PE_HEADER(hmod)->OptionalHeader.DataDirectory
269 + IMAGE_DIRECTORY_ENTRY_IMPORT;
270 if (dir->Size && dir->VirtualAddress)
272 IMAGE_IMPORT_DESCRIPTOR *pe_imp = (IMAGE_IMPORT_DESCRIPTOR *)((char *)hmod + dir->VirtualAddress);
273 for ( ; pe_imp->Name; pe_imp++)
275 char * name = (char *)hmod + (unsigned int)pe_imp->Name;
276 TRACE("%s\n", name);
278 if (!strncasecmp(name, "ntdll", 5))
280 if (3 == PE_HEADER(hmod)->OptionalHeader.MajorOperatingSystemVersion)
281 return NT351;
282 else
283 return NT40;
287 return WIN95;
289 /**********************************************************************
290 * VERSION_GetLinkedDllVersion
292 * Some version data (not reliable!):
293 * linker/OS/image/subsys
295 * x.xx/1.00/0.00/3.10 Win32s (any version ?)
296 * 2.39/1.00/0.00/3.10 Win32s freecell.exe (any version)
297 * 2.50/1.00/4.00/4.00 Win32s 1.30 winhlp32.exe
298 * 2.60/3.51/3.51/3.51 NT351SP5 system dlls
299 * 2.60/3.51/3.51/4.00 NT351SP5 comctl32 dll
300 * 2.xx/1.00/0.00/4.00 Win95 system files
301 * x.xx/4.00/0.00/4.00 Win95 most applications
302 * 3.10/4.00/0.00/4.00 Win98 notepad
303 * x.xx/5.00/5.00/4.00 Win98 system dlls
304 * x.xx/4.00/4.00/4.00 NT 4 most apps
305 * 5.12/5.00/5.00/4.00 NT4+IE5 comctl32.dll
306 * 5.12/5.00/5.00/4.00 Win98 calc
307 * x.xx/5.00/5.00/4.00 win95/win98/NT4 IE5 files
309 DWORD VERSION_GetLinkedDllVersion(void)
311 WINE_MODREF *wm;
312 DWORD WinVersion = NB_WINDOWS_VERSIONS;
313 PIMAGE_OPTIONAL_HEADER ophd;
315 /* First check the native dlls provided. These have to be
316 from one windows version */
317 for ( wm = MODULE_modref_list; wm; wm=wm->next )
319 ophd = &(PE_HEADER(wm->module)->OptionalHeader);
321 TRACE("%s: %02x.%02x/%02x.%02x/%02x.%02x/%02x.%02x\n",
322 wm->modname,
323 ophd->MajorLinkerVersion, ophd->MinorLinkerVersion,
324 ophd->MajorOperatingSystemVersion, ophd->MinorOperatingSystemVersion,
325 ophd->MajorImageVersion, ophd->MinorImageVersion,
326 ophd->MajorSubsystemVersion, ophd->MinorSubsystemVersion);
328 /* test if it is an external (native) dll */
329 if (!(wm->flags & WINE_MODREF_INTERNAL))
331 int i;
332 for (i = 0; special_dlls[i]; i++)
334 /* test if it is a special dll */
335 if (!strncasecmp(wm->modname, special_dlls[i], strlen(special_dlls[i]) ))
337 DWORD DllVersion = VERSION_GetSystemDLLVersion(wm->module);
338 if (WinVersion == NB_WINDOWS_VERSIONS)
339 WinVersion = DllVersion;
340 else {
341 if (WinVersion != DllVersion) {
342 ERR("You mixed system dlls from different windows versions! Expect a crash! (%s: expected version '%s', but is '%s')\n",
343 wm->modname,
344 VersionData[WinVersion].getVersionEx.szCSDVersion,
345 VersionData[DllVersion].getVersionEx.szCSDVersion);
346 return WIN31; /* this may let the exe exiting */
349 break;
355 if(WinVersion != NB_WINDOWS_VERSIONS) return WinVersion;
357 /* we are using no external system dlls, look at the exe */
358 ophd = &(PE_HEADER(GetModuleHandleA(NULL))->OptionalHeader);
360 TRACE("%02x.%02x/%02x.%02x/%02x.%02x/%02x.%02x\n",
361 ophd->MajorLinkerVersion, ophd->MinorLinkerVersion,
362 ophd->MajorOperatingSystemVersion, ophd->MinorOperatingSystemVersion,
363 ophd->MajorImageVersion, ophd->MinorImageVersion,
364 ophd->MajorSubsystemVersion, ophd->MinorSubsystemVersion);
366 /* special nt 3.51 */
367 if (3 == ophd->MajorOperatingSystemVersion && 51 == ophd->MinorOperatingSystemVersion)
369 return NT351;
372 /* the MajorSubsystemVersion is the only usable sign */
373 if (ophd->MajorSubsystemVersion < 4)
375 if ( ophd->MajorOperatingSystemVersion == 1
376 && ophd->MinorOperatingSystemVersion == 0)
378 return WIN31; /* win32s */
381 if (ophd->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
382 return NT351; /* FIXME: NT 3.1, not tested */
383 else
384 return WIN95;
387 return WIN95;
390 /**********************************************************************
391 * VERSION_GetVersion
393 * WARNING !!!
394 * Don't call this function too early during the Wine init,
395 * as pdb->exe_modref (required by VERSION_GetImageVersion()) might still
396 * be NULL in such cases, which causes the winver to ALWAYS be detected
397 * as WIN31.
398 * And as we cache the winver once it has been determined, this is bad.
399 * This can happen much easier than you might think, as this function
400 * is called by EVERY GetVersion*() API !
403 static WINDOWS_VERSION VERSION_GetVersion(void)
405 static WORD winver = 0xffff;
407 if (winver == 0xffff) /* to be determined */
409 WINDOWS_VERSION retver;
411 if (versionForced) /* user has overridden any sensible checks */
412 winver = defaultWinVersion;
413 else
415 retver = VERSION_GetLinkedDllVersion();
417 /* cache determined value, but do not store in case of WIN31 */
418 if (retver != WIN31) winver = retver;
420 return retver;
424 return winver;
428 /***********************************************************************
429 * GetVersion (KERNEL.3)
431 LONG WINAPI GetVersion16(void)
433 WINDOWS_VERSION ver = VERSION_GetVersion();
434 return VersionData[ver].getVersion16;
438 /***********************************************************************
439 * GetVersion (KERNEL32.@)
441 LONG WINAPI GetVersion(void)
443 WINDOWS_VERSION ver = VERSION_GetVersion();
444 return VersionData[ver].getVersion32;
448 /***********************************************************************
449 * GetVersionEx (KERNEL.149)
451 BOOL16 WINAPI GetVersionEx16(OSVERSIONINFO16 *v)
453 WINDOWS_VERSION ver = VERSION_GetVersion();
454 if (v->dwOSVersionInfoSize < sizeof(OSVERSIONINFO16))
456 WARN("wrong OSVERSIONINFO size from app\n");
457 SetLastError(ERROR_INSUFFICIENT_BUFFER);
458 return FALSE;
460 v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
461 v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
462 v->dwBuildNumber = VersionData[ver].getVersionEx.dwBuildNumber;
463 v->dwPlatformId = VersionData[ver].getVersionEx.dwPlatformId;
464 strcpy( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
465 return TRUE;
469 /***********************************************************************
470 * GetVersionExA (KERNEL32.@)
472 BOOL WINAPI GetVersionExA(OSVERSIONINFOA *v)
474 WINDOWS_VERSION ver = VERSION_GetVersion();
475 if (v->dwOSVersionInfoSize < sizeof(OSVERSIONINFOA))
477 WARN("wrong OSVERSIONINFO size from app (got: %ld, expected: %d)\n",
478 v->dwOSVersionInfoSize, sizeof(OSVERSIONINFOA));
479 SetLastError(ERROR_INSUFFICIENT_BUFFER);
480 return FALSE;
482 v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
483 v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
484 v->dwBuildNumber = VersionData[ver].getVersionEx.dwBuildNumber;
485 v->dwPlatformId = VersionData[ver].getVersionEx.dwPlatformId;
486 strcpy( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
487 return TRUE;
491 /***********************************************************************
492 * GetVersionExW (KERNEL32.@)
494 BOOL WINAPI GetVersionExW(OSVERSIONINFOW *v)
496 WINDOWS_VERSION ver = VERSION_GetVersion();
498 if (v->dwOSVersionInfoSize < sizeof(OSVERSIONINFOW))
500 WARN("wrong OSVERSIONINFO size from app (got: %ld, expected: %d)\n",
501 v->dwOSVersionInfoSize, sizeof(OSVERSIONINFOW));
502 SetLastError(ERROR_INSUFFICIENT_BUFFER);
503 return FALSE;
505 v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
506 v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
507 v->dwBuildNumber = VersionData[ver].getVersionEx.dwBuildNumber;
508 v->dwPlatformId = VersionData[ver].getVersionEx.dwPlatformId;
509 MultiByteToWideChar( CP_ACP, 0, VersionData[ver].getVersionEx.szCSDVersion, -1,
510 v->szCSDVersion, sizeof(v->szCSDVersion)/sizeof(WCHAR) );
511 return TRUE;
515 /***********************************************************************
516 * GetWinFlags (KERNEL.132)
518 DWORD WINAPI GetWinFlags16(void)
520 static const long cpuflags[5] =
521 { WF_CPU086, WF_CPU186, WF_CPU286, WF_CPU386, WF_CPU486 };
522 SYSTEM_INFO si;
523 OSVERSIONINFOA ovi;
524 DWORD result;
526 GetSystemInfo(&si);
528 /* There doesn't seem to be any Pentium flag. */
529 result = cpuflags[min(si.wProcessorLevel, 4)] | WF_ENHANCED | WF_PMODE | WF_80x87 | WF_PAGING;
530 if (si.wProcessorLevel >= 4) result |= WF_HASCPUID;
531 ovi.dwOSVersionInfoSize = sizeof(ovi);
532 GetVersionExA(&ovi);
533 if (ovi.dwPlatformId == VER_PLATFORM_WIN32_NT)
534 result |= WF_WIN32WOW; /* undocumented WF_WINNT */
535 return result;
539 #if 0
540 /* Not used at this time. This is here for documentation only */
542 /* WINDEBUGINFO flags values */
543 #define WDI_OPTIONS 0x0001
544 #define WDI_FILTER 0x0002
545 #define WDI_ALLOCBREAK 0x0004
547 /* dwOptions values */
548 #define DBO_CHECKHEAP 0x0001
549 #define DBO_BUFFERFILL 0x0004
550 #define DBO_DISABLEGPTRAPPING 0x0010
551 #define DBO_CHECKFREE 0x0020
553 #define DBO_SILENT 0x8000
555 #define DBO_TRACEBREAK 0x2000
556 #define DBO_WARNINGBREAK 0x1000
557 #define DBO_NOERRORBREAK 0x0800
558 #define DBO_NOFATALBREAK 0x0400
559 #define DBO_INT3BREAK 0x0100
561 /* DebugOutput flags values */
562 #define DBF_TRACE 0x0000
563 #define DBF_WARNING 0x4000
564 #define DBF_ERROR 0x8000
565 #define DBF_FATAL 0xc000
567 /* dwFilter values */
568 #define DBF_KERNEL 0x1000
569 #define DBF_KRN_MEMMAN 0x0001
570 #define DBF_KRN_LOADMODULE 0x0002
571 #define DBF_KRN_SEGMENTLOAD 0x0004
572 #define DBF_USER 0x0800
573 #define DBF_GDI 0x0400
574 #define DBF_MMSYSTEM 0x0040
575 #define DBF_PENWIN 0x0020
576 #define DBF_APPLICATION 0x0008
577 #define DBF_DRIVER 0x0010
579 #endif /* NOLOGERROR */
582 /***********************************************************************
583 * GetWinDebugInfo (KERNEL.355)
585 BOOL16 WINAPI GetWinDebugInfo16(WINDEBUGINFO16 *lpwdi, UINT16 flags)
587 FIXME("(%8lx,%d): stub returning 0\n",
588 (unsigned long)lpwdi, flags);
589 /* 0 means not in debugging mode/version */
590 /* Can this type of debugging be used in wine ? */
591 /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
592 return 0;
596 /***********************************************************************
597 * SetWinDebugInfo (KERNEL.356)
599 BOOL16 WINAPI SetWinDebugInfo16(WINDEBUGINFO16 *lpwdi)
601 FIXME("(%8lx): stub returning 0\n", (unsigned long)lpwdi);
602 /* 0 means not in debugging mode/version */
603 /* Can this type of debugging be used in wine ? */
604 /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
605 return 0;
609 /***********************************************************************
610 * K329 (KERNEL.329)
612 * TODO:
613 * Should fill lpBuffer only if DBO_BUFFERFILL has been set by SetWinDebugInfo()
615 void WINAPI DebugFillBuffer(LPSTR lpBuffer, WORD wBytes)
617 memset(lpBuffer, DBGFILL_BUFFER, wBytes);
620 /***********************************************************************
621 * DiagQuery (KERNEL.339)
623 * returns TRUE if Win called with "/b" (bootlog.txt)
625 BOOL16 WINAPI DiagQuery16()
627 /* perhaps implement a Wine "/b" command line flag sometime ? */
628 return FALSE;
631 /***********************************************************************
632 * DiagOutput (KERNEL.340)
634 * writes a debug string into <windir>\bootlog.txt
636 void WINAPI DiagOutput16(LPCSTR str)
638 /* FIXME */
639 DPRINTF("DIAGOUTPUT:%s\n", debugstr_a(str));