C++ 獲取檔案版本號的程式碼
阿新 • • 發佈:2019-01-03
// Test.cpp : 定義控制檯應用程式的入口點。
//
#include "stdafx.h"
#include <Windows.h>
#include <tchar.h>
#include <string>
#include <iostream>
#pragma comment(lib,"version.lib")
using namespace std;
string GetFileVersion(PCSTR filename)
{
string asVer = "";
VS_FIXEDFILEINFO *pVsInfo;
unsigned int iFileInfoSize = sizeof(VS_FIXEDFILEINFO);
int iVerInfoSize = GetFileVersionInfoSizeA(filename, NULL);
if(iVerInfoSize != 0)
{
char *pBuf = NULL;
while(!pBuf)
{
pBuf = new char[iVerInfoSize];
}
if(GetFileVersionInfoA(filename, 0, iVerInfoSize, pBuf))
{
if(VerQueryValueA(pBuf, "\\", (void **)&pVsInfo, &iFileInfoSize))
{
sprintf(pBuf, "%d.%d.%d.%d", HIWORD(pVsInfo->dwFileVersionMS), LOWORD(pVsInfo->dwFileVersionMS), HIWORD(pVsInfo->dwFileVersionLS), LOWORD(pVsInfo->dwFileVersionLS));
asVer = pBuf;
}
}
delete pBuf;
}
return asVer;
}
int _tmain(int argc, _TCHAR* argv[])
{
string strFilePath = "C:\\Users\\jhon am\\Desktop\\Backupper.exe";
string version = GetFileVersion(strFilePath.c_str());
getchar();
return 0;
}
//
#include "stdafx.h"
#include <Windows.h>
#include <tchar.h>
#include <string>
#include <iostream>
#pragma comment(lib,"version.lib")
using namespace std;
string GetFileVersion(PCSTR filename)
{
string asVer = "";
VS_FIXEDFILEINFO *pVsInfo;
unsigned int iFileInfoSize = sizeof(VS_FIXEDFILEINFO);
int iVerInfoSize = GetFileVersionInfoSizeA(filename, NULL);
if(iVerInfoSize != 0)
{
char *pBuf = NULL;
while(!pBuf)
{
pBuf = new char[iVerInfoSize];
}
if(GetFileVersionInfoA(filename, 0, iVerInfoSize, pBuf))
{
if(VerQueryValueA(pBuf, "\\", (void **)&pVsInfo, &iFileInfoSize))
{
sprintf(pBuf, "%d.%d.%d.%d", HIWORD(pVsInfo->dwFileVersionMS), LOWORD(pVsInfo->dwFileVersionMS), HIWORD(pVsInfo->dwFileVersionLS), LOWORD(pVsInfo->dwFileVersionLS));
asVer = pBuf;
}
}
delete pBuf;
}
return asVer;
}
int _tmain(int argc, _TCHAR* argv[])
{
string strFilePath = "C:\\Users\\jhon am\\Desktop\\Backupper.exe";
string version = GetFileVersion(strFilePath.c_str());
getchar();
return 0;
}