Chinaunix首页 | 论坛 | 博客
  • 博客访问: 262265
  • 博文数量: 42
  • 博客积分: 2245
  • 博客等级: 大尉
  • 技术积分: 466
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-20 13:02
文章分类

全部博文(42)

文章存档

2012年(4)

2011年(6)

2010年(2)

2009年(6)

2008年(24)

我的朋友

分类: C/C++

2009-03-30 20:18:05


取得文件版本号:




#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;
}

阅读(1362) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~