Modify the version number information of DLL (Exe) under Windows

The software needs to be updated, and the version number of the DLL or ExE needs to be modified. In many cases, it relies on an IDE like VS2008 to modify and then compile and generate.

The method described below is to modify the binary file by writing the code area to achieve the purpose of modifying the version number.

The main steps are as follows:

1. Get file header information size

2. Get file header information content

3. According to the content of the obtained information, the language information can be obtained through the VerQueryValue function

4. According to the content of the obtained information, through the VerQueryValue function to obtain other information, such as file version information, product version information, etc.

5. Modify the memory content according to the obtained version information

6. Complete resource update through BeginUpdateResource, UpdateResource, EndUpdateResource

------------------------------------------------------------------------------

Here is the main code:

The CVersion class is to divide the input string information into 4 digits. The version.GetVersion (1) in the code is the second digit of the version.

 

 

#include "stdafx.h" 
#include <Windows.h> 
#include "Version.h" 

typedef struct _tagLanguage 
{ 
	WORD wLanguage; 
	WORD wCodePage; 
} tagLanguage, * LPLanguage; 



int _tmain (int argc, _TCHAR * argv []) 
{ 
	if (argc! = 3) 
	{ 
		return 0; 
	} 

	TCHAR * FileName = argv [1]; 
	TCHAR * Version = argv [2]; 

	// CVersion class, convert the version information of the string "1234,456,789,1110" into Number, you can write your own code 
	CVersion version (Version); 

	DWORD dwVerHnd = 0; 
	// First get the size of the entire file version information 
	DWORD dwVerInfoSize = GetFileVersionInfoSize (FileName, & dwVerHnd); 
	// Apply memory according to size 
	TCHAR * VerInfo = new TCHAR [dwVerInfoSize]; 
	// Get file version information , The information is stored in the memory just applied, modify the version information by directly modifying the memory to update the version information at one time
	BOOL res = GetFileVersionInfo (FileName, 0, dwVerInfoSize, VerInfo); 
	if (! Res) 
	{ 
		delete [] VerInfo; 
		return 0; 
	} 

	// First read the language information in the obtained version information, because you need to modify the version information 
	LPLanguage language = NULL; 
	UINT size = 0; 
	VerQueryValue (VerInfo, _T ("\\ VarFileInfo \\ Translation"), (LPVOID *) & language, & size); 
	
	// Read file version informationVS_FIXEDFILEINFO 
	* FixedFileInfo = NULL; 
	VerQueryValue ( VerInfo, _T ("\\"), (LPVOID *) & FixedFileInfo, & size); 
	TCHAR TempBuf [MAX_PATH] = {0}; 
	if (FixedFileInfo) 
	{ 
		// Modify the version number of the file version information, here through the CVersion class Get the numbers "1234", "456", "789", "1110"  
		// here Need to combine high and low
		FixedFileInfo-> dwFileVersionMS = MAKELONG (version.GetVersion (1), version.GetVersion (0));
		FixedFileInfo->dwFileVersionLS = MAKELONG(version.GetVersion(3),version.GetVersion(2));


		FixedFileInfo->dwProductVersionMS = MAKELONG(version.GetVersion(1),version.GetVersion(0));
		FixedFileInfo->dwProductVersionMS = MAKELONG(version.GetVersion(3),version.GetVersion(2));

	}

	//读取StringFileInfo中的信息信息
	TCHAR *ProductVer = NULL;
	TCHAR *FileVer = NULL;
	_stprintf_s(TempBuf,_T("\\StringFileInfo\\%04x%04x\\FileVersion"),language->wLanguage,language->wCodePage);
	VerQueryValue(VerInfo,TempBuf,(LPVOID*)&FileVer,&size);
	_stprintf_s (TempBuf, _T ("\\ StringFileInfo \\% 04x% 04x \\ ProductVersion"), language-> wLanguage, language-> wCodePage); 
	VerQueryValue (VerInfo, TempBuf, (LPVOID *) & ProductVer, & size); 
		// This parameter is correct, It is the most originally read resource, by modifying the original resource memory, to achieve the purpose of updating


	size_t productlength = _tcslen(ProductVer);
	size_t fileLength = _tcslen (FileVer); 
	if (_tcslen (Version)> productlength) 
	{ 
		return 0; 
	} 

	if (_tcslen (Version)> fileLength) 
	{ 
		return 0; 
	} 

	// Modify 
	memory_tcscpy_s (ProductVer, productlength + 1, Version) ; 
	_tcscpy_s (FileVer, fileLength + 1, Version); 

	// The above are read and modified, here is the update resource 
	HANDLE hResource = BeginUpdateResource (FileName, FALSE); 
	if (NULL! = hResource) 
	{ 
		res = UpdateResource (hResource , RT_VERSION, MAKEINTRESOURCE (VS_VERSION_INFO), language-> wLanguage, VerInfo, dwVerInfoSize); 
		if (! Res) 
		{ 
			return 0; 
		} 
		EndUpdateResource (hResource, FALSE); 
	}

	return 0;
}
Published 54 original articles · Like 89 · Visit 680,000+

Guess you like

Origin blog.csdn.net/ayang1986/article/details/103914168