请教GetFileVersioninfo的用法,取文件版本信息的函数

stuwei 2003-09-09 07:32:51
请给一个C++Builder的例子,从文件中取版本信息的
...全文
273 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
SuperSuperLéon 2003-09-11
  • 打赏
  • 举报
回复
使用:
//取工程文件版本
//xuchao 2003-07-10 Created
AnsiString __fastcall TAboutForm::GetVersion()
{
//TODO: Add your source code here
char name[2048];
char block[2048];
LPVOID *translation;
LPVOID *buf;
UINT bytes;
BOOL bRet;
AnsiString Version;

Version = "";

if (GetFileVersionInfo(Application->ExeName.c_str(),0,2048,block) == 0)
{
return Version;
}

// Get the Product Information
bRet=VerQueryValue( block,
"\\VarFileInfo\\Translation",
(LPVOID *)&translation,
(PUINT)&bytes);
if(!bRet)
{
return Version;
}

wsprintf( name,
"\\StringFileInfo\\%04x%04x\\ProductVersion",
LOWORD (* translation),
HIWORD (* translation));

bRet=VerQueryValue(block,name,(LPVOID *)&buf,(PUINT)&bytes);

if (!bRet)
{
return Version;
}
Version = (AnsiString)"产品版本 V" + (AnsiString)((char *)buf);

wsprintf( name,
"\\StringFileInfo\\%04x%04x\\FileVersion",
LOWORD (* translation),
HIWORD (* translation));

bRet=VerQueryValue(block,name,(LPVOID *)&buf,(PUINT)&bytes);

if (!bRet)
{
return Version;
}

Version +=(AnsiString)"; 文件版本 V" + (AnsiString)((char *)buf);

return Version;
}
SuperSuperLéon 2003-09-11
  • 打赏
  • 举报
回复
.h

#pragma comment (lib,"Version.lib")

//--------------------------------------------------------------------------
#define VER_COMMENTS 1L //Comments
#define VER_INTERNALNAME 2L //InternalName
#define VER_PRODUCTNAME 3L //ProductName
#define VER_COMPANYNAME 4L //CompanyName
#define VER_LEGALCOPYRIGHT 5L //LegalCopyright
#define VER_PRODUCTVERSION 6L //ProductVersion
#define VER_FILEDESCRIPTION 7L //FileDescription
#define VER_LEGALTRADEMARKS 8L //LegalTrademarks
#define VER_PRIVATEBUILD 9L //PrivateBuild
#define VER_FILEVERSION 10L //FileVersion
#define VER_ORIGINALFILENAME 11L //OriginalFilename
#define VER_SPECIALBUILD 12L //SpecialBuild


//--------------------------------------------------------------------------

/*++------------------------------------------------------------------------

Function:
GetVerInfo
Requires:
提取版本信息
Parameters:
buff,版本信息缓存
block,版本信息资源
flag,提取的版本信息名称

Return Values:
失败返回NULL,成功返回指向版本信息的缓存指针
Remarks:
block缓存区包含由函数GetFileVersionInfo返回的版本信息资源

--------------------------------------------------------------------------*/

unsigned char *
_stdcall
GetVerInfo(
unsigned char buff[256],
unsigned char block[],
int flag
);
SuperSuperLéon 2003-09-11
  • 打赏
  • 举报
回复
.cpp

unsigned char *
_stdcall
GetVerInfo(
unsigned char buff[256],
unsigned char block[],
int flag
)
{
char name[2048];
LPVOID *translation;
LPVOID *buf;
UINT bytes;
BOOL bRet;

// Get the Product Information
bRet=VerQueryValue( block,
"\\VarFileInfo\\Translation",
(LPVOID *)&translation,
(PUINT)&bytes);
if(!bRet)
{
return NULL;
}

switch(flag)
{
case VER_COMMENTS:
// Get the Comments
wsprintf( name,
"\\StringFileInfo\\%04x%04x\\Comments",
LOWORD (* translation),
HIWORD (* translation));
break;
case VER_INTERNALNAME:
// Get the internal name
wsprintf( name,
"\\StringFileInfo\\%04x%04x\\InternalName",
LOWORD (* translation),
HIWORD (* translation));
break;
case VER_PRODUCTNAME:
// Get the product name
wsprintf( name,
"\\StringFileInfo\\%04x%04x\\ProductName",
LOWORD (* translation),
HIWORD (* translation));
break;
case VER_COMPANYNAME:
// Get the Company name information
wsprintf (name,
"\\StringFileInfo\\%04x%04x\\CompanyName",
LOWORD (* translation),
HIWORD (* translation));
break;
case VER_LEGALCOPYRIGHT:
// Get the file LegalCopyright
wsprintf( name,
"\\StringFileInfo\\%04x%04x\\LegalCopyright",
LOWORD (* translation),
HIWORD (* translation));
break;
case VER_PRODUCTVERSION:
// Get the file ProductVersion
wsprintf( name,
"\\StringFileInfo\\%04x%04x\\ProductVersion",
LOWORD (* translation),
HIWORD (* translation));
break;
case VER_FILEDESCRIPTION:
// Get the file description name
wsprintf( name,
"\\StringFileInfo\\%04x%04x\\FileDescription",
LOWORD (* translation),
HIWORD (* translation));
break;
case VER_LEGALTRADEMARKS:
// Get the file LegalTrademarks
wsprintf( name,
"\\StringFileInfo\\%04x%04x\\LegalTrademarks",
LOWORD (* translation),
HIWORD (* translation));
break;
case VER_PRIVATEBUILD:
// Get the file PrivateBuild
wsprintf( name,
"\\StringFileInfo\\%04x%04x\\PrivateBuild",
LOWORD (* translation),
HIWORD (* translation));
break;
case VER_FILEVERSION:
// Get the file version
wsprintf( name,
"\\StringFileInfo\\%04x%04x\\FileVersion",
LOWORD (* translation),
HIWORD (* translation));
break;
case VER_ORIGINALFILENAME:
// Get the Original file name for this file
wsprintf( name,
"\\StringFileInfo\\%04x%04x\\OriginalFileName",
LOWORD (* translation),
HIWORD (* translation));
break;
case VER_SPECIALBUILD:
// Get the SpecialBuild for this file
wsprintf( name,
"\\StringFileInfo\\%04x%04x\\SpecialBuild",
LOWORD (* translation),
HIWORD (* translation));
break;
default:
return NULL;
}

bRet=VerQueryValue(block,name,(LPVOID *)&buf,(PUINT)&bytes);

if (bRet)
{
lstrcpy((LPSTR)buff,(LPSTR)buf);
return buff;
}

return NULL;

}
ljianq 2003-09-10
  • 打赏
  • 举报
回复
我以前写了个类,需要的话,我发给你。
gloom 2003-09-10
  • 打赏
  • 举报
回复
获得文件版本号

void __fastcall TForm1::Button2Click(TObject *Sender)
{
DWORD dVersionSize;
LPTSTR lFileName="G:\\Swflash.ocx";
DWORD temp=0;
LPDWORD lpSize=&temp;
dVersionSize=GetFileVersionInfoSize(lFileName,lpSize);

VS_FIXEDFILEINFO *lpBuf;
UINT Size;
LPTSTR lpFileName="F:\\software\\Network\\IE\\ie6setup.exe";
char VerInfo[808];
if (GetFileVersionInfo(lpFileName,NULL,dVersionSize,VerInfo))
{
VerQueryValue(VerInfo,
TEXT("\\"), //StringFileInfo\\040904E4\\FileDescription"),
(LPVOID*)&lpBuf,&Size);
Label1->Caption=AnsiString(lpBuf->dwFileVersionMS/65536 );
}
}
CCLIS 2003-09-09
  • 打赏
  • 举报
回复
CB5编程实例中有,书不在手边,不转了:)
binbin 2003-09-09
  • 打赏
  • 举报
回复
ABOUTDLG.C
//THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright 1996 - 1997 Microsoft Corporation. All Rights Reserved.
//
// Module: aboutdlg.c
//
// Overview: contains the windows functions used by the about box
//
// PLATFORMS: Windows NT (only)
//
// FUNCTIONS: About() About box DlgProc
//
//
// Windows Header Files:
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>

// C RunTime Header Files
#include <stdlib.h>

// Local Header Files
#include "statlist.h"
#include "winutils.h"
#include "aboutdlg.h"
//
// FUNCTION: About(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for "About" dialog box
// This version allows greater flexibility over the contents of the 'About' box,
// by pulling out values from the 'Version' resource.
//
// MESSAGES:
//
// WM_INITDIALOG - initialize dialog box
// WM_COMMAND - Input received
//
//
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
static HFONT hfontDlg; // Font for dialog text
static HFONT hFinePrint; // Font for 'fine print' in dialog
DWORD dwVerInfoSize; // Size of version information block
LPSTR lpVersion; // String pointer to 'version' text
DWORD dwVerHnd=0; // An 'ignored' parameter, always '0'
UINT uVersionLen;
WORD wRootLen;
BOOL bRetCode;
int i;
char szFullPath[256];
char szResult[256];
char szGetName[256];
DWORD dwVersion;
char szVersion[40];
DWORD dwResult;

switch (message) {
case WM_INITDIALOG:
// hide window while it's being constructed
ShowWindow (hDlg, SW_HIDE);

// crate and load the fonts to be used for the text
hfontDlg = CreateFont(14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
VARIABLE_PITCH | FF_SWISS, "");
hFinePrint = CreateFont(11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
VARIABLE_PITCH | FF_SWISS, "");

// position the window in the center of the parent
CenterWindow (hDlg, GetWindow (hDlg, GW_OWNER));

// get the .exe path
GetModuleFileName ((HINSTANCE)(GetWindowLong(hDlg, GWL_HINSTANCE)),
szFullPath, sizeof(szFullPath));

// Now lets dive in and pull out the version information:
dwVerInfoSize = GetFileVersionInfoSize(szFullPath, &dwVerHnd);
if (dwVerInfoSize) {
LPSTR lpstrVffInfo;
HANDLE hMem;
hMem = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
lpstrVffInfo = GlobalLock(hMem);
GetFileVersionInfo(szFullPath, dwVerHnd, dwVerInfoSize, lpstrVffInfo);
// The below 'hex' value looks a little confusing, but
// essentially what it is, is the hexidecimal representation
// of a couple different values that represent the language
// and character set that we are wanting string values for.
// 040904E4 is a very common one, because it means:
// US English, Windows MultiLingual characterset
// Or to pull it all apart:
// 04------ = SUBLANG_ENGLISH_USA
// --09---- = LANG_ENGLISH
// --11---- = LANG_JAPANESE
// ----04E4 = 1252 = Codepage for Windows:Multilingual

lstrcpy(szGetName, GetStringRes(IDS_VER_INFO_LANG));

wRootLen = lstrlen(szGetName); // Save this position

// Set the title of the dialog:
lstrcat (szGetName, "ProductName");
bRetCode = VerQueryValue((LPVOID)lpstrVffInfo,
(LPSTR)szGetName,
(LPVOID)&lpVersion,
(UINT *)&uVersionLen);

// Notice order of version and string...
lstrcpy(szResult, "About ");
lstrcat(szResult, lpVersion);

// set dialog caption
SetWindowText (hDlg, szResult);

// Walk through the dialog items that we want to replace:
for (i = DLG_VERFIRST; i <= DLG_VERLAST; i++) {
GetDlgItemText(hDlg, i, szResult, sizeof(szResult));
szGetName[wRootLen] = (char)0;
lstrcat (szGetName, szResult);
uVersionLen = 0;
lpVersion = NULL;
bRetCode = VerQueryValue((LPVOID)lpstrVffInfo,
(LPSTR)szGetName,
(LPVOID)&lpVersion,
(UINT *)&uVersionLen);

if ( bRetCode && uVersionLen && lpVersion) {
// Replace dialog item text with version info
lstrcpy(szResult, lpVersion);
SetDlgItemText(hDlg, i, szResult);
}
else
{
dwResult = GetLastError();

wsprintf(szResult, GetStringRes(IDS_VERSION_ERROR), dwResult);
SetDlgItemText (hDlg, i, szResult);
}
SendMessage (GetDlgItem (hDlg, i), WM_SETFONT,
(UINT)((i==DLG_VERLAST)?hFinePrint:hfontDlg),
TRUE);
} // for (i = DLG_VERFIRST; i <= DLG_VERLAST; i++)

GlobalUnlock(hMem);
GlobalFree(hMem);

} else {
// No version information available.
} // if (dwVerInfoSize)

SendMessage (GetDlgItem (hDlg, IDC_LABEL), WM_SETFONT,
(WPARAM)hfontDlg,(LPARAM)TRUE);

// We are using GetVersion rather then GetVersionEx
// because earlier versions of Windows NT and Win32s
// didn't include GetVersionEx:
dwVersion = GetVersion();

if (dwVersion < 0x80000000) {
// Windows NT
wsprintf (szVersion, "Microsoft Windows NT %u.%u (Build: %u)",
(DWORD)(LOBYTE(LOWORD(dwVersion))),
(DWORD)(HIBYTE(LOWORD(dwVersion))),
(DWORD)(HIWORD(dwVersion)) );
} else if (LOBYTE(LOWORD(dwVersion))<4) {
// Win32s
wsprintf (szVersion, "Microsoft Win32s %u.%u (Build: %u)",
(DWORD)(LOBYTE(LOWORD(dwVersion))),
(DWORD)(HIBYTE(LOWORD(dwVersion))),
(DWORD)(HIWORD(dwVersion) & ~0x8000) );
} else {
// Windows 95
wsprintf (szVersion, "Microsoft Windows 95 %u.%u",
(DWORD)(LOBYTE(LOWORD(dwVersion))),
(DWORD)(HIBYTE(LOWORD(dwVersion))) );
}

SetWindowText (GetDlgItem(hDlg, IDC_OSVERSION), szVersion);

// now display the finished product
ShowWindow (hDlg, SW_SHOW);
return (TRUE);

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
EndDialog(hDlg, TRUE);
DeleteObject (hfontDlg);
DeleteObject (hFinePrint);
return (TRUE);
}
break;
}

return FALSE;
}

1,221

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧