鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 编程语言开发 > c++ > >

Get exe/dll file version from resource

来源:互联网 作者:佚名 时间:2016-06-06 10:05
无详细内容 无 #include Windows.h#pragma comment (lib, "version.lib")bool GetProductAndVersion(const CString file, CString strProductName, CString strProductVersion){ // allocate a block of memory for the version info DWORD dummy; DWORD dwSi
<无详细内容> <无>
#include <Windows.h>

#pragma comment (lib, "version.lib")

bool GetProductAndVersion(const CString & file, CString & strProductName, CString & strProductVersion)
{
    // allocate a block of memory for the version info
    DWORD dummy;
    DWORD dwSize = GetFileVersionInfoSize(file, &dummy);
    if (dwSize == 0)
    {
        TRACE("GetFileVersionInfoSize failed with error %d\n", GetLastError());
        return false;
    }
    std::vector<BYTE> data(dwSize);

    // load the version info
    if (!GetFileVersionInfo(file, NULL, dwSize, &data[0]))
    {
        TRACE("GetFileVersionInfo failed with error %d\n", GetLastError());
        return false;
    }

    // get the name and version strings
    LPVOID pvProductName = NULL;
    unsigned int iProductNameLen = 0;
    LPVOID pvProductVersion = NULL;
    unsigned int iProductVersionLen = 0;

    // replace "040904e4" with the language ID of your resources
    // http://stackoverflow.com/questions/316626/how-do-i-read-from-a-version-resource-in-visual-c
    // https://msdn.microsoft.com/en-us/library/windows/desktop/dd318693(v=vs.85).aspx 
    if (!VerQueryValue(&data[0], _T("\\StringFileInfo\\040904e4\\ProductName"), &pvProductName, &iProductNameLen) ||
        !VerQueryValue(&data[0], _T("\\StringFileInfo\\040904e4\\ProductVersion"), &pvProductVersion, &iProductVersionLen))
    {
        TRACE("Can't obtain ProductName and ProductVersion from resources\n");
        return false;
    }

    strProductName.SetString((LPCTSTR)pvProductName, iProductNameLen);
    strProductVersion.SetString((LPCTSTR)pvProductVersion, iProductVersionLen);

    return true;
}
网友评论
<