// VersionString.cpp // // Gets the version from the target file. // // lib files: version.lib // // Revision Log // // Date Who SAR Notes // ========== === ======= ===================================== // 1998-11-21 mph Initial coding by Mark Henri of // MPH Software - markhenri@attbi.com // #include "VersionString.h" #include std::string GetVersionString(const char * a_pszFilePath) { char ach[128]; GetVersionString(a_pszFilePath, ach, sizeof(ach)); std::string s = ach; return s; } bool GetVersionString(const char * a_pszFilePath, char * a_pszVersion, int a_uVersionSize) { if (a_pszFilePath[0] == '\0') { a_pszVersion[0] = 0; return false; } DWORD dwHandle = 0; DWORD dwVersionInfoSize; DWORD dwError; PVOID pFileInfo; PBYTE pVersionInfo; PDWORD pTranslation = NULL; UINT uLength = 0; char szString[512] = ""; dwVersionInfoSize = GetFileVersionInfoSize( (char*)a_pszFilePath, /* pointer to filename string */ & dwHandle); /* pointer to variable to receive zero */ if (! dwVersionInfoSize) { dwError = GetLastError(); return false; } pFileInfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwVersionInfoSize); pVersionInfo =(unsigned char*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwVersionInfoSize); if (! GetFileVersionInfo((char*)a_pszFilePath, /* pointer to filename string */ (DWORD) dwHandle, /* ignored */ dwVersionInfoSize, /* size of buffer */ pFileInfo)) /* pointer to buffer to receive file-version info.*/ { dwError = GetLastError(); HeapFree(GetProcessHeap(), 0, pFileInfo); HeapFree(GetProcessHeap(), 0, pVersionInfo); return false; } if (! VerQueryValue(pFileInfo, /* address of buffer for version resource */ TEXT("\\VarFileInfo\\Translation"), /* address of value to retrieve */ (void**)(& pTranslation), /* address of buffer for version pointer */ & uLength) /* address of version-value length buffer */ ) { dwError = GetLastError(); HeapFree(GetProcessHeap(), 0, pFileInfo); HeapFree(GetProcessHeap(), 0, pVersionInfo); return false; } wsprintf(szString, "\\StringFileInfo\\%04x%04x\\FileVersion", LOWORD((DWORD) * pTranslation,), HIWORD((DWORD) * pTranslation)); if (! VerQueryValue(pFileInfo, /* address of buffer for version resource */ szString, /* address of value to retrieve */ (PVOID *) & pVersionInfo, /* address of buffer for version pointer */ & uLength) /* address of version-value length buffer */ ) { dwError = GetLastError(); HeapFree(GetProcessHeap(), 0, pFileInfo); HeapFree(GetProcessHeap(), 0, pVersionInfo); return false; } if (lstrlen((const char*)pVersionInfo) >=(int) a_uVersionSize) lstrcpyn(a_pszVersion,(const char*)pVersionInfo, a_uVersionSize - 1); else lstrcpy(a_pszVersion,(const char*)pVersionInfo); HeapFree(GetProcessHeap(), 0, pFileInfo); HeapFree(GetProcessHeap(), 0, pVersionInfo); // remove spaces and convert to periods char *p1; char *p2; for (p1=a_pszVersion, p2=a_pszVersion ; ; ++p2) { while (*p2 == ' ') ++p2; if (*p2 == ',') *p2 = '.'; *p1++ = *p2; if (*p2 == '\0') break; } return TRUE; }