.spec.c files are no longer used for 32-bit dlls.
[wine] / dlls / kernel / version.c
1 /*
2  * Windows and DOS version functions
3  *
4  * Copyright 1997 Marcus Meissner
5  * Copyright 1998 Patrik Stridvall
6  * Copyright 1998, 2003 Andreas Mohr
7  * Copyright 1997, 2003 Alexandre Julliard
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <string.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include "ntstatus.h"
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winreg.h"
37 #include "winternl.h"
38 #include "winerror.h"
39 #include "wine/winbase16.h"
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(ver);
44
45 /**********************************************************************
46  *         parse_dos_version
47  *
48  * Parse the contents of the Version key.
49  */
50 static WORD parse_dos_version( HKEY hkey )
51 {
52     static const WCHAR DosW[] = {'D','O','S',0};
53
54     UNICODE_STRING valueW;
55     int hi, lo;
56     char tmp[64], buffer[50];
57     KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)tmp;
58     DWORD count, len;
59     WORD ret = 0;
60
61     RtlInitUnicodeString( &valueW, DosW );
62     if (!NtQueryValueKey( hkey, &valueW, KeyValuePartialInformation, tmp, sizeof(tmp), &count ))
63     {
64         RtlUnicodeToMultiByteN( buffer, sizeof(buffer)-1, &len,
65                                 (WCHAR *)info->Data, info->DataLength );
66         buffer[len] = 0;
67
68         if (sscanf( buffer, "%d.%d", &hi, &lo ) == 2) ret = MAKEWORD( lo, hi );
69         else MESSAGE("Wrong format for DOS version in config file. Use \"x.xx\"\n");
70     }
71     return ret;
72 }
73
74
75 /**********************************************************************
76  *         get_dos_version
77  */
78 WORD get_dos_version(void)
79 {
80     OBJECT_ATTRIBUTES attr;
81     UNICODE_STRING nameW;
82     HKEY hkey, config_key;
83     WCHAR buffer[MAX_PATH];
84     WORD ret = 0;
85
86     static const WCHAR configW[] = {'M','a','c','h','i','n','e','\\',
87                                     'S','o','f','t','w','a','r','e','\\',
88                                     'W','i','n','e','\\',
89                                     'W','i','n','e','\\',
90                                     'C','o','n','f','i','g',0};
91     static const WCHAR appdefaultsW[] = {'A','p','p','D','e','f','a','u','l','t','s','\\',0};
92     static const WCHAR versionW[] = {'\\','V','e','r','s','i','o','n',0};
93
94     attr.Length = sizeof(attr);
95     attr.RootDirectory = 0;
96     attr.ObjectName = &nameW;
97     attr.Attributes = 0;
98     attr.SecurityDescriptor = NULL;
99     attr.SecurityQualityOfService = NULL;
100     RtlInitUnicodeString( &nameW, configW );
101
102     if (NtOpenKey( &config_key, KEY_ALL_ACCESS, &attr )) return 0;
103     attr.RootDirectory = config_key;
104
105     /* open AppDefaults\\appname\\Version key */
106     if (GetModuleFileNameW( 0, buffer, sizeof(buffer)/sizeof(WCHAR) ))
107     {
108         WCHAR *p, *appname, appversion[MAX_PATH+20];
109
110         appname = buffer;
111         if ((p = strrchrW( appname, '/' ))) appname = p + 1;
112         if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
113
114         strcpyW( appversion, appdefaultsW );
115         strcatW( appversion, appname );
116         strcatW( appversion, versionW );
117
118         TRACE( "getting version from %s\n", debugstr_w(appversion) );
119         RtlInitUnicodeString( &nameW, appversion );
120
121         if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
122         {
123             ret = parse_dos_version( hkey );
124             NtClose( hkey );
125         }
126     }
127
128     if (!ret)
129     {
130         TRACE( "getting default version\n" );
131         RtlInitUnicodeString( &nameW, versionW + 1 );
132         if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
133         {
134             ret = parse_dos_version( hkey );
135             NtClose( hkey );
136         }
137     }
138
139     NtClose( config_key );
140     return ret;
141 }
142
143
144 /***********************************************************************
145  *         GetVersion   (KERNEL.3)
146  */
147 DWORD WINAPI GetVersion16(void)
148 {
149     static WORD dosver, winver;
150
151     if (!dosver)  /* not determined yet */
152     {
153         RTL_OSVERSIONINFOEXW info;
154
155         info.dwOSVersionInfoSize = sizeof(info);
156         if (RtlGetVersion( &info ) != STATUS_SUCCESS) return 0;
157
158         if (info.dwMajorVersion <= 3)
159             winver = MAKEWORD( info.dwMajorVersion, info.dwMinorVersion );
160         else
161             winver = MAKEWORD( 3, 95 );
162
163         switch(info.dwPlatformId)
164         {
165         case VER_PLATFORM_WIN32s:
166             if ((dosver = get_dos_version())) break;  /* got the configured version */
167
168             switch(MAKELONG( info.dwMinorVersion, info.dwMajorVersion ))
169             {
170             case 0x0200:
171                 dosver = 0x0303;  /* DOS 3.3 for Windows 2.0 */
172                 break;
173             case 0x0300:
174                 dosver = 0x0500;  /* DOS 5.0 for Windows 3.0 */
175                 break;
176             default:
177                 dosver = 0x0616;  /* DOS 6.22 for Windows 3.1 and later */
178                 break;
179             }
180             break;
181         case VER_PLATFORM_WIN32_WINDOWS:
182             /* DOS 8.0 for WinME, 7.0 for Win95/98 */
183             if (info.dwMinorVersion >= 90) dosver = 0x0800;
184             else dosver = 0x0700;
185             break;
186         case VER_PLATFORM_WIN32_NT:
187             dosver = 0x0500;  /* always DOS 5.0 for NT */
188             break;
189         }
190         TRACE( "DOS %d.%02d Win %d.%02d\n",
191                HIBYTE(dosver), LOBYTE(dosver), LOBYTE(winver), HIBYTE(winver) );
192     }
193     return MAKELONG( winver, dosver );
194 }
195
196
197 /***********************************************************************
198  *         GetVersion   (KERNEL32.@)
199  *
200  * Win31   0x80000a03
201  * Win95   0xc0000004
202  * Win98   0xc0000a04
203  * WinME   0xc0005a04
204  * NT351   0x04213303
205  * NT4     0x05650004
206  * Win2000 0x08930005
207  * WinXP   0x0a280105
208  */
209 DWORD WINAPI GetVersion(void)
210 {
211     RTL_OSVERSIONINFOEXW info;
212     DWORD result;
213
214     info.dwOSVersionInfoSize = sizeof(info);
215     if (RtlGetVersion( &info ) != STATUS_SUCCESS) return 0;
216
217     result = MAKELONG( MAKEWORD( info.dwMajorVersion, info.dwMinorVersion ),
218                        (info.dwPlatformId ^ 2) << 14 );
219     if (info.dwPlatformId == VER_PLATFORM_WIN32_NT) result |= LOWORD(info.dwBuildNumber) << 16;
220     return result;
221 }
222
223
224 /***********************************************************************
225  *         GetVersionEx   (KERNEL.149)
226  */
227 BOOL16 WINAPI GetVersionEx16(OSVERSIONINFO16 *v)
228 {
229     OSVERSIONINFOA info;
230
231     if (v->dwOSVersionInfoSize < sizeof(OSVERSIONINFO16))
232     {
233         WARN("wrong OSVERSIONINFO size from app\n");
234         return FALSE;
235     }
236
237     info.dwOSVersionInfoSize = sizeof(info);
238     if (!GetVersionExA( &info )) return FALSE;
239
240     v->dwMajorVersion = info.dwMajorVersion;
241     v->dwMinorVersion = info.dwMinorVersion;
242     v->dwBuildNumber  = info.dwBuildNumber;
243     v->dwPlatformId   = info.dwPlatformId;
244     strcpy( v->szCSDVersion, info.szCSDVersion );
245     return TRUE;
246 }
247
248
249 /***********************************************************************
250  *         GetVersionExA   (KERNEL32.@)
251  */
252 BOOL WINAPI GetVersionExA(OSVERSIONINFOA *v)
253 {
254     RTL_OSVERSIONINFOEXW infoW;
255
256     if (v->dwOSVersionInfoSize != sizeof(OSVERSIONINFOA) &&
257         v->dwOSVersionInfoSize != sizeof(OSVERSIONINFOEXA))
258     {
259         WARN("wrong OSVERSIONINFO size from app (got: %ld, expected: %d or %d)\n",
260                         v->dwOSVersionInfoSize, sizeof(OSVERSIONINFOA),
261                         sizeof(OSVERSIONINFOEXA));
262         return FALSE;
263     }
264
265     infoW.dwOSVersionInfoSize = sizeof(infoW);
266     if (RtlGetVersion( &infoW ) != STATUS_SUCCESS) return FALSE;
267
268     v->dwMajorVersion = infoW.dwMajorVersion;
269     v->dwMinorVersion = infoW.dwMinorVersion;
270     v->dwBuildNumber  = infoW.dwBuildNumber;
271     v->dwPlatformId   = infoW.dwPlatformId;
272     WideCharToMultiByte( CP_ACP, 0, infoW.szCSDVersion, -1,
273                          v->szCSDVersion, sizeof(v->szCSDVersion), NULL, NULL );
274
275     if(v->dwOSVersionInfoSize == sizeof(OSVERSIONINFOEXA))
276     {
277         LPOSVERSIONINFOEXA vex = (LPOSVERSIONINFOEXA) v;
278         vex->wServicePackMajor = infoW.wServicePackMajor;
279         vex->wServicePackMinor = infoW.wServicePackMinor;
280         vex->wSuiteMask        = infoW.wSuiteMask;
281         vex->wProductType      = infoW.wProductType;
282     }
283     return TRUE;
284 }
285
286
287 /***********************************************************************
288  *         GetVersionExW   (KERNEL32.@)
289  */
290 BOOL WINAPI GetVersionExW( OSVERSIONINFOW *info )
291 {
292     if (info->dwOSVersionInfoSize != sizeof(OSVERSIONINFOW) &&
293         info->dwOSVersionInfoSize != sizeof(OSVERSIONINFOEXW))
294     {
295         WARN("wrong OSVERSIONINFO size from app (got: %ld, expected: %d or %d)\n",
296                         info->dwOSVersionInfoSize, sizeof(OSVERSIONINFOW),
297                         sizeof(OSVERSIONINFOEXW));
298         return FALSE;
299     }
300     return (RtlGetVersion( (RTL_OSVERSIONINFOEXW *)info ) == STATUS_SUCCESS);
301 }
302
303
304 /******************************************************************************
305  *        VerifyVersionInfoA   (KERNEL32.@)
306  */
307 BOOL WINAPI VerifyVersionInfoA( LPOSVERSIONINFOEXA lpVersionInfo, DWORD dwTypeMask,
308                                 DWORDLONG dwlConditionMask)
309 {
310     OSVERSIONINFOEXW verW;
311
312     verW.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
313     verW.dwMajorVersion = lpVersionInfo->dwMajorVersion;
314     verW.dwMinorVersion = lpVersionInfo->dwMinorVersion;
315     verW.dwBuildNumber = lpVersionInfo->dwBuildNumber;
316     verW.dwPlatformId = lpVersionInfo->dwPlatformId;
317     verW.wServicePackMajor = lpVersionInfo->wServicePackMajor;
318     verW.wServicePackMinor = lpVersionInfo->wServicePackMinor;
319     verW.wSuiteMask = lpVersionInfo->wSuiteMask;
320     verW.wProductType = lpVersionInfo->wProductType;
321     verW.wReserved = lpVersionInfo->wReserved;
322
323     return VerifyVersionInfoW(&verW, dwTypeMask, dwlConditionMask);
324 }
325
326
327 /******************************************************************************
328  *        VerifyVersionInfoW   (KERNEL32.@)
329  */
330 BOOL WINAPI VerifyVersionInfoW( LPOSVERSIONINFOEXW lpVersionInfo, DWORD dwTypeMask,
331                                 DWORDLONG dwlConditionMask)
332 {
333     switch(RtlVerifyVersionInfo( lpVersionInfo, dwTypeMask, dwlConditionMask ))
334     {
335     case STATUS_INVALID_PARAMETER:
336         SetLastError( ERROR_BAD_ARGUMENTS );
337         return FALSE;
338     case STATUS_REVISION_MISMATCH:
339         SetLastError( ERROR_OLD_WIN_VERSION );
340         return FALSE;
341     }
342     return TRUE;
343 }
344
345
346 /***********************************************************************
347  *          GetWinFlags   (KERNEL.132)
348  */
349 DWORD WINAPI GetWinFlags16(void)
350 {
351   static const long cpuflags[5] =
352     { WF_CPU086, WF_CPU186, WF_CPU286, WF_CPU386, WF_CPU486 };
353   SYSTEM_INFO si;
354   OSVERSIONINFOA ovi;
355   DWORD result;
356
357   GetSystemInfo(&si);
358
359   /* There doesn't seem to be any Pentium flag.  */
360   result = cpuflags[min(si.wProcessorLevel, 4)] | WF_ENHANCED | WF_PMODE | WF_80x87 | WF_PAGING;
361   if (si.wProcessorLevel >= 4) result |= WF_HASCPUID;
362   ovi.dwOSVersionInfoSize = sizeof(ovi);
363   GetVersionExA(&ovi);
364   if (ovi.dwPlatformId == VER_PLATFORM_WIN32_NT)
365       result |= WF_WIN32WOW; /* undocumented WF_WINNT */
366   return result;
367 }
368
369
370 #if 0
371 /* Not used at this time. This is here for documentation only */
372
373 /* WINDEBUGINFO flags values */
374 #define WDI_OPTIONS         0x0001
375 #define WDI_FILTER          0x0002
376 #define WDI_ALLOCBREAK      0x0004
377
378 /* dwOptions values */
379 #define DBO_CHECKHEAP       0x0001
380 #define DBO_BUFFERFILL      0x0004
381 #define DBO_DISABLEGPTRAPPING 0x0010
382 #define DBO_CHECKFREE       0x0020
383
384 #define DBO_SILENT          0x8000
385
386 #define DBO_TRACEBREAK      0x2000
387 #define DBO_WARNINGBREAK    0x1000
388 #define DBO_NOERRORBREAK    0x0800
389 #define DBO_NOFATALBREAK    0x0400
390 #define DBO_INT3BREAK       0x0100
391
392 /* DebugOutput flags values */
393 #define DBF_TRACE           0x0000
394 #define DBF_WARNING         0x4000
395 #define DBF_ERROR           0x8000
396 #define DBF_FATAL           0xc000
397
398 /* dwFilter values */
399 #define DBF_KERNEL          0x1000
400 #define DBF_KRN_MEMMAN      0x0001
401 #define DBF_KRN_LOADMODULE  0x0002
402 #define DBF_KRN_SEGMENTLOAD 0x0004
403 #define DBF_USER            0x0800
404 #define DBF_GDI             0x0400
405 #define DBF_MMSYSTEM        0x0040
406 #define DBF_PENWIN          0x0020
407 #define DBF_APPLICATION     0x0008
408 #define DBF_DRIVER          0x0010
409
410 #endif /* NOLOGERROR */
411
412
413 /***********************************************************************
414  *          GetWinDebugInfo   (KERNEL.355)
415  */
416 BOOL16 WINAPI GetWinDebugInfo16(WINDEBUGINFO16 *lpwdi, UINT16 flags)
417 {
418     FIXME("(%8lx,%d): stub returning 0\n",
419           (unsigned long)lpwdi, flags);
420     /* 0 means not in debugging mode/version */
421     /* Can this type of debugging be used in wine ? */
422     /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
423     return 0;
424 }
425
426
427 /***********************************************************************
428  *          SetWinDebugInfo   (KERNEL.356)
429  */
430 BOOL16 WINAPI SetWinDebugInfo16(WINDEBUGINFO16 *lpwdi)
431 {
432     FIXME("(%8lx): stub returning 0\n", (unsigned long)lpwdi);
433     /* 0 means not in debugging mode/version */
434     /* Can this type of debugging be used in wine ? */
435     /* Constants: WDI_OPTIONS WDI_FILTER WDI_ALLOCBREAK */
436     return 0;
437 }
438
439
440 /***********************************************************************
441  *           K329                    (KERNEL.329)
442  *
443  * TODO:
444  * Should fill lpBuffer only if DBO_BUFFERFILL has been set by SetWinDebugInfo()
445  */
446 void WINAPI DebugFillBuffer(LPSTR lpBuffer, WORD wBytes)
447 {
448         memset(lpBuffer, DBGFILL_BUFFER, wBytes);
449 }
450
451 /***********************************************************************
452  *           DiagQuery                          (KERNEL.339)
453  *
454  * returns TRUE if Win called with "/b" (bootlog.txt)
455  */
456 BOOL16 WINAPI DiagQuery16(void)
457 {
458         /* perhaps implement a Wine "/b" command line flag sometime ? */
459         return FALSE;
460 }
461
462 /***********************************************************************
463  *           DiagOutput                         (KERNEL.340)
464  *
465  * writes a debug string into <windir>\bootlog.txt
466  */
467 void WINAPI DiagOutput16(LPCSTR str)
468 {
469         /* FIXME */
470         DPRINTF("DIAGOUTPUT:%s\n", debugstr_a(str));
471 }