lpBytes here is binary data. It is actually the resource content we are passing to the function UpdateResource.
Unfortunately, UpdateResource takes the resource data as parameter, not the attributes like "FileVersion". So we need to send "Version" data content.
There are two ways you can achieve this.
1) Get the fileversion using GetFileVersionInfo from the .exe, update it, make a resource of that content and then update the same to .exe using UpdateResource. This is could be errorneous as we are trying to modify the memory.
2) Second way is, you can get the "Version" resource data from another .exe file and use the same to pass it to the .exe you want to update. By this, you will have the "Version" details of an exe say app1.exe copied to another exe, say app2.exe. This can be done as in the code below,
hExe = LoadLibrary(TEXT("app1.exe"));
if (hExe == NULL)
{
std::wcout << TEXT("Could not load exe.");
return 0;
}
hRes = FindResource(hExe, MAKEINTRESOURCE(1), RT_VERSION);
if (hRes == NULL)
{
std::wcout << TEXT("Could not locate dialog box.");
return 0;
}
hResLoad = LoadResource(hExe, hRes);
if (hResLoad == NULL)
{
std::wcout << TEXT("Could not load dialog box.");
return 0;
}
lpResLock = LockResource(hResLoad);
if (lpResLock == NULL)
{
std::wcout << TEXT("Could not lock dialog box.");
return 0;
}
hUpdateRes = BeginUpdateResource(TEXT("app2.exe"), FALSE);
if (hUpdateRes == NULL)
{
std::wcout << TEXT("Could not open file for writing.");
return 0;
}
result = UpdateResource(hUpdateRes,
RT_VERSION,
MAKEINTRESOURCE(1),
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
lpResLock,
SizeofResource(hExe, hRes));
if (result == FALSE)
{
std::wcout << TEXT("Could not add resource.");
return 0;
}
if (!EndUpdateResource(hUpdateRes, FALSE))
{
std::wcout <<TEXT("Could not write changes to file.");
return 0;
}
if (!FreeLibrary(hExe))
{
std::wcout << TEXT("Could not free executable.");
return 0;
}