EDN Admin
Well-known member
Hi,
Saw quite a few threads on UpdateResource(), but i still could not figure out how to solve the issue am running into. I had earlier taken help from this forum for finding Resources, and it was very helpful. i have a EXE, which contains Custom "BIN" Resources,
3 files to be specific, and one of them is a ZIP file. i want to REPLACE the existing Zip file in the EXE, with a new ZIP file, using UpdateResource(). the code below, is not replacing the resource but actually adding to it. i want IDR_BIN4 to be replaced
with a new ZIP file. the original EXE size is 8,455,168 and the Zip file resource inside that is 8,105,498. after running this code, the EXE size is 16,531,968 bytes. as i have commented in the code, i thought i will then delete the resource
by passing {NULL,0} to the resource and size, but the API fails with code 87, invalid parameter. if i pass TRUE to BeginUpdateResource(), i am worried that it will delete the other 2 resources in the EXE. Would appreciate any help in this regard.
Code:
// DupUpdate.cpp : Defines the entry point for the console application.<br/>
//<br/>
<br/>
#include "stdafx.h"<br/>
#include "resource.h"<br/>
#include "windows.h"<br/>
<br/>
#include <string><br/>
<br/>
// same value as defined for the zip resource in the exe to be loaded<br/>
#define IDR_BIN4 104<br/>
<br/>
int _tmain(int argc, _TCHAR* argv[])<br/>
{<br/>
HMODULE ExeFile;<br/>
ExeFile = LoadLibrary("C:\Users\vinodh\DirListFilesProgram\DupSelfExtract.exe");<br/>
<br/>
if (!ExeFile) {<br/>
printf("n Error loading the EXE");<br/>
exit(0);<br/>
} else {<br/>
printf("n Exe successfully loaded");<br/>
}<br/>
<br/>
HRSRC handleZipFile = FindResource(ExeFile, MAKEINTRESOURCE(IDR_BIN4), "BIN");<br/>
if (handleZipFile == NULL) { <br/>
printf("n Could not find zip file in EXE");<br/>
return -1;<br/>
}<br/>
<br/>
DWORD zipFileSize = SizeofResource(ExeFile, handleZipFile);<br/>
if (INVALID_FILE_SIZE != zipFileSize) <br/>
printf("n Size of zip File in EXE = %lu", zipFileSize);<br/>
<br/>
// false will delete all resources i guess, true is appending to the exe !<br/>
HANDLE UpdRsc = BeginUpdateResource("C:\Users\vinodh\DirListFilesProgram\DupSelfExtract.exe",FALSE);<br/>
<br/>
std::string ReadFile("C:\Temp\Aug4thFactReleases\Mojo\Dup.zip");<br/>
FILE* fRead = fopen(ReadFile.c_str(), "r");<br/>
<br/>
if (!fRead) {<br/>
printf("n Failed to read file");<br/>
exit(0);<br/>
}<br/>
<br/>
fseek(fRead, 0L, SEEK_END);<br/>
long Filesize = ftell(fRead);<br/>
fseek(fRead, 0L, SEEK_SET);<br/>
<br/>
/* if i want to delete by passing null,0, it fails */<br/>
if (UpdateResource(UpdRsc,<br/>
"BIN", MAKEINTRESOURCE(IDR_BIN4) ,<br/>
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), <br/>
fRead, Filesize) == FALSE) {<br/>
printf("n Failed to update resource, error = %d", GetLastError());<br/>
exit(0);<br/>
}<br/>
<br/>
if (!FreeLibrary(ExeFile))<br/>
{<br/>
printf("n Failed to free library");<br/>
}<br/>
<br/>
if (!EndUpdateResource(UpdRsc, FALSE)) {<br/>
printf("n Failed to end update resource, error = %d", GetLastError());
<br/>
}<br/>
<br/>
fclose(fRead);<br/>
<br/>
return 0;<br/>
}<br/>
<br/>
<br/>
<hr class="sig -Vinodh
View the full article
Saw quite a few threads on UpdateResource(), but i still could not figure out how to solve the issue am running into. I had earlier taken help from this forum for finding Resources, and it was very helpful. i have a EXE, which contains Custom "BIN" Resources,
3 files to be specific, and one of them is a ZIP file. i want to REPLACE the existing Zip file in the EXE, with a new ZIP file, using UpdateResource(). the code below, is not replacing the resource but actually adding to it. i want IDR_BIN4 to be replaced
with a new ZIP file. the original EXE size is 8,455,168 and the Zip file resource inside that is 8,105,498. after running this code, the EXE size is 16,531,968 bytes. as i have commented in the code, i thought i will then delete the resource
by passing {NULL,0} to the resource and size, but the API fails with code 87, invalid parameter. if i pass TRUE to BeginUpdateResource(), i am worried that it will delete the other 2 resources in the EXE. Would appreciate any help in this regard.
Code:
// DupUpdate.cpp : Defines the entry point for the console application.<br/>
//<br/>
<br/>
#include "stdafx.h"<br/>
#include "resource.h"<br/>
#include "windows.h"<br/>
<br/>
#include <string><br/>
<br/>
// same value as defined for the zip resource in the exe to be loaded<br/>
#define IDR_BIN4 104<br/>
<br/>
int _tmain(int argc, _TCHAR* argv[])<br/>
{<br/>
HMODULE ExeFile;<br/>
ExeFile = LoadLibrary("C:\Users\vinodh\DirListFilesProgram\DupSelfExtract.exe");<br/>
<br/>
if (!ExeFile) {<br/>
printf("n Error loading the EXE");<br/>
exit(0);<br/>
} else {<br/>
printf("n Exe successfully loaded");<br/>
}<br/>
<br/>
HRSRC handleZipFile = FindResource(ExeFile, MAKEINTRESOURCE(IDR_BIN4), "BIN");<br/>
if (handleZipFile == NULL) { <br/>
printf("n Could not find zip file in EXE");<br/>
return -1;<br/>
}<br/>
<br/>
DWORD zipFileSize = SizeofResource(ExeFile, handleZipFile);<br/>
if (INVALID_FILE_SIZE != zipFileSize) <br/>
printf("n Size of zip File in EXE = %lu", zipFileSize);<br/>
<br/>
// false will delete all resources i guess, true is appending to the exe !<br/>
HANDLE UpdRsc = BeginUpdateResource("C:\Users\vinodh\DirListFilesProgram\DupSelfExtract.exe",FALSE);<br/>
<br/>
std::string ReadFile("C:\Temp\Aug4thFactReleases\Mojo\Dup.zip");<br/>
FILE* fRead = fopen(ReadFile.c_str(), "r");<br/>
<br/>
if (!fRead) {<br/>
printf("n Failed to read file");<br/>
exit(0);<br/>
}<br/>
<br/>
fseek(fRead, 0L, SEEK_END);<br/>
long Filesize = ftell(fRead);<br/>
fseek(fRead, 0L, SEEK_SET);<br/>
<br/>
/* if i want to delete by passing null,0, it fails */<br/>
if (UpdateResource(UpdRsc,<br/>
"BIN", MAKEINTRESOURCE(IDR_BIN4) ,<br/>
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), <br/>
fRead, Filesize) == FALSE) {<br/>
printf("n Failed to update resource, error = %d", GetLastError());<br/>
exit(0);<br/>
}<br/>
<br/>
if (!FreeLibrary(ExeFile))<br/>
{<br/>
printf("n Failed to free library");<br/>
}<br/>
<br/>
if (!EndUpdateResource(UpdRsc, FALSE)) {<br/>
printf("n Failed to end update resource, error = %d", GetLastError());
<br/>
}<br/>
<br/>
fclose(fRead);<br/>
<br/>
return 0;<br/>
}<br/>
<br/>
<br/>
<hr class="sig -Vinodh
View the full article