-
FindResource Fails
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 :)
-
That looks okay, providing your resource script has something like:
Code:
IDR_BIN1 RCDATA
{
/*data*/
}
Also have you checked to see if the resource was succesfully compiled and linked (something like resource hacker might be useful in this regard).
edit: This is windows specific so I've moved it.
-
Thanks :)
Sorry for the lame question but how can I check if the script has the
IDR_BIN RCDATA
section? THanks
-
>>Ive attached a binary file to the resource part of my program<<
You did this so I assume you started off with a resource script(*.rc) containing the binary data.
-
Thanks again for the reply.
I see what you mean. The rc file was created for me by the IDE. I used the IDE to attach a binary file as a resource and it created the rc file itself. I've opened up the resource file in notepad and this is what it says:
Code:
//Microsoft Developer Studio generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// English (U.K.) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENG)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_UK
#pragma code_page(1252)
#endif //_WIN32
/////////////////////////////////////////////////////////////////////////////
//
// bin
//
IDR_BIN1 bin DISCARDABLE "MilkDLL.dll"
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE DISCARDABLE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE DISCARDABLE
BEGIN
"#include ""afxres.h""\r\n"
"\0"
END
3 TEXTINCLUDE DISCARDABLE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
#endif // English (U.K.) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
The binary file that I imported as a resource is Milkdll.dll, which can be seen referenced from the above code. Im afraid that I dont know anything and rc coding so I dont understand the above code.
Thanks :)
-
It's not rcdata, it's a user-defined resource, with id IDR_BIN1 and resource type(user-defined) of 'bin'.
>>I want to then find it and extract it to the hard drive<<
What is the nature of your program?
-
oops lol. Thats been driving me mad for days. Thanks a lot for your help mate, it now works perfectly!