GetFileVersionInfo function (Windows)

Switch View :
ScriptFree
GetFileVersionInfo function

Retrieves version information for the specified file.

Syntax

BOOL WINAPI GetFileVersionInfo(
  __in        LPCTSTR lptstrFilename,
  __reserved  DWORD dwHandle,
  __in        DWORD dwLen,
  __out       LPVOID lpData
);

Parameters

lptstrFilename [in]

Type: LPCTSTR

The name of the file. If a full path is not specified, the function uses the search sequence specified by the LoadLibrary function.

dwHandle

Type: DWORD

This parameter is ignored.

dwLen [in]

Type: DWORD

The size, in bytes, of the buffer pointed to by the lpData parameter.

Call the GetFileVersionInfoSize function first to determine the size, in bytes, of a file's version information. The dwLen member should be equal to or greater than that value.

If the buffer pointed to by lpData is not large enough, the function truncates the file's version information to the size of the buffer.

lpData [out]

Type: LPVOID

Pointer to a buffer that receives the file-version information.

You can use this value in a subsequent call to the VerQueryValue function to retrieve data from the buffer.

Return value

Type: BOOL

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

File version info has fixed and non-fixed part. The fixed part contains information like version number. The non-fixed part contains things like strings. In the past GetFileVersionInfo was taking version information from the binary (exe/dll). Currently, it is querying fixed version from language neutral file (exe/dll) and the non-fixed part from mui file, merges them and returns to the user. If the given binary does not have a mui file then behavior is as in previous version.

Call the GetFileVersionInfoSize function before calling the GetFileVersionInfo function. To retrieve information from the file-version information buffer, use the VerQueryValue function.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winver.h (include Windows.h)

Library

Version.lib

DLL

Version.dll

Unicode and ANSI names

GetFileVersionInfoW (Unicode) and GetFileVersionInfoA (ANSI)

See also

Reference
GetFileVersionInfoSize
VerQueryValue
VS_VERSIONINFO
Conceptual
Version Information

 

 

Send comments about this topic to Microsoft

Build date: 9/7/2011

Community Content

Anonymous7957
Nice code below.It actually worked like a charm.Small completation...
To get the full file path of the current application use something like this:
WORD MajorVersion        =0; 
WORD MinorVersion        =0;
WORD BuildNumber        =0;
WORD RevisionNumber        =0;

TCHAR fileName[4096];
GetModuleFileName(NULL,fileName,4096);
//theApp.m_pszExeName is no good

GetAppVersion(fileName,
              &MajorVersion,   
              &MinorVersion,   
              &BuildNumber,   
              &RevisionNumber);

m_strVersion.Format (_T("%s, Version:%hu.%hu.%hu.%hu"),theApp.m_pszAppName,MajorVersion,MinorVersion,BuildNumber,RevisionNumber);
CWnd* pWndAppStatic= GetDlgItem (IDC_STATIC_VERSION);
if (pWndAppStatic)
pWndAppStatic->SetWindowText (m_strVersion);
Also, for the GetAppVersion() function to work, you need to add
     #pragma comment(lib,"Version.lib")
to the beginning of the .cpp file.
VS2008, MFC feature pack, 32-bit app.IDC_STATIC_VERSION is the ID of one static control in the About Dialog.

SkyLined_
Function doesn't work correctly on Windows Vista, Server 2008 and 7
Please read this kb article for details about the problem and how to fix it: http://support.microsoft.com/kb/959441.

Karel Zikmund
Sample function to return version info for an app or dll

Here is a sample function to return version info for an app or dll. Adapated from code found at:

http://gnuwin32.sourceforge.net/version.c.txt

BOOL GetAppVersion( char *LibName, WORD *MajorVersion, WORD *MinorVersion, WORD *BuildNumber, WORD *RevisionNumber )
{
DWORD dwHandle, dwLen;
UINT BufLen;
LPTSTR lpData;
VS_FIXEDFILEINFO *pFileInfo;
dwLen = GetFileVersionInfoSize( LibName, &dwHandle );
if (!dwLen)
return FALSE;

lpData = (LPTSTR) malloc (dwLen);
if (!lpData)
return FALSE;

if( !GetFileVersionInfo( LibName, dwHandle, dwLen, lpData ) )
{
free (lpData);
return FALSE;
}
if( VerQueryValue( lpData, "\\", (LPVOID *) &pFileInfo, (PUINT)&BufLen ) )
{
*MajorVersion = HIWORD(pFileInfo->dwFileVersionMS);
*MinorVersion = LOWORD(pFileInfo->dwFileVersionMS);
*BuildNumber = HIWORD(pFileInfo->dwFileVersionLS);
*RevisionNumber = LOWORD(pFileInfo->dwFileVersionLS);
free (lpData);
return TRUE;
}
free (lpData);
return FALSE;
}

Thomas Lee
not always correct value returned
on some files GetFileVersionInfo does not return 0. Pointer is filled with data, but the next call to VerQueryValue leads to application crash.
To avoid this situation always call GetLastError after GetFileVersionInfo ( even if it succeeded )

Topochicho
sample function to return version info for an app or dll (compile bug)

The LPVOID cast in VerQueryValue call needs to be a LPVOID * cast instead.

if( VerQueryValue( lpData, "\\", (LPVOID *) &pFileInfo, (PUINT)&BufLen ) )