Hi all,
I am making an installer program. Ive attached a binary file to the resource part of my program. I want to then find it and extract it to the hard drive. This is the code to extract the resource.
Code:
bool ExtractResource()
{
HRSRC hResource;
HANDLE hFile;
DWORD dwFileSize, dwBytesWritten;
LPBYTE buffer;
hResource = FindResource(NULL, MAKEINTRESOURCE(IDR_BIN1), RT_RCDATA);
if(hResource==NULL){
MessageBox(NULL, "FindResource Failure", "df", MB_OK);
int err = GetLastError();
char buffer[33];
itoa(err,buffer,10);
MessageBox(NULL, buffer, buffer, MB_OK);
}
HGLOBAL loadedRes = LoadResource(GetModuleHandle(NULL), hResource);
if(loadedRes==NULL)
MessageBox(NULL, "LoadResource Failure", "df", MB_OK);
buffer = (LPBYTE) LockResource(loadedRes);
if(buffer==NULL)
MessageBox(NULL, "LockResource Failure", "df", MB_OK);
dwFileSize = SizeofResource(GetModuleHandle(NULL),hResource);
hFile = CreateFile("c:/thedll.dll",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (INVALID_HANDLE_VALUE != hFile)
{
WriteFile(hFile, buffer, dwFileSize, &dwBytesWritten, NULL);
CloseHandle(hFile);
}
FreeResource(loadedRes);
return true;
}
FindResource returns null and the GetLastError returns 6 which is ERROR_INVALID_HANDLE.
Im sure i have had this code working flawlessly in the past but have no idea why it isnt working now. I have a feeling it might be my resource.h file. Does this look correct?
Code:
//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by resource.rc
//
#define IDR_BIN1 101
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
Thanks