检索文件描述应用程序VerQueryValue

时间:2018-11-02 18:19:42

标签: c++ windows winapi

我真的很困惑这个功能。目前,我已成功检索文件版本和产品版本。现在我想在应用程序中检索更多信息。例如FileDescription和CompanyName。

DWORD dwLen;
VS_FIXEDFILEINFO    *pFileInfo;
UINT                pLenFileInfo;

dwLen = GetFileVersionInfoSize("D:/firefox.exe", NULL);

BYTE *sKey = new BYTE[dwLen];

GetFileVersionInfo("D:/firefox.exe", NULL, dwLen, sKey);

VerQueryValue(sKey, "\\", (LPVOID*)&pFileInfo, &pLenFileInfo);

// at now i can retrieve file Version with structure VS_FIXEDFILEINFO

VerQueryValue(sKey, "\\StringFileInfo\\%04x%09x\\FileDescription", (LPVOID*) &pFileInfo, &pLenFileInfo);

delete[] sKey;

cout << pFileInfo;

// it return address buffer `00230428`;

如何准确返回文件描述Firefox?参数3 LPVOID中检索文件说明的结构如何?在我的代码中,我两次将pFileInfo传递给VerQueryValue

新编辑

    DWORD dwLen;

    struct LANGANDCODEPAGE {
            WORD wLanguage;
            WORD wCodePage;
        } *lpTranslate;

    dwLen = GetFileVersionInfoSize("D:/firefox.exe", NULL);

        BYTE *sKey = new BYTE[dwLen];
TCHAR *sCompanyName = new char[1024];

        GetFileVersionInfo("D:/firefox.exe", NULL, dwLen, sKey);

    VerQueryValue(sKey, "\\VarFileInfo\\Translation", (LPVOID*)&lpTranslate, &pLenFileInfo);

VerQueryValue(test, "\\StringFileInfo\\%04x%09x\\FileDescription", (LPVOID*)&sCompanyName, &pLenFileInfo);

delete[] sKey;

cout << lpTranslate -> wLanguage;

1 个答案:

答案 0 :(得分:1)

VerQueryValue(test, "\\StringFileInfo\\%04x%09x\\FileDescription",
   (LPVOID*)&sCompanyName, &pLenFileInfo);

第二个参数的格式应为"\\StringFileInfo\\NxM\\FileDescription",其中NM分别是wLanguagewCodePage。按照注释部分中的示例,您可以使用"%04x%04x"作为打印格式说明符来创建字符串。示例:

BOOL foo()
{
    const char* filename = "c:\\windows\\hh.exe";
    int dwLen = GetFileVersionInfoSize(filename, NULL);
    if(!dwLen)
        return 0;

    auto *sKey = new BYTE[dwLen];
    std::unique_ptr<BYTE[]> skey_automatic_cleanup(sKey);
    if(!GetFileVersionInfo(filename, NULL, dwLen, sKey))
        return 0;

    struct LANGANDCODEPAGE {
        WORD wLanguage;
        WORD wCodePage;
    } *lpTranslate;

    UINT cbTranslate = 0;
    if(!VerQueryValue(sKey, "\\VarFileInfo\\Translation",
        (LPVOID*)&lpTranslate, &cbTranslate))
        return 0;

    for(unsigned int i = 0; i < (cbTranslate / sizeof(LANGANDCODEPAGE)); i++)
    {
        char subblock[256];
        //use sprintf if sprintf_s is not available
        sprintf_s(subblock, "\\StringFileInfo\\%04x%04x\\FileDescription",
            lpTranslate[i].wLanguage, lpTranslate[i].wCodePage);
        char *description = NULL;
        UINT dwBytes;
        if(VerQueryValue(sKey, subblock, (LPVOID*)&description, &dwBytes))
            MessageBox(0, description, 0, 0);
    }
    return TRUE;
}