본문 바로가기

프로그래밍/코드 조각

파일버전, 제품버전 가져오기

//////////////////////////////////////////////////////////////////////////
// 선언

enum FileInfo { Comments, InternalName, ProductName, CompanyName, LegalCopyright, ProductVersion, 
                         FileDescription, LegalTrademarks, PrivateBuild, FileVersion, OriginalFilename, SpecialBuild };
tstring GetFileInfo(__in const tstring& filename, __in FileInfo fi);

tstring GetFileVersion(__in const tstring& filename);
tstring GetProductVersion(__in const tstring& filename);


//////////////////////////////////////////////////////////////////////////
// 구현

tstring GetFileInfo(__in const tstring& filename, __in FileInfo fi)
{
    if(GetFileAttributes(filename.c_str()) == INVALID_FILE_ATTRIBUTES) {
        return _T("");
    }

    static TCHAR* FileInfoMap[] = { _T("Comments"), _T("InternalName"), _T("ProductName"), 
                                                      _T("CompanyName"), _T("LegalCopyright"), _T("ProductVersion"), 
                                                      _T("FileDescription"), _T("LegalTrademarks"), _T("PrivateBuild"), 
                                                      _T("FileVersion"), _T("OriginalFilename"), _T("SpecialBuild") };
    tstring fileinfo;
    DWORD dwHandle;
    DWORD dwVerInfoSize;
    dwVerInfoSize = GetFileVersionInfoSize(filename.c_str(), &dwHandle);
    if (dwVerInfoSize)
    {
        TCHAR* pVerInfoData = new TCHAR[dwVerInfoSize + 1];

        if (GetFileVersionInfo(filename.c_str(), dwHandle, dwVerInfoSize, pVerInfoData))
       {
            UINT uLen;
            DWORD* pLangCode;
            if (VerQueryValue(pVerInfoData, _T("\\VarFileInfo\\Translation"), (void**)&pLangCode, &uLen))
            {
                TCHAR subBlock[MAX_PATH] = {0,};
                wsprintf(subBlock, _T("\\StringFileInfo\\%04X%04X\\%s"), LOWORD(*pLangCode), HIWORD(*pLangCode), FileInfoMap[fi]);

                TCHAR buffer[32] = {0,};
                TCHAR* pVer = buffer;
                if (VerQueryValue(pVerInfoData, subBlock, (void**)&pVer, &uLen))
                {
                    fileinfo = pVer;
                }
            }
        }
        delete [] pVerInfoData;
    }
    return fileinfo;
}

tstring GetFileVersion(__in const tstring& filename)
{
    tstring filever = GetFileInfo(filename, FileVersion);
    return filever;
}

tstring GetProductVersion(__in const tstring& filename)
{
    tstring prodver = GetFileInfo(filename, ProductVersion);
    return prodver;
}