| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | // |
| 5 | // GetProductVersionNumber.h |
| 6 | // |
| 7 | // Helper function to retrieve the file version number of a file. |
| 8 | // |
| 9 | // ====================================================================================== |
| 10 | |
| 11 | |
| 12 | |
| 13 | #ifndef __GetProductVersionNumber_h__ |
| 14 | #define __GetProductVersionNumber_h__ |
| 15 | |
| 16 | #include "contract.h" |
| 17 | #include "sstring.h" |
| 18 | #include "holder.h" |
| 19 | #include "ex.h" |
| 20 | |
| 21 | //--------------------------------------------------------------------------------------- |
| 22 | // |
| 23 | // Given the full path to an image, return the product version number. |
| 24 | // |
| 25 | // Arguments: |
| 26 | // szFullPath - full path to the image |
| 27 | // pdwVersionMS - out parameter; return the most significant 4 bytes of the version number according to |
| 28 | // the VS_FIXEDFILEINFO convention |
| 29 | // pdwVersionLS - out parameter; return the least significant 4 bytes of the version number according to |
| 30 | // the VS_FIXEDFILEINFO convention |
| 31 | // |
| 32 | // Notes: |
| 33 | // Throws on error |
| 34 | |
| 35 | void inline GetProductVersionNumber(SString &szFullPath, DWORD * pdwVersionMS, DWORD * pdwVersionLS) |
| 36 | { |
| 37 | WRAPPER_NO_CONTRACT; |
| 38 | #ifndef FEATURE_PAL |
| 39 | |
| 40 | DWORD dwDummy = 0; |
| 41 | DWORD dwFileInfoSize = 0; |
| 42 | |
| 43 | // Get the size of all of the file version information. |
| 44 | dwFileInfoSize = GetFileVersionInfoSize(szFullPath, &dwDummy); |
| 45 | if (dwFileInfoSize == 0) |
| 46 | { |
| 47 | ThrowLastError(); |
| 48 | } |
| 49 | |
| 50 | // Create the buffer to store the file information. |
| 51 | NewHolder<BYTE> pbFileInfo(new BYTE[dwFileInfoSize]); |
| 52 | |
| 53 | // Actually retrieve the file version information. |
| 54 | if (!GetFileVersionInfo(szFullPath, NULL, dwFileInfoSize, pbFileInfo)) |
| 55 | { |
| 56 | ThrowLastError(); |
| 57 | } |
| 58 | |
| 59 | // Now retrieve only the relevant version information, which will be returned in a VS_FIXEDFILEINFO. |
| 60 | UINT uVersionInfoSize = 0; |
| 61 | VS_FIXEDFILEINFO * pVersionInfo = NULL; |
| 62 | |
| 63 | if (!VerQueryValue(pbFileInfo, W("\\" ), reinterpret_cast<LPVOID *>(&pVersionInfo), &uVersionInfoSize)) |
| 64 | { |
| 65 | ThrowLastError(); |
| 66 | } |
| 67 | _ASSERTE(uVersionInfoSize == sizeof(VS_FIXEDFILEINFO)); |
| 68 | |
| 69 | *pdwVersionMS = pVersionInfo->dwProductVersionMS; |
| 70 | *pdwVersionLS = pVersionInfo->dwProductVersionLS; |
| 71 | #else |
| 72 | *pdwVersionMS = 0; |
| 73 | *pdwVersionLS = 0; |
| 74 | #endif // FEATURE_PAL |
| 75 | } |
| 76 | |
| 77 | #endif // __GetProductVersionNumber_h__ |
| 78 | |