PE file for reading and writing
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
void UpdateVersionResource(const char* filePath) {
// Open the PE file for reading and writing
HANDLE hFile = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
printf("Failed to open file: %d\n", GetLastError());
return;
}
DWORD fileSize = GetFileSize(hFile, NULL);
if (fileSize == INVALID_FILE_SIZE) {
printf("Failed to get file size: %d\n", GetLastError());
CloseHandle(hFile);
return;
}
// Map the file into memory
HANDLE hMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
if (!hMapping) {
printf("Failed to create file mapping: %d\n", GetLastError());
CloseHandle(hFile);
return;
}
LPVOID pBase = MapViewOfFile(hMapping, FILE_MAP_WRITE, 0, 0, 0);
if (!pBase) {
printf("Failed to map view of file: %d\n", GetLastError());
CloseHandle(hMapping);
CloseHandle(hFile);
return;
}
// Locate the resource section (this example assumes standard PE structure)
PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)pBase;
PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)((BYTE*)pBase + dosHeader->e_lfanew);
PIMAGE_RESOURCE_DIRECTORY resourceDirectory = (PIMAGE_RESOURCE_DIRECTORY)((BYTE*)pBase + ntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress);
PIMAGE_RESOURCE_DIRECTORY_ENTRY resourceEntry = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)((BYTE*)resourceDirectory + sizeof(IMAGE_RESOURCE_DIRECTORY));
// Search for RT_VERSION resource
PIMAGE_RESOURCE_DATA_ENTRY resourceData = NULL;
for (int i = 0; i < resourceDirectory->NumberOfIdEntries; i++) {
if (resourceEntry[i].Id == RT_VERSION) {
resourceData = (PIMAGE_RESOURCE_DATA_ENTRY)((BYTE*)pBase + resourceEntry[i].OffsetToData);
break;
}
}
if (!resourceData) {
printf("RT_VERSION resource not found!\n");
UnmapViewOfFile(pBase);
CloseHandle(hMapping);
CloseHandle(hFile);
return;
}
// Modify the version resource in memory
VS_FIXEDFILEINFO* pFileInfo = (VS_FIXEDFILEINFO*)((BYTE*)pBase + resourceData->OffsetToData);
// Modify the version fields (e.g., changing FileVersion)
pFileInfo->dwFileVersionMS = MAKELONG(1, 1); // Major version: 1, Minor version: 1
pFileInfo->dwFileVersionLS = MAKELONG(0, 0); // Build version: 0, Revision: 0
pFileInfo->dwProductVersionMS = MAKELONG(1, 2); // Product version 1.2.0.0
pFileInfo->dwProductVersionLS = MAKELONG(0, 0);
// Flush changes back to file
FlushViewOfFile(pBase, fileSize);
// Clean up
UnmapViewOfFile(pBase);
CloseHandle(hMapping);
CloseHandle(hFile);
printf("Version resource updated successfully.\n");
}
int main() {
const char* filePath = "C:\\path\\to\\your\\application.exe"; // Change this to the path of your executable
UpdateVersionResource(filePath);
return 0;
}