1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
// récupération des informations de version du fichier .exe ou dll.
// les zones suivantes sont disponibles:/
// "FixedFileVersion","FixedProductVersion"
// "Comments","InternalName","ProductName",
// "CompanyName","LegalCopyright","ProductVersion",
// "FileDescription","LegalTrademarks","PrivateBuild",
// "FileVersion","OriginalFilename","SpecialBuild"
class CVersionFile
{
private: CVersionFile(const CVersionFile&); // pas de constructeur de copie
private: void operator=(const CVersionFile&); // pas d'operateur de copie.
public:
CVersionFile(){}
// la valeur de la clef szLib correspondant a une entrée du dossier version
CString GetInfos(const TCHAR *szLib);
// permet d'itérer les valeurs stockées.
// charge la valeur rValue pour la clef correspondante dans rKeyName
// rFirstOne doit etre initialisée à true au premier appel de la fonction.
bool GetNextInfos(bool &rFirstOne,CString &rstrKeyName,CString &rstrValue);
// récuperation des valeurs
// const char *szFileName le module spécifié ou si null 'exe en cours d'execution.
// WORD wLanguage : le langage à lire ex:SUBLANG_ENGLISH_US ou si 0 le premier langage utilisé
// WORD wCodePage : la page code à lire ex:LANG_ENGLISH ou si 0 la premiere page de code utilisée.
bool GetInfosFile(const TCHAR *szFileName=NULL,WORD wLanguage=0,WORD wCodePage=0);
protected:
CMap<CString ,const TCHAR *,CString ,const TCHAR *> m_aListInfos;
POSITION m_pos;
};
//-----------------------------------------------------------------
CString CVersionFile::GetInfos(const TCHAR *szLib)
{
//
CString str;
m_aListInfos.Lookup(szLib,str);
return str;
}
//-----------------------------------------------------------------
bool CVersionFile::GetNextInfos(bool &rFirstOne,CString &rstrKeyName,CString &rstrValue)
{
//
if(rFirstOne) m_pos = m_aListInfos.GetStartPosition();
rFirstOne=false;
if(m_pos!= NULL)
{
m_aListInfos.GetNextAssoc(m_pos,rstrKeyName,rstrValue);
return true;
}
return false;
}
//-----------------------------------------------------------------
bool CVersionFile::GetInfosFile(const TCHAR *szFileName /*=NULL*/,WORD wLanguage /*=0*/,WORD wCodePage /*=0*/)
{
//
DWORD dwVerInfoSize;
DWORD dwHnd;
void* pBuffer;
VS_FIXEDFILEINFO *pFixedInfo;
LPVOID lpVersionBuffer;
UINT uLen;
TCHAR szGetName[500];
TCHAR szExeName[MAX_PATH];
m_aListInfos.RemoveAll();
if(!szFileName)
GetModuleFileName(AfxGetInstanceHandle(), szExeName, sizeof (szExeName));
#ifdef _UNICODE
else wcscpy(szExeName,szFileName);
#else
else strcpy(szExeName,szFileName);
#endif
dwVerInfoSize = GetFileVersionInfoSize(szExeName, &dwHnd);
if(dwVerInfoSize)
{
CString str;
pBuffer = new TCHAR[dwVerInfoSize];
if (pBuffer == NULL) return false;
GetFileVersionInfo(szExeName, dwHnd, dwVerInfoSize, pBuffer);
// récupère les infos fixes indépendantes du langage.
// FixedProductVersion
VerQueryValue(pBuffer,_T("\\"),(void**)&pFixedInfo,(UINT *)&uLen);
str.Format (_T("%u,%u,%u,%u"), HIWORD (pFixedInfo->dwProductVersionMS),
LOWORD (pFixedInfo->dwProductVersionMS),
HIWORD (pFixedInfo->dwProductVersionLS),
LOWORD (pFixedInfo->dwProductVersionLS));
m_aListInfos.SetAt(_T("FixedProductVersion"),str);
//FixedFileVersion
str.Format (_T("%u,%u,%u,%u"),HIWORD (pFixedInfo->dwFileVersionMS),
LOWORD (pFixedInfo->dwFileVersionMS),
HIWORD (pFixedInfo->dwFileVersionLS),
LOWORD (pFixedInfo->dwFileVersionLS));
m_aListInfos.SetAt(_T("FixedFileVersion"),str);
// infos liées au langage.
unsigned int cbTranslate;
struct LANGANDCODEPAGE
{
WORD wLanguage;
WORD wCodePage;
} *lpTranslate;
// Read the list of languages and code pages.
VerQueryValue(pBuffer,
TEXT("\\VarFileInfo\\Translation"),
(LPVOID*)&lpTranslate,
&cbTranslate);
TCHAR *aszLib[]={_T("Comments"),_T("InternalName"),_T("ProductName"),
_T("CompanyName"),_T("LegalCopyright"),_T("ProductVersion"),
_T("FileDescription"),_T("LegalTrademarks"),_T("PrivateBuild"),
_T("FileVersion"),_T("OriginalFilename"),_T("SpecialBuild")
};
// lecture des infos pour chage langue et page de code
for(int i=0; i < (cbTranslate/sizeof(struct LANGANDCODEPAGE)); i++ )
{
// filtrage langage et page de code désirée ?
if(wLanguage && lpTranslate[i].wLanguage!=wLanguage) continue;
if(wCodePage && lpTranslate[i].wCodePage!=wCodePage) continue;
for(int n=0;n<sizeof(aszLib)/sizeof(char *);n++)
{
str.Format(_T("\\StringFileInfo\\%04x%04x\\%s"),
lpTranslate[i].wLanguage,
lpTranslate[i].wCodePage,
aszLib[n]);
lstrcpy(szGetName,str);
// recuperation de l'information.
if (VerQueryValue(pBuffer,szGetName,(void**)&lpVersionBuffer,(UINT *)&uLen) != 0)
{
if(lpVersionBuffer)
{
str = (LPTSTR)lpVersionBuffer;
m_aListInfos.SetAt(aszLib[n],str);
}
}
}
}
delete [] pBuffer;
}
return (m_aListInfos.GetCount()!=0);
} |
Partager