Do not free the not owned error string.
[wine.git] / misc / version.c
blob3671ce2906b392fff8edb5c8968bd251d9089700
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 <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 "module.h"
19 #include "options.h"
20 #include "debugtools.h"
21 #include "winerror.h"
23 DEFAULT_DEBUG_CHANNEL(ver);
25 typedef enum
27 WIN20, /* Windows 2.0 */
28 WIN30, /* Windows 3.0 */
29 WIN31, /* Windows 3.1 */
30 WIN95, /* Windows 95 */
31 WIN98, /* Windows 98 */
32 /* insert Windows ME here as WINME if needed */
33 NT351, /* Windows NT 3.51 */
34 NT40, /* Windows NT 4.0 */
35 NT2K, /* Windows 2000 */
36 NB_WINDOWS_VERSIONS
37 } WINDOWS_VERSION;
39 typedef struct
41 LONG getVersion16;
42 LONG getVersion32;
43 OSVERSIONINFOA getVersionEx;
44 } VERSION_DATA;
46 /* FIXME: compare values below with original and fix */
47 static VERSION_DATA VersionData[NB_WINDOWS_VERSIONS] =
49 /* WIN20 FIXME: verify values */
51 MAKELONG( 0x0002, 0x0303 ), /* assume DOS 3.3 */
52 MAKELONG( 0x0003, 0x8000 ),
54 sizeof(OSVERSIONINFOA), 2, 0, 0,
55 VER_PLATFORM_WIN32s, "Win32s 1.3"
58 /* WIN30 FIXME: verify values */
60 MAKELONG( 0x0003, 0x0500 ), /* assume DOS 5.00 */
61 MAKELONG( 0x0003, 0x8000 ),
63 sizeof(OSVERSIONINFOA), 3, 0, 0,
64 VER_PLATFORM_WIN32s, "Win32s 1.3"
67 /* WIN31 */
69 MAKELONG( 0x0a03, 0x0616 ), /* DOS 6.22 */
70 MAKELONG( 0x0a03, 0x8000 ),
72 sizeof(OSVERSIONINFOA), 3, 10, 0,
73 VER_PLATFORM_WIN32s, "Win32s 1.3"
76 /* WIN95 */
78 0x07005F03,
79 0xC0000004,
81 sizeof(OSVERSIONINFOA), 4, 0, 0x40003B6,
82 VER_PLATFORM_WIN32_WINDOWS, "Win95"
85 /* WIN98 */
87 0x070A5F03,
88 0xC0000A04,
90 sizeof(OSVERSIONINFOA), 4, 10, 0x40A07CE,
91 VER_PLATFORM_WIN32_WINDOWS, "Win98"
94 /* NT351 */
96 0x05000A03,
97 0x04213303,
99 sizeof(OSVERSIONINFOA), 3, 51, 0x421,
100 VER_PLATFORM_WIN32_NT, "Service Pack 2"
103 /* NT40 */
105 0x05000A03,
106 0x05650004,
108 sizeof(OSVERSIONINFOA), 4, 0, 0x565,
109 VER_PLATFORM_WIN32_NT, "Service Pack 6"
112 /* NT2K FIXME: verify values */
114 0x05000A03, /* ? */
115 0x08930005, /* better build ? using 2195 (final) for now */
117 sizeof(OSVERSIONINFOA), 5, 0, 0x893,
118 VER_PLATFORM_WIN32_NT, "FIXME: OS version string not known yet"
123 static const char *WinVersionNames[NB_WINDOWS_VERSIONS] =
124 { /* no spaces in here ! */
125 "win20",
126 "win30",
127 "win31",
128 "win95",
129 "win98",
130 "nt351",
131 "nt40",
132 "win2000,win2k,nt2k,nt2000"
135 /* if one of the following dlls is importing ntdll the windows
136 version autodetection switches wine to unicode (nt 3.51 or 4.0) */
137 static char * special_dlls[] =
139 "COMDLG32",
140 "COMCTL32",
141 "SHELL32",
142 "OLE32",
143 "RPCRT4",
144 NULL
147 /* the current version has not been autodetected but forced via cmdline */
148 static BOOL versionForced = FALSE;
149 static WINDOWS_VERSION defaultWinVersion = WIN31;
151 /**********************************************************************
152 * VERSION_ParseWinVersion
154 void VERSION_ParseWinVersion( const char *arg )
156 int i, len;
157 const char *pCurr, *p;
158 for (i = 0; i < NB_WINDOWS_VERSIONS; i++)
160 pCurr = WinVersionNames[i];
161 /* iterate through all winver aliases separated by comma */
162 do {
163 p = strchr(pCurr, ',');
164 len = p ? (int)p - (int)pCurr : strlen(pCurr);
165 if ( (!strncmp( pCurr, arg, len )) && (arg[len] == '\0') )
167 defaultWinVersion = (WINDOWS_VERSION)i;
168 versionForced = TRUE;
169 return;
171 pCurr = p+1;
172 } while (p);
174 MESSAGE("Invalid winver value '%s' specified.\n", arg );
175 MESSAGE("Valid versions are:" );
176 for (i = 0; i < NB_WINDOWS_VERSIONS; i++)
178 /* only list the first, "official" alias in case of aliases */
179 pCurr = WinVersionNames[i];
180 p = strchr(pCurr, ',');
181 len = (p) ? (int)p - (int)pCurr : strlen(pCurr);
183 MESSAGE(" '%.*s'%c", len, pCurr,
184 (i == NB_WINDOWS_VERSIONS - 1) ? '\n' : ',' );
186 ExitProcess(1);
190 /**********************************************************************
191 * VERSION_ParseDosVersion
193 void VERSION_ParseDosVersion( const char *arg )
195 int hi, lo;
196 if (sscanf( arg, "%d.%d", &hi, &lo ) == 2)
198 VersionData[WIN31].getVersion16 =
199 MAKELONG(LOWORD(VersionData[WIN31].getVersion16),
200 (hi<<8) + lo);
202 else
204 MESSAGE("--dosver: Wrong version format. Use \"--dosver x.xx\"\n");
205 ExitProcess(1);
209 /**********************************************************************
210 * VERSION_GetSystemDLLVersion
212 * This function tries to figure out if a given (native) dll comes from
213 * win95/98 or winnt. Since all values in the OptionalHeader are not a
214 * usable hint, we test if a dll imports the ntdll.
215 * This is at least working for all system dlls like comctl32, comdlg32 and
216 * shell32.
217 * If you have a better idea to figure this out...
219 static DWORD VERSION_GetSystemDLLVersion( HMODULE hmod )
221 IMAGE_DATA_DIRECTORY *dir = PE_HEADER(hmod)->OptionalHeader.DataDirectory
222 + IMAGE_DIRECTORY_ENTRY_IMPORT;
223 if (dir->Size && dir->VirtualAddress)
225 IMAGE_IMPORT_DESCRIPTOR *pe_imp = (IMAGE_IMPORT_DESCRIPTOR *)((char *)hmod + dir->VirtualAddress);
226 for ( ; pe_imp->Name; pe_imp++)
228 char * name = (char *)hmod + (unsigned int)pe_imp->Name;
229 TRACE("%s\n", name);
231 if (!strncasecmp(name, "ntdll", 5))
233 if (3 == PE_HEADER(hmod)->OptionalHeader.MajorOperatingSystemVersion)
234 return NT351;
235 else
236 return NT40;
240 return WIN95;
242 /**********************************************************************
243 * VERSION_GetLinkedDllVersion
245 * Some version data (not reliable!):
246 * linker/OS/image/subsys
248 * x.xx/1.00/0.00/3.10 Win32s (any version ?)
249 * 2.39/1.00/0.00/3.10 Win32s freecell.exe (any version)
250 * 2.50/1.00/4.00/4.00 Win32s 1.30 winhlp32.exe
251 * 2.60/3.51/3.51/3.51 NT351SP5 system dlls
252 * 2.60/3.51/3.51/4.00 NT351SP5 comctl32 dll
253 * 2.xx/1.00/0.00/4.00 Win95 system files
254 * x.xx/4.00/0.00/4.00 Win95 most applications
255 * 3.10/4.00/0.00/4.00 Win98 notepad
256 * x.xx/5.00/5.00/4.00 Win98 system dlls
257 * x.xx/4.00/4.00/4.00 NT 4 most apps
258 * 5.12/5.00/5.00/4.00 NT4+IE5 comctl32.dll
259 * 5.12/5.00/5.00/4.00 Win98 calc
260 * x.xx/5.00/5.00/4.00 win95/win98/NT4 IE5 files
262 DWORD VERSION_GetLinkedDllVersion(void)
264 WINE_MODREF *wm;
265 DWORD WinVersion = NB_WINDOWS_VERSIONS;
266 PIMAGE_OPTIONAL_HEADER ophd;
268 /* First check the native dlls provided. These have to be
269 from one windows version */
270 for ( wm = MODULE_modref_list; wm; wm=wm->next )
272 ophd = &(PE_HEADER(wm->module)->OptionalHeader);
274 TRACE("%s: %02x.%02x/%02x.%02x/%02x.%02x/%02x.%02x\n",
275 wm->modname,
276 ophd->MajorLinkerVersion, ophd->MinorLinkerVersion,
277 ophd->MajorOperatingSystemVersion, ophd->MinorOperatingSystemVersion,
278 ophd->MajorImageVersion, ophd->MinorImageVersion,
279 ophd->MajorSubsystemVersion, ophd->MinorSubsystemVersion);
281 /* test if it is an external (native) dll */
282 if (!(wm->flags & WINE_MODREF_INTERNAL))
284 int i;
285 for (i = 0; special_dlls[i]; i++)
287 /* test if it is a special dll */
288 if (!strncasecmp(wm->modname, special_dlls[i], strlen(special_dlls[i]) ))
290 DWORD DllVersion = VERSION_GetSystemDLLVersion(wm->module);
291 if (WinVersion == NB_WINDOWS_VERSIONS)
292 WinVersion = DllVersion;
293 else {
294 if (WinVersion != DllVersion) {
295 ERR("You mixed system dlls from different windows versions! Expect a crash! (%s: expected version '%s', but is '%s')\n",
296 wm->modname,
297 VersionData[WinVersion].getVersionEx.szCSDVersion,
298 VersionData[DllVersion].getVersionEx.szCSDVersion);
299 return WIN31; /* this may let the exe exiting */
302 break;
308 if(WinVersion != NB_WINDOWS_VERSIONS) return WinVersion;
310 /* we are using no external system dlls, look at the exe */
311 ophd = &(PE_HEADER(GetModuleHandleA(NULL))->OptionalHeader);
313 TRACE("%02x.%02x/%02x.%02x/%02x.%02x/%02x.%02x\n",
314 ophd->MajorLinkerVersion, ophd->MinorLinkerVersion,
315 ophd->MajorOperatingSystemVersion, ophd->MinorOperatingSystemVersion,
316 ophd->MajorImageVersion, ophd->MinorImageVersion,
317 ophd->MajorSubsystemVersion, ophd->MinorSubsystemVersion);
319 /* special nt 3.51 */
320 if (3 == ophd->MajorOperatingSystemVersion && 51 == ophd->MinorOperatingSystemVersion)
322 return NT351;
325 /* the MajorSubsystemVersion is the only usable sign */
326 if (ophd->MajorSubsystemVersion < 4)
328 if ( ophd->MajorOperatingSystemVersion == 1
329 && ophd->MinorOperatingSystemVersion == 0)
331 return WIN31; /* win32s */
334 if (ophd->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
335 return NT351; /* FIXME: NT 3.1, not tested */
336 else
337 return WIN95;
340 return WIN95;
343 /**********************************************************************
344 * VERSION_GetVersion
346 * WARNING !!!
347 * Don't call this function too early during the Wine init,
348 * as pdb->exe_modref (required by VERSION_GetImageVersion()) might still
349 * be NULL in such cases, which causes the winver to ALWAYS be detected
350 * as WIN31.
351 * And as we cache the winver once it has been determined, this is bad.
352 * This can happen much easier than you might think, as this function
353 * is called by EVERY GetVersion*() API !
356 static WINDOWS_VERSION VERSION_GetVersion(void)
358 static WORD winver = 0xffff;
360 if (versionForced) /* user has overridden any sensible checks */
361 return defaultWinVersion;
363 if (winver == 0xffff) /* to be determined */ {
364 WINDOWS_VERSION retver = VERSION_GetLinkedDllVersion();
366 if (retver != WIN31) winver = retver;
367 return retver;
369 return winver;
373 /***********************************************************************
374 * GetVersion16 (KERNEL.3)
376 LONG WINAPI GetVersion16(void)
378 WINDOWS_VERSION ver = VERSION_GetVersion();
379 return VersionData[ver].getVersion16;
383 /***********************************************************************
384 * GetVersion (KERNEL32.427)
386 LONG WINAPI GetVersion(void)
388 WINDOWS_VERSION ver = VERSION_GetVersion();
389 return VersionData[ver].getVersion32;
393 /***********************************************************************
394 * GetVersionEx16 (KERNEL.149)
396 BOOL16 WINAPI GetVersionEx16(OSVERSIONINFO16 *v)
398 WINDOWS_VERSION ver = VERSION_GetVersion();
399 if (v->dwOSVersionInfoSize != sizeof(OSVERSIONINFO16))
401 WARN("wrong OSVERSIONINFO size from app");
402 SetLastError(ERROR_INSUFFICIENT_BUFFER);
403 return FALSE;
405 v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
406 v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
407 v->dwBuildNumber = VersionData[ver].getVersionEx.dwBuildNumber;
408 v->dwPlatformId = VersionData[ver].getVersionEx.dwPlatformId;
409 strcpy( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
410 return TRUE;
414 /***********************************************************************
415 * GetVersionExA (KERNEL32.428)
417 BOOL WINAPI GetVersionExA(OSVERSIONINFOA *v)
419 WINDOWS_VERSION ver = VERSION_GetVersion();
420 if (v->dwOSVersionInfoSize != sizeof(OSVERSIONINFOA))
422 WARN("wrong OSVERSIONINFO size from app (got: %ld, expected: %d)",
423 v->dwOSVersionInfoSize, sizeof(OSVERSIONINFOA));
424 SetLastError(ERROR_INSUFFICIENT_BUFFER);
425 return FALSE;
427 v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
428 v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
429 v->dwBuildNumber = VersionData[ver].getVersionEx.dwBuildNumber;
430 v->dwPlatformId = VersionData[ver].getVersionEx.dwPlatformId;
431 strcpy( v->szCSDVersion, VersionData[ver].getVersionEx.szCSDVersion );
432 return TRUE;
436 /***********************************************************************
437 * GetVersionExW (KERNEL32.429)
439 BOOL WINAPI GetVersionExW(OSVERSIONINFOW *v)
441 WINDOWS_VERSION ver = VERSION_GetVersion();
443 if (v->dwOSVersionInfoSize!=sizeof(OSVERSIONINFOW))
445 WARN("wrong OSVERSIONINFO size from app (got: %ld, expected: %d)",
446 v->dwOSVersionInfoSize, sizeof(OSVERSIONINFOW));
447 SetLastError(ERROR_INSUFFICIENT_BUFFER);
448 return FALSE;
450 v->dwMajorVersion = VersionData[ver].getVersionEx.dwMajorVersion;
451 v->dwMinorVersion = VersionData[ver].getVersionEx.dwMinorVersion;
452 v->dwBuildNumber = VersionData[ver].getVersionEx.dwBuildNumber;
453 v->dwPlatformId = VersionData[ver].getVersionEx.dwPlatformId;
454 MultiByteToWideChar( CP_ACP, 0, VersionData[ver].getVersionEx.szCSDVersion, -1,
455 v->szCSDVersion, sizeof(v->szCSDVersion)/sizeof(WCHAR) );
456 return TRUE;
460 /***********************************************************************
461 * GetWinFlags (KERNEL.132)
463 DWORD WINAPI GetWinFlags16(void)
465 static const long cpuflags[5] =
466 { WF_CPU086, WF_CPU186, WF_CPU286, WF_CPU386, WF_CPU486 };
467 SYSTEM_INFO si;
468 OSVERSIONINFOA ovi;
469 DWORD result;
471 GetSystemInfo(&si);
473 /* There doesn't seem to be any Pentium flag. */
474 result = cpuflags[min(si.wProcessorLevel, 4)] | WF_ENHANCED | WF_PMODE | WF_80x87 | WF_PAGING;
475 if (si.wProcessorLevel >= 4) result |= WF_HASCPUID;
476 ovi.dwOSVersionInfoSize = sizeof(ovi);
477 GetVersionExA(&ovi);
478 if (ovi.dwPlatformId == VER_PLATFORM_WIN32_NT)
479 result |= WF_WIN32WOW; /* undocumented WF_WINNT */
480 return result;
484 /***********************************************************************
485 * GetWinDebugInfo (KERNEL.355)
487 BOOL16 WINAPI GetWinDebugInfo16(WINDEBUGINFO *lpwdi, UINT16 flags)
489 FIXME("(%8lx,%d): stub returning 0\n",
490 (unsigned long)lpwdi, flags);
491 /* 0 means not in debugging mode/version */
492 /* Can this type of debugging be used in wine ? */
493 /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
494 return 0;
498 /***********************************************************************
499 * SetWinDebugInfo (KERNEL.356)
501 BOOL16 WINAPI SetWinDebugInfo16(WINDEBUGINFO *lpwdi)
503 FIXME("(%8lx): stub returning 0\n", (unsigned long)lpwdi);
504 /* 0 means not in debugging mode/version */
505 /* Can this type of debugging be used in wine ? */
506 /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
507 return 0;
511 /***********************************************************************
512 * DebugFillBuffer (KERNEL.329)
514 * TODO:
515 * Should fill lpBuffer only if DBO_BUFFERFILL has been set by SetWinDebugInfo()
517 void WINAPI DebugFillBuffer(LPSTR lpBuffer, WORD wBytes)
519 memset(lpBuffer, DBGFILL_BUFFER, wBytes);
522 /***********************************************************************
523 * DiagQuery (KERNEL.339)
525 * returns TRUE if Win called with "/b" (bootlog.txt)
527 BOOL16 WINAPI DiagQuery16()
529 /* perhaps implement a Wine "/b" command line flag sometime ? */
530 return FALSE;
533 /***********************************************************************
534 * DiagOutput (KERNEL.340)
536 * writes a debug string into <windir>\bootlog.txt
538 void WINAPI DiagOutput16(LPCSTR str)
540 /* FIXME */
541 DPRINTF("DIAGOUTPUT:%s\n", debugstr_a(str));