analyzetool/commandlineengine/src/version.cpp
branchRCL_3
changeset 13 da2cedce4920
equal deleted inserted replaced
12:d27dfa8884ad 13:da2cedce4920
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Show / Check atool version.
       
    15 *
       
    16 */
       
    17 #include "../inc/ATCommonDefines.h"
       
    18 #include "../inc/CATBase.h"
       
    19 
       
    20 //Function declarations.
       
    21 int showVersionInfo( void );
       
    22 int showCoreVersionInfo( void );
       
    23 bool readCoreVersionInfo( const string& sFile, string& sVersion );
       
    24 string findAnalyzeToolHeader( void );
       
    25 int showDbgHelpVersionInfo( bool showVersion );
       
    26 int getDllVersion( LPCTSTR libName, WORD *majorVersion, WORD *minorVersion, WORD *buildNumber, WORD *revisionNumber );
       
    27 
       
    28 //External GetEpocRoot function
       
    29 extern bool GetEpocRoot( string &sEpocRoot );
       
    30 
       
    31 void convertWriteTimeToLocalTime( FILETIME ftWrite, LPSTR lpszString );
       
    32 
       
    33 int showCoreVersionInfo( void )
       
    34 {
       
    35 	LOG_FUNC_ENTRY( "version.cpp::showCoreVersionInfo" );
       
    36 	// Try find header from environment where to locate version info.
       
    37 	string sHeaderFile = findAnalyzeToolHeader();
       
    38 	if ( sHeaderFile.empty() )
       
    39 		return 0;
       
    40 	string sVersion("");
       
    41 	if ( readCoreVersionInfo( sHeaderFile, sVersion ) )
       
    42 	{
       
    43 		cout << "AnalyzeTool SDK binaries version: " 
       
    44 			<< sVersion
       
    45 			<< endl;
       
    46 	}
       
    47 	return 0;
       
    48 }
       
    49 
       
    50 
       
    51 /**
       
    52 * Find analyzetool.h header file
       
    53 * @return string containing full path to file or empty string if not found.
       
    54 */
       
    55 string findAnalyzeToolHeader( void )
       
    56 {
       
    57 	LOG_FUNC_ENTRY( "version.cpp::findAnalyzeToolHeader" );
       
    58 	string sEpocRoot;
       
    59 	if ( ! CATBase::GetEpocRoot( sEpocRoot ) )
       
    60 		return string("");
       
    61 	int iC = sizeof( AT_CORE_INCLUDE_FILE_WITH_VERSION_NUMBER ) / sizeof ( string );
       
    62 	for( int i = 0 ; i < iC ; i++ )
       
    63 	{
       
    64 		string sCheck( sEpocRoot );
       
    65 		sCheck.append( AT_CORE_INCLUDE_FILE_WITH_VERSION_NUMBER[i] );
       
    66 		if ( CATBase::FileExists( sCheck.c_str() ) )
       
    67 			return sCheck;
       
    68 	}
       
    69 	return string("");
       
    70 }
       
    71 /**
       
    72 * Read core version string.
       
    73 * @param sVersion string will contain version info if funtion returns true.
       
    74 * @return true if successful.
       
    75 */
       
    76 bool readCoreVersionInfo( const string& sFile, string& sVersion )
       
    77 {
       
    78 	LOG_FUNC_ENTRY( "version.cpp::readCoreVersionInfo" );
       
    79 	try {
       
    80 		ifstream in;
       
    81 		in.open( sFile.c_str() );
       
    82 		if ( ! in.good() )
       
    83 			return false;
       
    84 		char cBuff[MAX_LINE_LENGTH];
       
    85 		while ( in.good() )
       
    86 		{
       
    87 			in.getline( cBuff, MAX_LINE_LENGTH );
       
    88 			string s( cBuff );
       
    89 			if ( s.find( AT_CORE_VERSION_NUMBER_TAG ) != string::npos )
       
    90 			{
       
    91 				// Find spot after first space (ignore first 3 chars).
       
    92 				size_t t =  s.find_first_of( ' ', 3 )+1;
       
    93 				sVersion = s.substr( t, s.size()-t );
       
    94 				return true;
       
    95 			}
       
    96 		}
       
    97 	}
       
    98 	catch(...)
       
    99 	{
       
   100 		LOG_STRING(AT_MSG << "Exception reading core version info.");
       
   101 	}
       
   102 	return false;
       
   103 }
       
   104 
       
   105 /**
       
   106 * Print version information of atool.exe binary.
       
   107 */
       
   108 int showVersionInfo( void )
       
   109 {
       
   110 	LOG_FUNC_ENTRY( "version.cpp::showVersionInfo" );
       
   111 	string sTemp( "Version: " );
       
   112 	sTemp.append( ATOOL_VERSION );
       
   113 	sTemp.append( "\n" );
       
   114 	//Print atool version
       
   115 	printf( sTemp.c_str() );
       
   116 
       
   117 	//atool.exe:s path + filename
       
   118 	char buffer[MAX_PATH];
       
   119 
       
   120 	GetModuleFileName( NULL, buffer, MAX_PATH );
       
   121 
       
   122 	printf( "Path: %s\n", buffer );
       
   123 
       
   124 	WIN32_FIND_DATA FindFileData;
       
   125 	HANDLE hFind;
       
   126 	//Get file handle
       
   127 	hFind = FindFirstFile( buffer, &FindFileData );
       
   128 
       
   129 	if( hFind == INVALID_HANDLE_VALUE )
       
   130 	{
       
   131 		printf( "Can not find file:%s", buffer );
       
   132 		return 0;
       
   133 	}
       
   134 
       
   135 	convertWriteTimeToLocalTime( FindFileData.ftLastWriteTime , buffer );
       
   136 	printf( "Modified: %s\n", buffer );
       
   137 
       
   138 	// Show core version information.
       
   139 	showCoreVersionInfo();
       
   140 	return 0;
       
   141 }
       
   142 
       
   143 // Convert the last-write time to local time.
       
   144 void convertWriteTimeToLocalTime( FILETIME ftWrite, LPSTR lpszString )
       
   145 {
       
   146 	LOG_FUNC_ENTRY( "version.cpp::convertWriteTimeToLocalTime" );
       
   147 	SYSTEMTIME stUTC, stLocal;
       
   148     // Convert the last-write time to local time.
       
   149     FileTimeToSystemTime(&ftWrite, &stUTC);
       
   150     SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
       
   151 
       
   152     // Build a string showing the date and time.
       
   153     wsprintf(lpszString, "%02d/%02d/%d %02d:%02d:%02d",
       
   154         stLocal.wDay, stLocal.wMonth, stLocal.wYear,
       
   155         stLocal.wHour, stLocal.wMinute, stLocal.wSecond);
       
   156 }
       
   157 
       
   158 /**
       
   159 * Print version information of dbghelp.dll.
       
   160 */
       
   161 int showDbgHelpVersionInfo( bool showVersion )
       
   162 {
       
   163 	LOG_FUNC_ENTRY( "version.cpp::showDbgHelpVersionInfo" );
       
   164 
       
   165 	WORD majorVersion;
       
   166 	WORD minorVersion;
       
   167 	WORD buildNumber;
       
   168 	WORD revisionNumber;
       
   169 
       
   170 	if(getDllVersion( _T(DBGHELP_DLL_NAME), &majorVersion, &minorVersion, &buildNumber, &revisionNumber))
       
   171 	{
       
   172 		//Check if current dll version is lower than latest version
       
   173 		if((majorVersion < DBGHELP_VERSION_MAJ) ||
       
   174 			(majorVersion == DBGHELP_VERSION_MAJ && minorVersion < DBGHELP_VERSION_MIN) ||
       
   175 			(majorVersion == DBGHELP_VERSION_MAJ && minorVersion == DBGHELP_VERSION_MIN && buildNumber < DBGHELP_VERSION_BUILD) ||
       
   176 			(majorVersion == DBGHELP_VERSION_MAJ && minorVersion == DBGHELP_VERSION_MIN && buildNumber == DBGHELP_VERSION_BUILD && revisionNumber < DBGHELP_VERSION_REVIS))
       
   177 		{
       
   178 			cout << "WARNING: Version of your dbghelp.dll is "
       
   179 					<< majorVersion << "."
       
   180 					<< minorVersion << "."
       
   181 					<< buildNumber << "."
       
   182 					<< revisionNumber << ".\n";
       
   183 
       
   184 			cout << "Source code pinpointing for winscw may not work properly with this version."
       
   185 				<< endl
       
   186 				<< "Please, update it to at least version "
       
   187 				<< DBGHELP_VERSION_MAJ << "." 
       
   188 				<< DBGHELP_VERSION_MIN << "." 
       
   189 				<< DBGHELP_VERSION_BUILD << "." 
       
   190 				<< DBGHELP_VERSION_REVIS << ".\n"
       
   191 				<< "dbghelp.dll file is included in Debugging Tools from Microsoft. "
       
   192 				<< "You can download and install it from\n"
       
   193 				<< "http://www.microsoft.com/whdc/devtools/debugging/\n";
       
   194 
       
   195 			return 1;
       
   196 		}
       
   197 		else
       
   198 		{
       
   199 			if( showVersion )
       
   200 			{
       
   201 				cout << "Version of your dbghelp.dll is "
       
   202 					<< majorVersion << "."
       
   203 				    << minorVersion << "."
       
   204 				    << buildNumber << "."
       
   205 				    << revisionNumber << "."
       
   206 				    << endl;
       
   207 				cout << "No need for update.\n";
       
   208 			}
       
   209 		}
       
   210 	}
       
   211 
       
   212 	return 0;
       
   213 }
       
   214 
       
   215 /**
       
   216 * Get dll version.
       
   217 * @param libName library name.
       
   218 * @param majorVersion will contain major version if funtion returns true.
       
   219 * @param minorVersion will contain minor version if funtion returns true.
       
   220 * @param buildNumber will contain build number if funtion returns true.
       
   221 * @param revisionNumber will contain revision number if funtion returns true.
       
   222 * @return true if successful.
       
   223 */
       
   224 int getDllVersion( LPCTSTR  libName, WORD *majorVersion, WORD *minorVersion, WORD *buildNumber, WORD *revisionNumber ) 
       
   225 {
       
   226 	LOG_FUNC_ENTRY( "version.cpp::getDllVersion" );
       
   227 
       
   228 	DWORD dwHandle, dwLen;
       
   229 	UINT BufLen;
       
   230 	LPTSTR lpData;
       
   231 	VS_FIXEDFILEINFO *pFileInfo;
       
   232 	dwLen = GetFileVersionInfoSize( libName, &dwHandle );
       
   233 	if (!dwLen)
       
   234 		return 0;
       
   235 	
       
   236 	lpData = (LPTSTR) malloc (dwLen);
       
   237 	if (!lpData)
       
   238 		return 0;
       
   239 	
       
   240 	if( !GetFileVersionInfo( libName, dwHandle, dwLen, lpData ) )
       
   241 	{
       
   242 		free (lpData);
       
   243 		return 0;
       
   244 	}
       
   245 	
       
   246 	if( VerQueryValue( lpData, _T("\\"), (LPVOID *) &pFileInfo, (PUINT)&BufLen ) )
       
   247 	{
       
   248 		*majorVersion = HIWORD(pFileInfo->dwFileVersionMS);
       
   249 		*minorVersion = LOWORD(pFileInfo->dwFileVersionMS);
       
   250 		*buildNumber = HIWORD(pFileInfo->dwFileVersionLS);
       
   251 		*revisionNumber = LOWORD(pFileInfo->dwFileVersionLS);
       
   252 		return 1;
       
   253 	 }
       
   254 	free (lpData);
       
   255 	return 0;
       
   256 }
       
   257 
       
   258 //EOF