vc 获取程序版本号,当前程序路径,文件修改时间 等

来源:互联网 发布:艾薇软件5.2 编辑:程序博客网 时间:2024/06/05 07:26

获取版本:

 

[cpp:nogutter] view plaincopy
  1. CString GetFileVersion(char*  FileName)  
  2. {     
  3.     int   iVerInfoSize;  
  4.     char   *pBuf;  
  5.     CString   asVer="";  
  6.     VS_FIXEDFILEINFO   *pVsInfo;  
  7.     unsigned   int   iFileInfoSize   =   sizeof(   VS_FIXEDFILEINFO   );  
  8.       
  9.       
  10.     iVerInfoSize   =   GetFileVersionInfoSize(FileName,NULL);   
  11.       
  12.     if(iVerInfoSize!= 0)  
  13.     {     
  14.         pBuf   =   new   char[iVerInfoSize];  
  15.         if(GetFileVersionInfo(FileName,0,   iVerInfoSize,   pBuf   )   )     
  16.         {     
  17.             if(VerQueryValue(pBuf,   "//",(void   **)&pVsInfo,&iFileInfoSize))     
  18.             {     
  19.                 asVer.Format("%d.%d.%d.%d",HIWORD(pVsInfo->dwFileVersionMS),LOWORD(pVsInfo->dwFileVersionMS),HIWORD(pVsInfo->dwFileVersionLS),LOWORD(pVsInfo->dwFileVersionLS));  
  20.             }     
  21.         }     
  22.         delete   pBuf;     
  23.     }     
  24.     return   asVer;     
  25. }    

 

获取路径:

[cpp:nogutter] view plaincopy
  1. CString GetCurrentAppDirectory()  
  2. {  
  3.     char szFile[MAX_PATH];  
  4.     char szPath[MAX_PATH];  
  5.     GetModuleFileName(NULL,szFile,MAX_PATH);  
  6.     size_t i;  
  7.     for(i=strlen(szFile)-1; i>0 && szFile[i]!='//'; i--);  
  8.     szFile[i]='/0';  
  9.     strcpy(szPath,szFile);   
  10.     return (szPath);  
  11. }  

 

获取修改时间:

[cpp:nogutter] view plaincopy
  1. CString GetModifyTime(CString appname)  
  2. {  
  3.     WIN32_FIND_DATA ffd ;  
  4.     HANDLE hFind = FindFirstFile(appname,&ffd);  
  5.     SYSTEMTIME stUTC, stLocal;  
  6.     FileTimeToSystemTime(&(ffd.ftLastWriteTime), &stUTC);  
  7.     SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);  
  8.     CString strTmp;  
  9.     strTmp.Format("%d-%d-%d,%d:%d", stLocal.wYear,stLocal.wMonth,stLocal.wDay,stLocal.wHour,stLocal.wMinute);  
  10.     //--      
  11.     return strTmp;  
  12. }  

 转帖:http://blog.csdn.net/lanmanck/article/details/3901590


==============================================================================================

上面那个有问题,下面这个成功:

CString CMFCVLCDlg::GetFileVer(){ char cPath[200],szVersionBuffer[200]; DWORD dwHandle,InfoSize; CString strVersion; ::GetModuleFileName(NULL,cPath,sizeof(cPath)); //首先获得版本信息资源的长度 InfoSize = GetFileVersionInfoSize(cPath,&dwHandle); //将版本信息资源读入缓冲区 if(InfoSize==0) return _T("None VerSion Supprot"); char *InfoBuf = new char[InfoSize]; GetFileVersionInfo(cPath,0,InfoSize,InfoBuf); //获得生成文件使用的代码页及文件版本 unsigned int  cbTranslate = 0; struct LANGANDCODEPAGE {  WORD wLanguage;  WORD wCodePage; } *lpTranslate; VerQueryValue(InfoBuf, TEXT("\\VarFileInfo\\Translation"),(LPVOID*)&lpTranslate,&cbTranslate); // Read the file description for each language and code page. for( int i=0; i < (cbTranslate/sizeof(struct LANGANDCODEPAGE)); i++ ) {  char  SubBlock[200];  wsprintf( SubBlock,            TEXT("\\StringFileInfo\\%04x%04x\\FileVersion"),            lpTranslate[i].wLanguage,            lpTranslate[i].wCodePage);  void *lpBuffer=NULL;  unsigned int dwBytes=0;  VerQueryValue(InfoBuf,   SubBlock,   &lpBuffer,   &dwBytes);  CString strTemp=(char *)lpBuffer;  strVersion+=strTemp;   } return strVersion;}

转帖:http://www.cnblogs.com/wxlzhizu/archive/2009/12/09/1620009.html