我可以为 CHAR 和 WCHAR 写入版本信息 API 吗?

Can I Write Version Information API For Both CHAR And WCHAR?

我离目标还有点距离。

GetFileVersionInfoSize() 与其他两个函数 GetFileVersionInfo()VerQueryValue() 一起工作正常。我想给它添加更多功能以使其完整。

我在 WCHAR 上将其编码为 运行,想知道为 CHAR 编码为 运行 是否有意义?

有没有办法绕过它,这样我就可以编写一次代码,并且对两者都适用?

此外,有没有办法枚举 \\StringFileInfo\\lang-codepage\\* 的内容?

DWORD GetFileVersionInfo3(const TCHAR *pszFilePath, std::vector<std::pair<std::wstring, std::wstring>> *lplist)
{
    DWORD               dwSize = 0;
    BYTE                *pbVersionInfo = NULL;
    VS_FIXEDFILEINFO    *pFileInfo = NULL;
    UINT                puLenFileInfo = 0;

    dwSize = GetFileVersionInfoSize(pszFilePath, NULL);
    if (dwSize == 0)
    {
        printf("\nError in GetFileVersionInfoSize: %d\n", GetLastError());
        return 1;
    }

    pbVersionInfo = new BYTE[dwSize];
    memset(pbVersionInfo, '[=11=]', dwSize);

    if (!GetFileVersionInfo(pszFilePath, 0, dwSize, pbVersionInfo))
    {
        printf("\nError in GetFileVersionInfo: %d\n", GetLastError());
        delete[] pbVersionInfo;
        return 1;
    }

    if (!VerQueryValue(pbVersionInfo, TEXT("\"), (LPVOID*)&pFileInfo, &puLenFileInfo))
    {
        printf("\nError in VerQueryValue: %d\n", GetLastError());
        delete[] pbVersionInfo;
        return 1;
    }

    if (!VerQueryValue(pbVersionInfo, TEXT("\VarFileInfo\Translation"), (LPVOID*)&lpTranslate, &puLenFileInfo))
    {
        printf("\nError in VerQueryValue: %d\n", GetLastError());
        return 1;
    }

    std::vector<std::wstring>::iterator itr;
    std::vector<std::wstring> wlist;
    wlist.clear();
    wlist.push_back(L"FileDescription");
    wlist.push_back(L"InternalName");
    wlist.push_back(L"OriginalFilename");
    wlist.push_back(L"CompanyName");
    wlist.push_back(L"FileVersion");
    wlist.push_back(L"ProductName");
    wlist.push_back(L"ProductVersion");
    wlist.push_back(L"LegalCopyright");

    char fileEntry[1024];
    for (int i = 0; i < (puLenFileInfo / sizeof(struct LANGANDCODEPAGE)); i++)
    {
        sprintf_s(fileEntry, 1024, "\StringFileInfo\%04x%04x\",
        lpTranslate[i].wLanguage,
        lpTranslate[i].wCodePage);

        lplist->push_back(std::pair<std::wstring, std::wstring>(L"File: ", pszFilePath));

        std::string s1(fileEntry);
        for (itr = wlist.begin(); itr != wlist.end(); itr++)
        {
            std::wstring item = *itr;
            std::wstring wstr;
            wstr.append(s1.begin(), s1.end());
            wstr.append(item);
            LPVOID lpBuffer = NULL;
            UINT   dwBytes = 0;
            bool bRes = VerQueryValue(pbVersionInfo, wstr.c_str(), (LPVOID*)&lpBuffer, &dwBytes);
            if (!bRes)
            {
                continue;
            }
            LPTSTR wsResult;
            wsResult = (LPTSTR)lpBuffer;
            lplist->push_back(std::pair<std::wstring, std::wstring>(item, wsResult));
        }
    }

    return 0;
}

由于您使用的是TCHAR,所以使用std:::basic_string<TCHAR>而不是std::wstring来匹配。否则,删除 TCHAR 并为所有内容使用 WCHAR