FindResource Fails

This is a discussion on FindResource Fails within the Windows Programming forums, part of the Platform Specific Boards category; Hi all, I am making an installer program. Ive attached a binary file to the resource part of my program. ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    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

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    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.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    Thanks

    Sorry for the lame question but how can I check if the script has the

    IDR_BIN RCDATA

    section? THanks

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>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.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    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

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    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?
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    oops lol. Thats been driving me mad for days. Thanks a lot for your help mate, it now works perfectly!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 06-24-2009, 09:49 AM
  2. Mac - File locking with fcntl() fails on shared volumes!?
    By idelovski in forum Linux Programming
    Replies: 3
    Last Post: 11-10-2008, 06:37 PM
  3. Replies: 0
    Last Post: 05-23-2005, 11:39 AM
  4. CreateDialog fails (Using resource but not MFC)
    By skiingwiz in forum Windows Programming
    Replies: 5
    Last Post: 03-03-2005, 05:04 PM
  5. SetCommState fails on WinNT
    By marsface in forum Windows Programming
    Replies: 4
    Last Post: 03-25-2003, 12:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21