首页 > 代码库 > 获取文件版本

获取文件版本

GetFileInfo
CUIAction::GetFileVersion2
GetSystemDirectory

    WCHAR szConfigFile[MAX_PATH + 1];
   
::GetSystemDirectory(szConfigFile, MAX_PATH);
  
  ::PathAppend(szConfigFile, FileBootCleanConfigFileName);

void GetApplicationVersion(char * strVersion){ TCHAR szFullPath[MAX_PATH]; DWORD dwVerInfoSize = 0; DWORD dwVerHnd; VS_FIXEDFILEINFO * pFileInfo; GetModuleFileName(NULL, szFullPath, sizeof(szFullPath)); dwVerInfoSize = GetFileVersionInfoSize(szFullPath, &dwVerHnd); if (dwVerInfoSize) { // If we were able to get the information, process it: HANDLE hMem; LPVOID lpvMem; unsigned int uInfoSize = 0; hMem = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize); lpvMem = GlobalLock(hMem); GetFileVersionInfo(szFullPath, dwVerHnd, dwVerInfoSize, lpvMem); ::VerQueryValue(lpvMem, (LPTSTR)("\\"), (void**)&pFileInfo, &uInfoSize); WORD m_nProdVersion[4]; // Product version from the FILEVERSION of the version info resource m_nProdVersion[0] = HIWORD(pFileInfo->dwProductVersionMS); m_nProdVersion[1] = LOWORD(pFileInfo->dwProductVersionMS); m_nProdVersion[2] = HIWORD(pFileInfo->dwProductVersionLS); m_nProdVersion[3] = LOWORD(pFileInfo->dwProductVersionLS); sprintf(strVersion, "%d.%d.%d.%d", m_nProdVersion[0], m_nProdVersion[1],m_nProdVersion[2],m_nProdVersion[3]); GlobalUnlock(hMem); GlobalFree(hMem); } else { strcpy(strVersion, "1.0"); }}

 

BOOL CModuleVersion::GetDLLVersionInfo(LPCTSTR filename){    m_translation.charset = 1252;    // default = ANSI code page    memset((VS_FIXEDFILEINFO*)this, 0, sizeof(VS_FIXEDFILEINFO));    // get module handle    //HMODULE hModule = LoadLibrary(filename);    //if (hModule==NULL)    //    return FALSE;    // some times, LoadLibrary may fail , --- by bbcallen    HMODULE hModule = LoadLibraryEx(filename, NULL, LOAD_LIBRARY_AS_DATAFILE);    if (hModule==NULL)        return FALSE;    // read file version info    DWORD dwDummyHandle; // will always be set to zero    DWORD len = GetFileVersionInfoSize(filename, &dwDummyHandle);    if (len <= 0)    {        FreeLibrary(hModule);        return FALSE;    }    BYTE* pbData = http://www.mamicode.com/new BYTE[len]; // allocate version info"\\"), &lpvi, &iLen))    {        FreeLibrary(hModule);        return FALSE;    }    // copy fixed info to myself, which am derived from VS_FIXEDFILEINFO    *(VS_FIXEDFILEINFO*)this = *(VS_FIXEDFILEINFO*)lpvi;    // Get translation info    if (VerQueryValue(m_atpVersionInfo,        _T("\\VarFileInfo\\Translation"), &lpvi, &iLen) && iLen >= 4) {        m_translation = *(TRANSLATION*)lpvi;    }    FreeLibrary(hModule);    return dwSignature == VS_FFI_SIGNATURE;}

 

CString CGlobalFun::getIEVersion(){	TCHAR szFilename[] = _T("mshtml.dll");  	DWORD dwMajorVersion =0, dwMinorVersion = 0;  	DWORD dwBuildNumber =0, dwRevisionNumber = 0;  	DWORD dwHandle = 0;  	DWORD dwVerInfoSize = GetFileVersionInfoSize(szFilename, &dwHandle);  	if (dwVerInfoSize)  	{  		LPVOID lpBuffer = LocalAlloc(LPTR, dwVerInfoSize);  		if (lpBuffer)  		{  			if (GetFileVersionInfo(szFilename, dwHandle, dwVerInfoSize, lpBuffer))  			{  				VS_FIXEDFILEINFO * lpFixedFileInfo = NULL;  				UINT nFixedFileInfoSize = 0;  				if (VerQueryValue(lpBuffer, TEXT("\\"), (LPVOID*)&lpFixedFileInfo, &nFixedFileInfoSize) &&(nFixedFileInfoSize))  				{  					dwMajorVersion = HIWORD(lpFixedFileInfo->dwFileVersionMS);    					dwMinorVersion = LOWORD(lpFixedFileInfo->dwFileVersionMS);    					dwBuildNumber = HIWORD(lpFixedFileInfo->dwFileVersionLS);    					dwRevisionNumber = LOWORD(lpFixedFileInfo->dwFileVersionLS);  				}  			}  			LocalFree(lpBuffer);  		}  	}  	CString strVersion;	strVersion.Format(_T("MSIE %d.%d"),dwMajorVersion,dwMinorVersion);	/*printf("mshtml.dll VersionInfo: \n\tMajorVersion=%d\n\tMinorVersion=%d\n\tBuildNumber=%d\n\tRevisionNumber=%d\n",  		dwMajorVersion, dwMinorVersion, dwBuildNumber, dwRevisionNumber); */	return strVersion;}

 

HRESULT CBootCheckTool::CopyDriverIfNew(LPCWSTR lpszNewDriver){    WCHAR szOldDriver[MAX_PATH + 1];    UINT  uRet = ::GetSystemDirectory(szOldDriver, MAX_PATH);    if (!uRet)        return GetLastError() ? AtlHresultFromLastError() : E_FAIL;    ::PathAppend(szOldDriver, L"Drivers");    ::PathAppend(szOldDriver, BC_DriverFileName);    ULARGE_INTEGER uiOldVersion = GetDllVersion(szOldDriver);    ULARGE_INTEGER uiNewVersion = GetDllVersion(lpszNewDriver);    if (uiNewVersion.QuadPart <= uiOldVersion.QuadPart)        return S_FALSE; // 不需要拷贝    // 拷贝较新的版本    BOOL br = ::CopyFile(lpszNewDriver, szOldDriver, FALSE);    if (!br)        return GetLastError() ? AtlHresultFromLastError() : E_FAIL;    return S_OK;}ULARGE_INTEGER CBootCheckTool::GetDllVersion(LPCWSTR lpszFilePath){    BOOL  bRet              = FALSE;    DWORD dwHandle          = 0;    DWORD dwFileInfoSize    = 0;    VS_FIXEDFILEINFO *pFixFileInfo = NULL;    TCHAR *pszFileInfo      = NULL;    UINT  uLen              = 0;    ULARGE_INTEGER uiVersion;    uiVersion.HighPart = 0;    uiVersion.LowPart  = 0;    dwFileInfoSize = ::GetFileVersionInfoSize(lpszFilePath, &dwHandle);    if (dwFileInfoSize == 0)        goto Exit0;          pszFileInfo = new TCHAR[dwFileInfoSize];    if (pszFileInfo == NULL)        goto Exit0;          bRet = ::GetFileVersionInfo(lpszFilePath, dwHandle, dwFileInfoSize, pszFileInfo);    if (!bRet)        goto Exit0;    bRet = ::VerQueryValue(pszFileInfo, _T("\\"), (LPVOID *)&pFixFileInfo, &uLen);    if (!bRet)        goto Exit0;    if (uLen > 0)    {        uiVersion.HighPart = pFixFileInfo->dwFileVersionMS;        uiVersion.LowPart  = pFixFileInfo->dwFileVersionLS;    }Exit0:    if (pszFileInfo)    {        delete pszFileInfo;        pszFileInfo = NULL;    }    return uiVersion;}