还在苦苦敲代码开发APP?你out啦! 试试积木搭建APP吧~

c++获取windows程序的版本号

来源:清泛原创     2016-04-08 12:11:59    人气:     我有话说( 0 人参与)

c++获取windows程序的版本号的完整代码。

#include "stdafx.h"
#include <windows.h>
#include <atlstr.h>
#pragma comment(lib, "version")

int _tmain(int argc, _TCHAR* argv[])
{
	LPCTSTR lpszModuleName = _T("C:\\Windows\\notepad.exe");

    // Get the version information size for allocate the buffer
    DWORD dwHandle;
    DWORD dwDataSize = ::GetFileVersionInfoSize((LPTSTR)lpszModuleName, &dwHandle); 
    if ( dwDataSize == 0 ) 
        return FALSE;

    // Allocate buffer and retrieve version information
    LPBYTE lpVersionData = new BYTE[dwDataSize]; 
    if (!::GetFileVersionInfo((LPTSTR)lpszModuleName, dwHandle, dwDataSize, 
	                          (void**)lpVersionData) )
    {
        delete[] lpVersionData;
        return FALSE;
    }

    // Retrieve the first language and character-set identifier
    UINT nQuerySize;
    DWORD* pTransTable;
    if (!::VerQueryValue(lpVersionData, _T("\\VarFileInfo\\Translation"),
                         (void **)&pTransTable, &nQuerySize) )
    {
        delete[] lpVersionData;
        return FALSE;
    }

    // Swap the words to have lang-charset in the correct format
    DWORD dwLangCharset = MAKELONG(HIWORD(pTransTable[0]), LOWORD(pTransTable[0]));

    // Query version information value
    LPVOID lpData;
    CString strValue, strBlockName;
    strBlockName.Format(_T("\\StringFileInfo\\%08lx\\%s"), 
	                     dwLangCharset, _T("FileVersion"));
    if ( ::VerQueryValue((void **)lpVersionData, strBlockName.GetBuffer(0), 
	                     &lpData, &nQuerySize) )
        strValue = (LPCTSTR)lpData;

    strBlockName.ReleaseBuffer();

	printf("%S 版本号:%S\n", lpszModuleName, strValue);

	return 0;
}


工程代码点此下载

c++ 程序 版本号

注:本文为本站或本站会员原创优质内容,版权属于原作者及清泛网所有,
欢迎转载,转载时须注明版权并添加来源链接,谢谢合作! (编辑:admin)
分享到: