Fix CreateProcessA to implement correct actions on ambiguous command
[wine/multimedia.git] / dlls / version / info.c
blobdcc295b3634a2cb9b4083380312a5b4d7b6f341b
1 /*
2 * Implementation of VERSION.DLL - Version Info access
3 *
4 * Copyright 1996,1997 Marcus Meissner
5 * Copyright 1997 David Cuthbert
6 * Copyright 1999 Ulrich Weigand
7 */
9 #include <stdlib.h>
10 #include <string.h>
12 #include "winreg.h"
13 #include "winver.h"
14 #include "wine/winestring.h"
15 #include "winerror.h"
16 #include "heap.h"
17 #include "ver.h"
18 #include "debug.h"
20 DEFAULT_DEBUG_CHANNEL(ver)
23 /******************************************************************************
25 * This function will print via dprintf[_]ver to stddeb debug info regarding
26 * the file info structure vffi.
27 * 15-Feb-1998 Dimitrie Paun (dimi@cs.toronto.edu)
28 * Added this function to clean up the code.
30 *****************************************************************************/
31 static void print_vffi_debug(VS_FIXEDFILEINFO *vffi)
33 dbg_decl_str(ver, 1024);
35 TRACE(ver," structversion=%u.%u, fileversion=%u.%u.%u.%u, productversion=%u.%u.%u.%u, flagmask=0x%lx, flags=%s%s%s%s%s%s\n",
36 HIWORD(vffi->dwStrucVersion),LOWORD(vffi->dwStrucVersion),
37 HIWORD(vffi->dwFileVersionMS),LOWORD(vffi->dwFileVersionMS),
38 HIWORD(vffi->dwFileVersionLS),LOWORD(vffi->dwFileVersionLS),
39 HIWORD(vffi->dwProductVersionMS),LOWORD(vffi->dwProductVersionMS),
40 HIWORD(vffi->dwProductVersionLS),LOWORD(vffi->dwProductVersionLS),
41 vffi->dwFileFlagsMask,
42 (vffi->dwFileFlags & VS_FF_DEBUG) ? "DEBUG," : "",
43 (vffi->dwFileFlags & VS_FF_PRERELEASE) ? "PRERELEASE," : "",
44 (vffi->dwFileFlags & VS_FF_PATCHED) ? "PATCHED," : "",
45 (vffi->dwFileFlags & VS_FF_PRIVATEBUILD) ? "PRIVATEBUILD," : "",
46 (vffi->dwFileFlags & VS_FF_INFOINFERRED) ? "INFOINFERRED," : "",
47 (vffi->dwFileFlags & VS_FF_SPECIALBUILD) ? "SPECIALBUILD," : ""
50 dsprintf(ver," OS=0x%x.0x%x ",
51 HIWORD(vffi->dwFileOS),
52 LOWORD(vffi->dwFileOS)
54 switch (vffi->dwFileOS&0xFFFF0000) {
55 case VOS_DOS:dsprintf(ver,"DOS,");break;
56 case VOS_OS216:dsprintf(ver,"OS/2-16,");break;
57 case VOS_OS232:dsprintf(ver,"OS/2-32,");break;
58 case VOS_NT:dsprintf(ver,"NT,");break;
59 case VOS_UNKNOWN:
60 default:
61 dsprintf(ver,"UNKNOWN(0x%lx),",vffi->dwFileOS&0xFFFF0000);break;
63 switch (LOWORD(vffi->dwFileOS)) {
64 case VOS__BASE:dsprintf(ver,"BASE");break;
65 case VOS__WINDOWS16:dsprintf(ver,"WIN16");break;
66 case VOS__WINDOWS32:dsprintf(ver,"WIN32");break;
67 case VOS__PM16:dsprintf(ver,"PM16");break;
68 case VOS__PM32:dsprintf(ver,"PM32");break;
69 default:dsprintf(ver,"UNKNOWN(0x%x)",LOWORD(vffi->dwFileOS));break;
71 TRACE(ver, "(%s)\n", dbg_str(ver));
73 dbg_reset_str(ver);
74 switch (vffi->dwFileType) {
75 default:
76 case VFT_UNKNOWN:
77 dsprintf(ver,"filetype=Unknown(0x%lx)",vffi->dwFileType);
78 break;
79 case VFT_APP:dsprintf(ver,"filetype=APP,");break;
80 case VFT_DLL:dsprintf(ver,"filetype=DLL,");break;
81 case VFT_DRV:
82 dsprintf(ver,"filetype=DRV,");
83 switch(vffi->dwFileSubtype) {
84 default:
85 case VFT2_UNKNOWN:
86 dsprintf(ver,"UNKNOWN(0x%lx)",vffi->dwFileSubtype);
87 break;
88 case VFT2_DRV_PRINTER:
89 dsprintf(ver,"PRINTER");
90 break;
91 case VFT2_DRV_KEYBOARD:
92 dsprintf(ver,"KEYBOARD");
93 break;
94 case VFT2_DRV_LANGUAGE:
95 dsprintf(ver,"LANGUAGE");
96 break;
97 case VFT2_DRV_DISPLAY:
98 dsprintf(ver,"DISPLAY");
99 break;
100 case VFT2_DRV_MOUSE:
101 dsprintf(ver,"MOUSE");
102 break;
103 case VFT2_DRV_NETWORK:
104 dsprintf(ver,"NETWORK");
105 break;
106 case VFT2_DRV_SYSTEM:
107 dsprintf(ver,"SYSTEM");
108 break;
109 case VFT2_DRV_INSTALLABLE:
110 dsprintf(ver,"INSTALLABLE");
111 break;
112 case VFT2_DRV_SOUND:
113 dsprintf(ver,"SOUND");
114 break;
115 case VFT2_DRV_COMM:
116 dsprintf(ver,"COMM");
117 break;
118 case VFT2_DRV_INPUTMETHOD:
119 dsprintf(ver,"INPUTMETHOD");
120 break;
122 break;
123 case VFT_FONT:
124 dsprintf(ver,"filetype=FONT.");
125 switch (vffi->dwFileSubtype) {
126 default:
127 dsprintf(ver,"UNKNOWN(0x%lx)",vffi->dwFileSubtype);
128 break;
129 case VFT2_FONT_RASTER:dsprintf(ver,"RASTER");break;
130 case VFT2_FONT_VECTOR:dsprintf(ver,"VECTOR");break;
131 case VFT2_FONT_TRUETYPE:dsprintf(ver,"TRUETYPE");break;
133 break;
134 case VFT_VXD:dsprintf(ver,"filetype=VXD");break;
135 case VFT_STATIC_LIB:dsprintf(ver,"filetype=STATIC_LIB");break;
137 TRACE(ver, "%s\n", dbg_str(ver));
139 TRACE(ver, " filedata=0x%lx.0x%lx\n",
140 vffi->dwFileDateMS,vffi->dwFileDateLS);
144 /***********************************************************************
145 * Version Info Structure
148 typedef struct
150 WORD wLength;
151 WORD wValueLength;
152 CHAR szKey[1];
153 #if 0 /* variable length structure */
154 /* DWORD aligned */
155 BYTE Value[];
156 /* DWORD aligned */
157 VS_VERSION_INFO_STRUCT16 Children[];
158 #endif
159 } VS_VERSION_INFO_STRUCT16;
161 typedef struct
163 WORD wLength;
164 WORD wValueLength;
165 WORD bText;
166 WCHAR szKey[1];
167 #if 0 /* variable length structure */
168 /* DWORD aligned */
169 BYTE Value[];
170 /* DWORD aligned */
171 VS_VERSION_INFO_STRUCT32 Children[];
172 #endif
173 } VS_VERSION_INFO_STRUCT32;
175 #define VersionInfoIs16( ver ) \
176 ( ((VS_VERSION_INFO_STRUCT16 *)ver)->szKey[0] >= ' ' )
178 #define VersionInfo16_Value( ver ) \
179 (LPBYTE)( ((DWORD)((ver)->szKey) + (lstrlenA((ver)->szKey)+1) + 3) & ~3 )
180 #define VersionInfo32_Value( ver ) \
181 (LPBYTE)( ((DWORD)((ver)->szKey) + 2*(lstrlenW((ver)->szKey)+1) + 3) & ~3 )
183 #define VersionInfo16_Children( ver ) \
184 (VS_VERSION_INFO_STRUCT16 *)( VersionInfo16_Value( ver ) + \
185 ( ( (ver)->wValueLength + 3 ) & ~3 ) )
186 #define VersionInfo32_Children( ver ) \
187 (VS_VERSION_INFO_STRUCT32 *)( VersionInfo32_Value( ver ) + \
188 ( ( (ver)->wValueLength * \
189 ((ver)->bText? 2 : 1) + 3 ) & ~3 ) )
191 #define VersionInfo16_Next( ver ) \
192 (VS_VERSION_INFO_STRUCT16 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
193 #define VersionInfo32_Next( ver ) \
194 (VS_VERSION_INFO_STRUCT32 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
196 /***********************************************************************
197 * ConvertVersionInfo32To16 [internal]
199 void ConvertVersionInfo32To16( VS_VERSION_INFO_STRUCT32 *info32,
200 VS_VERSION_INFO_STRUCT16 *info16 )
202 /* Copy data onto local stack to prevent overwrites */
203 WORD wLength = info32->wLength;
204 WORD wValueLength = info32->wValueLength;
205 WORD bText = info32->bText;
206 LPBYTE lpValue = VersionInfo32_Value( info32 );
207 VS_VERSION_INFO_STRUCT32 *child32 = VersionInfo32_Children( info32 );
208 VS_VERSION_INFO_STRUCT16 *child16;
210 TRACE( ver, "Converting %p to %p\n", info32, info16 );
211 TRACE( ver, "wLength %d, wValueLength %d, bText %d, value %p, child %p\n",
212 wLength, wValueLength, bText, lpValue, child32 );
214 /* Convert key */
215 lstrcpyWtoA( info16->szKey, info32->szKey );
217 TRACE( ver, "Copied key from %p to %p: %s\n", info32->szKey, info16->szKey,
218 debugstr_a(info16->szKey) );
220 /* Convert value */
221 if ( wValueLength == 0 )
223 info16->wValueLength = 0;
224 TRACE( ver, "No value present\n" );
226 else if ( bText )
228 info16->wValueLength = lstrlenW( (LPCWSTR)lpValue ) + 1;
229 lstrcpyWtoA( VersionInfo16_Value( info16 ), (LPCWSTR)lpValue );
231 TRACE( ver, "Copied value from %p to %p: %s\n", lpValue,
232 VersionInfo16_Value( info16 ),
233 debugstr_a(VersionInfo16_Value( info16 )) );
235 else
237 info16->wValueLength = wValueLength;
238 memmove( VersionInfo16_Value( info16 ), lpValue, wValueLength );
240 TRACE( ver, "Copied value from %p to %p: %d bytes\n", lpValue,
241 VersionInfo16_Value( info16 ), wValueLength );
244 /* Convert children */
245 child16 = VersionInfo16_Children( info16 );
246 while ( (DWORD)child32 < (DWORD)info32 + wLength )
248 VS_VERSION_INFO_STRUCT32 *nextChild = VersionInfo32_Next( child32 );
250 ConvertVersionInfo32To16( child32, child16 );
252 child16 = VersionInfo16_Next( child16 );
253 child32 = nextChild;
256 /* Fixup length */
257 info16->wLength = (DWORD)child16 - (DWORD)info16;
259 TRACE( ver, "Finished, length is %d (%p - %p)\n",
260 info16->wLength, info16, child16 );
264 /***********************************************************************
265 * GetFileVersionInfoSize32A [VERSION.2]
267 DWORD WINAPI GetFileVersionInfoSizeA( LPCSTR filename, LPDWORD handle )
269 VS_FIXEDFILEINFO *vffi;
270 DWORD len, ret, offset;
271 BYTE buf[144];
273 TRACE( ver, "(%s,%p)\n", debugstr_a(filename), handle );
275 len = GetFileResourceSize( filename,
276 MAKEINTRESOURCEA(VS_FILE_INFO),
277 MAKEINTRESOURCEA(VS_VERSION_INFO),
278 &offset );
279 if (!len) return 0;
281 ret = GetFileResource( filename,
282 MAKEINTRESOURCEA(VS_FILE_INFO),
283 MAKEINTRESOURCEA(VS_VERSION_INFO),
284 offset, sizeof( buf ), buf );
285 if (!ret) return 0;
287 if ( handle ) *handle = offset;
289 if ( VersionInfoIs16( buf ) )
290 vffi = (VS_FIXEDFILEINFO *)VersionInfo16_Value( (VS_VERSION_INFO_STRUCT16 *)buf );
291 else
292 vffi = (VS_FIXEDFILEINFO *)VersionInfo32_Value( (VS_VERSION_INFO_STRUCT32 *)buf );
294 if ( vffi->dwSignature != VS_FFI_SIGNATURE )
296 WARN( ver, "vffi->dwSignature is 0x%08lx, but not 0x%08lx!\n",
297 vffi->dwSignature, VS_FFI_SIGNATURE );
298 return 0;
301 if ( ((VS_VERSION_INFO_STRUCT16 *)buf)->wLength < len )
302 len = ((VS_VERSION_INFO_STRUCT16 *)buf)->wLength;
304 if ( TRACE_ON( ver ) )
305 print_vffi_debug( vffi );
307 return len;
310 /***********************************************************************
311 * GetFileVersionInfoSize32W [VERSION.3]
313 DWORD WINAPI GetFileVersionInfoSizeW( LPCWSTR filename, LPDWORD handle )
315 LPSTR fn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
316 DWORD ret = GetFileVersionInfoSizeA( fn, handle );
317 HeapFree( GetProcessHeap(), 0, fn );
318 return ret;
321 /***********************************************************************
322 * GetFileVersionInfo32A [VERSION.1]
324 DWORD WINAPI GetFileVersionInfoA( LPCSTR filename, DWORD handle,
325 DWORD datasize, LPVOID data )
327 TRACE( ver, "(%s,%ld,size=%ld,data=%p)\n",
328 debugstr_a(filename), handle, datasize, data );
330 if ( !GetFileResource( filename, MAKEINTRESOURCEA(VS_FILE_INFO),
331 MAKEINTRESOURCEA(VS_VERSION_INFO),
332 handle, datasize, data ) )
333 return FALSE;
335 if ( datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
336 && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength
337 && !VersionInfoIs16( data ) )
339 /* convert resource from PE format to NE format */
340 ConvertVersionInfo32To16( (VS_VERSION_INFO_STRUCT32 *)data,
341 (VS_VERSION_INFO_STRUCT16 *)data );
344 return TRUE;
347 /***********************************************************************
348 * GetFileVersionInfo32W [VERSION.4]
350 DWORD WINAPI GetFileVersionInfoW( LPCWSTR filename, DWORD handle,
351 DWORD datasize, LPVOID data )
353 LPSTR fn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
354 DWORD retv = TRUE;
356 TRACE( ver, "(%s,%ld,size=%ld,data=%p)\n",
357 debugstr_a(fn), handle, datasize, data );
359 if ( !GetFileResource( fn, MAKEINTRESOURCEA(VS_FILE_INFO),
360 MAKEINTRESOURCEA(VS_VERSION_INFO),
361 handle, datasize, data ) )
362 retv = FALSE;
364 else if ( datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
365 && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength
366 && VersionInfoIs16( data ) )
368 ERR( ver, "Cannot access NE resource in %s\n", debugstr_a(fn) );
369 retv = FALSE;
372 HeapFree( GetProcessHeap(), 0, fn );
373 return retv;
377 /***********************************************************************
378 * VersionInfo16_FindChild [internal]
380 VS_VERSION_INFO_STRUCT16 *VersionInfo16_FindChild( VS_VERSION_INFO_STRUCT16 *info,
381 LPCSTR szKey, UINT cbKey )
383 VS_VERSION_INFO_STRUCT16 *child = VersionInfo16_Children( info );
385 while ( (DWORD)child < (DWORD)info + info->wLength )
387 if ( !lstrncmpiA( child->szKey, szKey, cbKey ) )
388 return child;
390 if (!(child->wLength)) return NULL;
391 child = VersionInfo16_Next( child );
394 return NULL;
397 /***********************************************************************
398 * VersionInfo32_FindChild [internal]
400 VS_VERSION_INFO_STRUCT32 *VersionInfo32_FindChild( VS_VERSION_INFO_STRUCT32 *info,
401 LPCWSTR szKey, UINT cbKey )
403 VS_VERSION_INFO_STRUCT32 *child = VersionInfo32_Children( info );
405 while ( (DWORD)child < (DWORD)info + info->wLength )
407 if ( !lstrncmpiW( child->szKey, szKey, cbKey ) )
408 return child;
410 child = VersionInfo32_Next( child );
413 return NULL;
416 /***********************************************************************
417 * VerQueryValue32A [VERSION.12]
419 DWORD WINAPI VerQueryValueA( LPVOID pBlock, LPCSTR lpSubBlock,
420 LPVOID *lplpBuffer, UINT *puLen )
422 VS_VERSION_INFO_STRUCT16 *info = (VS_VERSION_INFO_STRUCT16 *)pBlock;
423 if ( !VersionInfoIs16( info ) )
425 ERR( ver, "called on PE resource!\n" );
426 return FALSE;
429 TRACE( ver, "(%p,%s,%p,%p)\n",
430 pBlock, debugstr_a(lpSubBlock), lplpBuffer, puLen );
432 while ( *lpSubBlock )
434 /* Find next path component */
435 LPCSTR lpNextSlash;
436 for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
437 if ( *lpNextSlash == '\\' )
438 break;
440 /* Skip empty components */
441 if ( lpNextSlash == lpSubBlock )
443 lpSubBlock++;
444 continue;
447 /* We have a non-empty component: search info for key */
448 info = VersionInfo16_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
449 if ( !info ) return FALSE;
451 /* Skip path component */
452 lpSubBlock = lpNextSlash;
455 /* Return value */
456 *lplpBuffer = VersionInfo16_Value( info );
457 *puLen = info->wValueLength;
459 return TRUE;
462 /***********************************************************************
463 * VerQueryValue32W [VERSION.13]
465 DWORD WINAPI VerQueryValueW( LPVOID pBlock, LPCWSTR lpSubBlock,
466 LPVOID *lplpBuffer, UINT *puLen )
468 VS_VERSION_INFO_STRUCT32 *info = (VS_VERSION_INFO_STRUCT32 *)pBlock;
469 if ( VersionInfoIs16( info ) )
471 ERR( ver, "called on NE resource!\n" );
472 return FALSE;
475 TRACE( ver, "(%p,%s,%p,%p)\n",
476 pBlock, debugstr_w(lpSubBlock), lplpBuffer, puLen );
478 while ( *lpSubBlock )
480 /* Find next path component */
481 LPCWSTR lpNextSlash;
482 for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
483 if ( *lpNextSlash == '\\' )
484 break;
486 /* Skip empty components */
487 if ( lpNextSlash == lpSubBlock )
489 lpSubBlock++;
490 continue;
493 /* We have a non-empty component: search info for key */
494 info = VersionInfo32_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
495 if ( !info ) return FALSE;
497 /* Skip path component */
498 lpSubBlock = lpNextSlash;
501 /* Return value */
502 *lplpBuffer = VersionInfo32_Value( info );
503 *puLen = info->wValueLength;
505 return TRUE;
508 extern LPCSTR WINE_GetLanguageName( UINT langid );
510 /***********************************************************************
511 * VerLanguageName32A [VERSION.9]
513 DWORD WINAPI VerLanguageNameA( UINT wLang, LPSTR szLang, UINT nSize )
515 char buffer[80];
516 LPCSTR name;
517 DWORD result;
519 TRACE( ver, "(%d,%p,%d)\n", wLang, szLang, nSize );
522 * First, check \System\CurrentControlSet\control\Nls\Locale\<langid>
523 * from the registry.
526 sprintf( buffer,
527 "\\System\\CurrentControlSet\\control\\Nls\\Locale\\%08x",
528 wLang );
530 result = RegQueryValueA( HKEY_LOCAL_MACHINE, buffer, szLang, (LPDWORD)&nSize );
531 if ( result == ERROR_SUCCESS || result == ERROR_MORE_DATA )
532 return nSize;
535 * If that fails, use the internal table
536 * (actually, Windows stores the names in a string table resource ...)
539 name = WINE_GetLanguageName( wLang );
540 lstrcpynA( szLang, name, nSize );
541 return lstrlenA( name );
544 /***********************************************************************
545 * VerLanguageName32W [VERSION.10]
547 DWORD WINAPI VerLanguageNameW( UINT wLang, LPWSTR szLang, UINT nSize )
549 char buffer[80];
550 LPWSTR keyname;
551 LPCSTR name;
552 DWORD result;
554 TRACE( ver, "(%d,%p,%d)\n", wLang, szLang, nSize );
557 * First, check \System\CurrentControlSet\control\Nls\Locale\<langid>
558 * from the registry.
561 sprintf( buffer,
562 "\\System\\CurrentControlSet\\control\\Nls\\Locale\\%08x",
563 wLang );
565 keyname = HEAP_strdupAtoW( GetProcessHeap(), 0, buffer );
566 result = RegQueryValueW( HKEY_LOCAL_MACHINE, keyname, szLang, (LPDWORD)&nSize );
567 HeapFree( GetProcessHeap(), 0, keyname );
569 if ( result == ERROR_SUCCESS || result == ERROR_MORE_DATA )
570 return nSize;
573 * If that fails, use the internal table
574 * (actually, Windows stores the names in a string table resource ...)
577 name = WINE_GetLanguageName( wLang );
578 lstrcpynAtoW( szLang, name, nSize );
579 return lstrlenA( name );