2 * Implementation of VERSION.DLL - Version Info access
4 * Copyright 1996,1997 Marcus Meissner
5 * Copyright 1997 David Cuthbert
6 * Copyright 1999 Ulrich Weigand
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * o Verify VerQueryValue()
34 #include "wine/winuser16.h"
35 #include "wine/unicode.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ver);
42 /******************************************************************************
44 * This function will print via dprintf[_]ver to stddeb debug info regarding
45 * the file info structure vffi.
46 * 15-Feb-1998 Dimitrie Paun (dimi@cs.toronto.edu)
47 * Added this function to clean up the code.
49 *****************************************************************************/
50 static void print_vffi_debug(VS_FIXEDFILEINFO *vffi)
52 TRACE(" structversion=%u.%u, fileversion=%u.%u.%u.%u, productversion=%u.%u.%u.%u, flagmask=0x%lx, flags=%s%s%s%s%s%s\n",
53 HIWORD(vffi->dwStrucVersion),LOWORD(vffi->dwStrucVersion),
54 HIWORD(vffi->dwFileVersionMS),LOWORD(vffi->dwFileVersionMS),
55 HIWORD(vffi->dwFileVersionLS),LOWORD(vffi->dwFileVersionLS),
56 HIWORD(vffi->dwProductVersionMS),LOWORD(vffi->dwProductVersionMS),
57 HIWORD(vffi->dwProductVersionLS),LOWORD(vffi->dwProductVersionLS),
58 vffi->dwFileFlagsMask,
59 (vffi->dwFileFlags & VS_FF_DEBUG) ? "DEBUG," : "",
60 (vffi->dwFileFlags & VS_FF_PRERELEASE) ? "PRERELEASE," : "",
61 (vffi->dwFileFlags & VS_FF_PATCHED) ? "PATCHED," : "",
62 (vffi->dwFileFlags & VS_FF_PRIVATEBUILD) ? "PRIVATEBUILD," : "",
63 (vffi->dwFileFlags & VS_FF_INFOINFERRED) ? "INFOINFERRED," : "",
64 (vffi->dwFileFlags & VS_FF_SPECIALBUILD) ? "SPECIALBUILD," : ""
68 TRACE(" OS=0x%x.0x%x ",
69 HIWORD(vffi->dwFileOS),
70 LOWORD(vffi->dwFileOS)
72 switch (vffi->dwFileOS&0xFFFF0000) {
73 case VOS_DOS:TRACE("DOS,");break;
74 case VOS_OS216:TRACE("OS/2-16,");break;
75 case VOS_OS232:TRACE("OS/2-32,");break;
76 case VOS_NT:TRACE("NT,");break;
79 TRACE("UNKNOWN(0x%lx),",vffi->dwFileOS&0xFFFF0000);break;
81 switch (LOWORD(vffi->dwFileOS)) {
82 case VOS__BASE:TRACE("BASE");break;
83 case VOS__WINDOWS16:TRACE("WIN16");break;
84 case VOS__WINDOWS32:TRACE("WIN32");break;
85 case VOS__PM16:TRACE("PM16");break;
86 case VOS__PM32:TRACE("PM32");break;
87 default:TRACE("UNKNOWN(0x%x)",LOWORD(vffi->dwFileOS));break;
91 switch (vffi->dwFileType) {
94 TRACE("filetype=Unknown(0x%lx)",vffi->dwFileType);
96 case VFT_APP:TRACE("filetype=APP,");break;
97 case VFT_DLL:TRACE("filetype=DLL,");break;
99 TRACE("filetype=DRV,");
100 switch(vffi->dwFileSubtype) {
103 TRACE("UNKNOWN(0x%lx)",vffi->dwFileSubtype);
105 case VFT2_DRV_PRINTER:
108 case VFT2_DRV_KEYBOARD:
111 case VFT2_DRV_LANGUAGE:
114 case VFT2_DRV_DISPLAY:
120 case VFT2_DRV_NETWORK:
123 case VFT2_DRV_SYSTEM:
126 case VFT2_DRV_INSTALLABLE:
127 TRACE("INSTALLABLE");
135 case VFT2_DRV_INPUTMETHOD:
136 TRACE("INPUTMETHOD");
141 TRACE("filetype=FONT.");
142 switch (vffi->dwFileSubtype) {
144 TRACE("UNKNOWN(0x%lx)",vffi->dwFileSubtype);
146 case VFT2_FONT_RASTER:TRACE("RASTER");break;
147 case VFT2_FONT_VECTOR:TRACE("VECTOR");break;
148 case VFT2_FONT_TRUETYPE:TRACE("TRUETYPE");break;
151 case VFT_VXD:TRACE("filetype=VXD");break;
152 case VFT_STATIC_LIB:TRACE("filetype=STATIC_LIB");break;
155 TRACE(" filedata=0x%lx.0x%lx\n",
156 vffi->dwFileDateMS,vffi->dwFileDateLS);
160 /***********************************************************************
161 * Version Info Structure
169 #if 0 /* variable length structure */
173 VS_VERSION_INFO_STRUCT16 Children[];
175 } VS_VERSION_INFO_STRUCT16;
183 #if 0 /* variable length structure */
187 VS_VERSION_INFO_STRUCT32 Children[];
189 } VS_VERSION_INFO_STRUCT32;
191 #define VersionInfoIs16( ver ) \
192 ( ((VS_VERSION_INFO_STRUCT16 *)ver)->szKey[0] >= ' ' )
194 #define DWORD_ALIGN( base, ptr ) \
195 ( (LPBYTE)(base) + ((((LPBYTE)(ptr) - (LPBYTE)(base)) + 3) & ~3) )
197 #define VersionInfo16_Value( ver ) \
198 DWORD_ALIGN( (ver), (ver)->szKey + strlen((ver)->szKey) + 1 )
199 #define VersionInfo32_Value( ver ) \
200 DWORD_ALIGN( (ver), (ver)->szKey + strlenW((ver)->szKey) + 1 )
202 #define VersionInfo16_Children( ver ) \
203 (VS_VERSION_INFO_STRUCT16 *)( VersionInfo16_Value( ver ) + \
204 ( ( (ver)->wValueLength + 3 ) & ~3 ) )
205 #define VersionInfo32_Children( ver ) \
206 (VS_VERSION_INFO_STRUCT32 *)( VersionInfo32_Value( ver ) + \
207 ( ( (ver)->wValueLength * \
208 ((ver)->bText? 2 : 1) + 3 ) & ~3 ) )
210 #define VersionInfo16_Next( ver ) \
211 (VS_VERSION_INFO_STRUCT16 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
212 #define VersionInfo32_Next( ver ) \
213 (VS_VERSION_INFO_STRUCT32 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
215 /***********************************************************************
216 * ConvertVersionInfo32To16 [internal]
218 void ConvertVersionInfo32To16( VS_VERSION_INFO_STRUCT32 *info32,
219 VS_VERSION_INFO_STRUCT16 *info16 )
221 /* Copy data onto local stack to prevent overwrites */
222 WORD wLength = info32->wLength;
223 WORD wValueLength = info32->wValueLength;
224 WORD bText = info32->bText;
225 LPBYTE lpValue = VersionInfo32_Value( info32 );
226 VS_VERSION_INFO_STRUCT32 *child32 = VersionInfo32_Children( info32 );
227 VS_VERSION_INFO_STRUCT16 *child16;
229 TRACE("Converting %p to %p\n", info32, info16 );
230 TRACE("wLength %d, wValueLength %d, bText %d, value %p, child %p\n",
231 wLength, wValueLength, bText, lpValue, child32 );
234 WideCharToMultiByte( CP_ACP, 0, info32->szKey, -1, info16->szKey, 0x7fffffff, NULL, NULL );
236 TRACE("Copied key from %p to %p: %s\n", info32->szKey, info16->szKey,
237 debugstr_a(info16->szKey) );
240 if ( wValueLength == 0 )
242 info16->wValueLength = 0;
243 TRACE("No value present\n" );
247 info16->wValueLength = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)lpValue, -1, NULL, 0, NULL, NULL );
248 WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)lpValue, -1,
249 VersionInfo16_Value( info16 ), info16->wValueLength, NULL, NULL );
251 TRACE("Copied value from %p to %p: %s\n", lpValue,
252 VersionInfo16_Value( info16 ),
253 debugstr_a(VersionInfo16_Value( info16 )) );
257 info16->wValueLength = wValueLength;
258 memmove( VersionInfo16_Value( info16 ), lpValue, wValueLength );
260 TRACE("Copied value from %p to %p: %d bytes\n", lpValue,
261 VersionInfo16_Value( info16 ), wValueLength );
264 /* Convert children */
265 child16 = VersionInfo16_Children( info16 );
266 while ( (DWORD)child32 < (DWORD)info32 + wLength && child32->wLength != 0 )
268 VS_VERSION_INFO_STRUCT32 *nextChild = VersionInfo32_Next( child32 );
270 ConvertVersionInfo32To16( child32, child16 );
272 child16 = VersionInfo16_Next( child16 );
277 info16->wLength = (DWORD)child16 - (DWORD)info16;
279 TRACE("Finished, length is %d (%p - %p)\n",
280 info16->wLength, info16, child16 );
283 /***********************************************************************
284 * VERSION_GetFileVersionInfo_PE [internal]
286 * NOTE: returns size of the PE VERSION resource or 0xFFFFFFFF
287 * in the case if file exists, but VERSION_INFO not found.
288 * FIXME: handle is not used.
290 static DWORD VERSION_GetFileVersionInfo_PE( LPCSTR filename, LPDWORD handle,
291 DWORD datasize, LPVOID data )
293 VS_FIXEDFILEINFO *vffi;
300 TRACE("(%s,%p)\n", debugstr_a(filename), handle );
302 hModule = GetModuleHandleA(filename);
304 hModule = LoadLibraryExA(filename, 0, LOAD_LIBRARY_AS_DATAFILE);
306 hModule = LoadLibraryExA(filename, 0, 0);
309 WARN("Could not load %s\n", debugstr_a(filename));
312 hRsrc = FindResourceW(hModule,
313 MAKEINTRESOURCEW(VS_VERSION_INFO),
314 MAKEINTRESOURCEW(VS_FILE_INFO));
317 WARN("Could not find VS_VERSION_INFO in %s\n", debugstr_a(filename));
318 FreeLibrary(hModule);
321 len = SizeofResource(hModule, hRsrc);
322 hMem = LoadResource(hModule, hRsrc);
325 WARN("Could not load VS_VERSION_INFO from %s\n", debugstr_a(filename));
326 FreeLibrary(hModule);
329 buf = LockResource(hMem);
331 vffi = (VS_FIXEDFILEINFO *)VersionInfo32_Value( (VS_VERSION_INFO_STRUCT32 *)buf );
333 if ( vffi->dwSignature != VS_FFI_SIGNATURE )
335 WARN("vffi->dwSignature is 0x%08lx, but not 0x%08lx!\n",
336 vffi->dwSignature, VS_FFI_SIGNATURE );
342 print_vffi_debug( vffi );
347 len = datasize; /* truncate data */
349 memcpy(data, buf, len);
355 FreeLibrary(hModule);
360 /***********************************************************************
361 * VERSION_GetFileVersionInfo_16 [internal]
363 * NOTE: returns size of the 16-bit VERSION resource or 0xFFFFFFFF
364 * in the case if file exists, but VERSION_INFO not found.
365 * FIXME: handle is not used.
367 static DWORD VERSION_GetFileVersionInfo_16( LPCSTR filename, LPDWORD handle,
368 DWORD datasize, LPVOID data )
370 VS_FIXEDFILEINFO *vffi;
377 TRACE("(%s,%p)\n", debugstr_a(filename), handle );
379 hModule = LoadLibrary16(filename);
382 WARN("Could not load %s\n", debugstr_a(filename));
385 hRsrc = FindResource16(hModule,
386 MAKEINTRESOURCEA(VS_VERSION_INFO),
387 MAKEINTRESOURCEA(VS_FILE_INFO));
390 WARN("Could not find VS_VERSION_INFO in %s\n", debugstr_a(filename));
391 FreeLibrary16(hModule);
394 len = SizeofResource16(hModule, hRsrc);
395 hMem = LoadResource16(hModule, hRsrc);
398 WARN("Could not load VS_VERSION_INFO from %s\n", debugstr_a(filename));
399 FreeLibrary16(hModule);
402 buf = LockResource16(hMem);
404 if(!VersionInfoIs16(buf))
407 vffi = (VS_FIXEDFILEINFO *)VersionInfo16_Value( (VS_VERSION_INFO_STRUCT16 *)buf );
409 if ( vffi->dwSignature != VS_FFI_SIGNATURE )
411 WARN("vffi->dwSignature is 0x%08lx, but not 0x%08lx!\n",
412 vffi->dwSignature, VS_FFI_SIGNATURE );
418 print_vffi_debug( vffi );
423 len = datasize; /* truncate data */
425 memcpy(data, buf, len);
430 FreeResource16(hMem);
431 FreeLibrary16(hModule);
436 /***********************************************************************
437 * GetFileVersionInfoSizeA [VERSION.@]
439 DWORD WINAPI GetFileVersionInfoSizeA( LPCSTR filename, LPDWORD handle )
441 VS_FIXEDFILEINFO *vffi;
442 DWORD len, ret, offset;
445 TRACE("(%s,%p)\n", debugstr_a(filename), handle );
447 len = VERSION_GetFileVersionInfo_PE(filename, handle, 0, NULL);
448 /* 0xFFFFFFFF means: file exists, but VERSION_INFO not found */
449 if(len == 0xFFFFFFFF)
451 SetLastError(ERROR_RESOURCE_DATA_NOT_FOUND);
455 len = VERSION_GetFileVersionInfo_16(filename, handle, 0, NULL);
456 /* 0xFFFFFFFF means: file exists, but VERSION_INFO not found */
457 if(len == 0xFFFFFFFF)
459 SetLastError(ERROR_RESOURCE_DATA_NOT_FOUND);
464 len = GetFileResourceSize16( filename,
465 MAKEINTRESOURCEA(VS_FILE_INFO),
466 MAKEINTRESOURCEA(VS_VERSION_INFO),
470 ret = GetFileResource16( filename,
471 MAKEINTRESOURCEA(VS_FILE_INFO),
472 MAKEINTRESOURCEA(VS_VERSION_INFO),
473 offset, sizeof( buf ), buf );
476 if ( handle ) *handle = offset;
478 if ( VersionInfoIs16( buf ) )
479 vffi = (VS_FIXEDFILEINFO *)VersionInfo16_Value( (VS_VERSION_INFO_STRUCT16 *)buf );
481 vffi = (VS_FIXEDFILEINFO *)VersionInfo32_Value( (VS_VERSION_INFO_STRUCT32 *)buf );
483 if ( vffi->dwSignature != VS_FFI_SIGNATURE )
485 WARN("vffi->dwSignature is 0x%08lx, but not 0x%08lx!\n",
486 vffi->dwSignature, VS_FFI_SIGNATURE );
490 if ( ((VS_VERSION_INFO_STRUCT16 *)buf)->wLength < len )
491 len = ((VS_VERSION_INFO_STRUCT16 *)buf)->wLength;
494 print_vffi_debug( vffi );
499 /***********************************************************************
500 * GetFileVersionInfoSizeW [VERSION.@]
502 DWORD WINAPI GetFileVersionInfoSizeW( LPCWSTR filename, LPDWORD handle )
504 DWORD ret, len = WideCharToMultiByte( CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL );
505 LPSTR fn = HeapAlloc( GetProcessHeap(), 0, len );
506 WideCharToMultiByte( CP_ACP, 0, filename, -1, fn, len, NULL, NULL );
507 ret = GetFileVersionInfoSizeA( fn, handle );
508 HeapFree( GetProcessHeap(), 0, fn );
512 /***********************************************************************
513 * GetFileVersionInfoA [VERSION.@]
515 BOOL WINAPI GetFileVersionInfoA( LPCSTR filename, DWORD handle,
516 DWORD datasize, LPVOID data )
520 TRACE("(%s,%ld,size=%ld,data=%p)\n",
521 debugstr_a(filename), handle, datasize, data );
523 len = VERSION_GetFileVersionInfo_PE(filename, &handle, datasize, data);
524 /* 0xFFFFFFFF means: file exists, but VERSION_INFO not found */
525 if(len == 0xFFFFFFFF) return FALSE;
528 len = VERSION_GetFileVersionInfo_16(filename, &handle, datasize, data);
529 /* 0xFFFFFFFF means: file exists, but VERSION_INFO not found */
530 if(len == 0xFFFFFFFF) return FALSE;
534 if ( !GetFileResource16( filename, MAKEINTRESOURCEA(VS_FILE_INFO),
535 MAKEINTRESOURCEA(VS_VERSION_INFO),
536 handle, datasize, data ) )
539 if ( datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
540 && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength
541 && !VersionInfoIs16( data ) )
543 /* convert resource from PE format to NE format */
544 ConvertVersionInfo32To16( (VS_VERSION_INFO_STRUCT32 *)data,
545 (VS_VERSION_INFO_STRUCT16 *)data );
551 /***********************************************************************
552 * GetFileVersionInfoW [VERSION.@]
554 BOOL WINAPI GetFileVersionInfoW( LPCWSTR filename, DWORD handle,
555 DWORD datasize, LPVOID data )
557 DWORD len = WideCharToMultiByte( CP_ACP, 0, filename, -1, NULL, 0, NULL, NULL );
558 LPSTR fn = HeapAlloc( GetProcessHeap(), 0, len );
561 WideCharToMultiByte( CP_ACP, 0, filename, -1, fn, len, NULL, NULL );
563 TRACE("(%s,%ld,size=%ld,data=%p)\n",
564 debugstr_w(filename), handle, datasize, data );
566 if(VERSION_GetFileVersionInfo_PE(fn, &handle, datasize, data))
568 if(VERSION_GetFileVersionInfo_16(fn, &handle, datasize, data))
571 if ( !GetFileResource16( fn, MAKEINTRESOURCEA(VS_FILE_INFO),
572 MAKEINTRESOURCEA(VS_VERSION_INFO),
573 handle, datasize, data ) )
576 else if ( datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
577 && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength
578 && VersionInfoIs16( data ) )
580 ERR("Cannot access NE resource in %s\n", debugstr_a(fn) );
584 HeapFree( GetProcessHeap(), 0, fn );
589 /***********************************************************************
590 * VersionInfo16_FindChild [internal]
592 static VS_VERSION_INFO_STRUCT16 *VersionInfo16_FindChild( VS_VERSION_INFO_STRUCT16 *info,
593 LPCSTR szKey, UINT cbKey )
595 VS_VERSION_INFO_STRUCT16 *child = VersionInfo16_Children( info );
597 while ( (DWORD)child < (DWORD)info + info->wLength )
599 if ( !strncasecmp( child->szKey, szKey, cbKey ) )
602 if (!(child->wLength)) return NULL;
603 child = VersionInfo16_Next( child );
609 /***********************************************************************
610 * VersionInfo32_FindChild [internal]
612 static VS_VERSION_INFO_STRUCT32 *VersionInfo32_FindChild( VS_VERSION_INFO_STRUCT32 *info,
613 LPCWSTR szKey, UINT cbKey )
615 VS_VERSION_INFO_STRUCT32 *child = VersionInfo32_Children( info );
617 while ( (DWORD)child < (DWORD)info + info->wLength )
619 if ( !strncmpiW( child->szKey, szKey, cbKey ) )
622 child = VersionInfo32_Next( child );
628 /***********************************************************************
629 * VerQueryValueA [VERSION.@]
631 DWORD WINAPI VerQueryValueA( LPVOID pBlock, LPCSTR lpSubBlock,
632 LPVOID *lplpBuffer, UINT *puLen )
634 VS_VERSION_INFO_STRUCT16 *info = (VS_VERSION_INFO_STRUCT16 *)pBlock;
635 if ( !VersionInfoIs16( info ) )
641 /* <lawson_whitney@juno.com> Feb 2001 */
642 /* AOL 5.0 does this, expecting to get this: */
643 len = MultiByteToWideChar(CP_ACP, 0, lpSubBlock, -1, NULL, 0);
644 wide_str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
645 MultiByteToWideChar(CP_ACP, 0, lpSubBlock, -1, wide_str, len);
647 give = VerQueryValueW(pBlock, wide_str, lplpBuffer, puLen);
648 HeapFree(GetProcessHeap(), 0, wide_str);
652 TRACE("(%p,%s,%p,%p)\n",
653 pBlock, debugstr_a(lpSubBlock), lplpBuffer, puLen );
655 while ( *lpSubBlock )
657 /* Find next path component */
659 for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
660 if ( *lpNextSlash == '\\' )
663 /* Skip empty components */
664 if ( lpNextSlash == lpSubBlock )
670 /* We have a non-empty component: search info for key */
671 info = VersionInfo16_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
672 if ( !info ) return FALSE;
674 /* Skip path component */
675 lpSubBlock = lpNextSlash;
679 *lplpBuffer = VersionInfo16_Value( info );
680 *puLen = info->wValueLength;
685 /***********************************************************************
686 * VerQueryValueW [VERSION.@]
688 DWORD WINAPI VerQueryValueW( LPVOID pBlock, LPCWSTR lpSubBlock,
689 LPVOID *lplpBuffer, UINT *puLen )
691 VS_VERSION_INFO_STRUCT32 *info = (VS_VERSION_INFO_STRUCT32 *)pBlock;
692 if ( VersionInfoIs16( info ) )
694 ERR("called on NE resource!\n" );
698 TRACE("(%p,%s,%p,%p)\n",
699 pBlock, debugstr_w(lpSubBlock), lplpBuffer, puLen );
701 while ( *lpSubBlock )
703 /* Find next path component */
705 for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
706 if ( *lpNextSlash == '\\' )
709 /* Skip empty components */
710 if ( lpNextSlash == lpSubBlock )
716 /* We have a non-empty component: search info for key */
717 info = VersionInfo32_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
718 if ( !info ) return FALSE;
720 /* Skip path component */
721 lpSubBlock = lpNextSlash;
725 *lplpBuffer = VersionInfo32_Value( info );
726 *puLen = info->wValueLength;