#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
string GetFileVersion1(string fName)
{
char version[64]="";
LPDWORD dwHandle=NULL;
DWORD dwSize;
dwSize=GetFileVersionInfoSize(fName.c_str(),dwHandle);
if (dwSize==0) return version;
char * pData=new char[dwSize];
if (!GetFileVersionInfo(fName.c_str(),NULL,dwSize,pData)) return version;
HRESULT hr;
struct LANGANDCODEPAGE
{
WORD wLanguage;
WORD wCodePage;
} *lpTranslate;
// Read the list of languages and code pages.
unsigned int cbTranslate;
VerQueryValue(pData,
("\\VarFileInfo\\Translation"),
(LPVOID*)&lpTranslate,
&cbTranslate);
// Read the file description for each language and code page.
char SubBlock[128];
char * Item;
UINT Size;
for (unsigned int i=0; i < (cbTranslate/sizeof(struct LANGANDCODEPAGE)); i++ )
{
sprintf(SubBlock,
("\\StringFileInfo\\%04x%04x\\ProductVersion"),
lpTranslate[i].wLanguage,
lpTranslate[i].wCodePage);
// Retrieve file description for language and code page "i".
VerQueryValue(pData,
SubBlock,
(LPVOID*)&Item,
&Size);
}
strncpy(version,Item,Size);
version[Size]='\0';
return version;
}
string GetFileVersion(string fName)
{
char version[64]="";
LPDWORD dwHandle=NULL;
DWORD dwSize;
dwSize=GetFileVersionInfoSize(fName.c_str(),dwHandle);
if (dwSize==0) return version;
char * pData=new char[dwSize];
if (!GetFileVersionInfo(fName.c_str(),NULL,dwSize,pData)) return version;
VS_FIXEDFILEINFO *verInfo;
UINT Size=sizeof(verInfo);
VerQueryValue(pData,"\\",(LPVOID*)&verInfo,&Size);
sprintf(version,"%d.%d.%d.%d",
(verInfo->dwProductVersionMS>>16)&0xffff,
verInfo->dwProductVersionMS&0xffff,
(verInfo->dwProductVersionLS>>16)&0xffff,
(verInfo->dwProductVersionLS)&0xffff);
return version;
}
int main(int argc,char *argv[])
{
if(argc<2){
cout<<"FILE Copyright (c) 2009 Moremoresoft.cn\n";
cout<<"Usage:\n";
cout<<"file file_name\n";
//return 1;
}
string ver=GetFileVersion(argv[1]);
//string ver=GetFileVersion("..\\WinTools\\WinTools.exe");
cout<<ver<<endl;
return 0;
}
|