std::string strVersion

GetModuleVersion("test.exe", "ProductVersion", strVersion);

 

/** @fn     GetModuleVersion
     *  @brief  获取指定模块名称,版本关键字的模块版本号
     *  @param  lpszMoudleName 模块名称
     *  @param  lpszKeyName 版本关键字
     *  @param  strVerValue 输出版本号
     *  @return NULL
     */
    void GetModuleVersion(LPTSTR lpszMoudleName, LPCTSTR lpszKeyName, std::string& strVerValue) const
    {
        void* pVersionInfo = NULL;
        TRANSLATION stTanslation;
        memset((char*)&stTanslation, 0, sizeof(TRANSLATION));

        // read file version info
        DWORD dwHandle; // a variable that the function sets to zero.
        DWORD dwSize = GetFileVersionInfoSize(lpszMoudleName, &dwHandle);
        if (dwSize > 0)
        {
            pVersionInfo = malloc(dwSize); // allocate version info
            if (::GetFileVersionInfo(lpszMoudleName, 0, dwSize, pVersionInfo))
            {
                LPVOID lpvi;
                UINT uLen;
                if (VerQueryValue(pVersionInfo, "\\", &lpvi, &uLen))
                {
                    //*(VS_FIXEDFILEINFO*)this = *(VS_FIXEDFILEINFO*)lpvi;
                    // Get translation info
                    if (VerQueryValue(pVersionInfo, "\\VarFileInfo\\Translation", &lpvi, &uLen) && uLen >= 4)
                    {
                            stTanslation = *(TRANSLATION *)lpvi;
                    }
                }
            }
        }

        if (pVersionInfo)
        {
            // To get a string value must pass query in the form
            //
            //    "\StringFileInfo\<langID><codepage>\keyname"
            //
            // where <lang-codepage> is the languageID concatenated with the
            // code page, in hex.
            //
            char query[128] = {0};
            sprintf(query,"\\StringFileInfo\\%04x%04x\\%s", stTanslation.langID, stTanslation.charset, lpszKeyName);
            LPCTSTR pVal;
            UINT iLenVal;
            if (VerQueryValue(pVersionInfo, (LPTSTR)(LPCTSTR)query, (LPVOID*)&pVal, &iLenVal))
            {
                strVerValue = pVal;
            }

            free(pVersionInfo);
        }
    }