CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2004
    Location
    Pakistan
    Posts
    466

    Question LockResource might be causing problem

    Hi guys I have a function that extracts resources from application, it goes fine with custom resources, but when it comes to standard resources it extracts files of same size but garbage content in them.

    In my applciation I have embedded two resources of type 'BITMAP', in resource file I defined them as:

    Code:
    117 BITMAP DISCARDABLE "p2/ide_icons/splash.bmp"
    118 BITMAP DISCARDABLE "p2/ide_icons/splash.gif"
    117 and 118 are resource Ids, and BITMAP is type of resource.
    Now if the resource type is something else e.g. 'MYRES' defined in resource file as
    Code:
    117 MYRES DISCARDABLE "p2/ide_icons/splash.bmp"
    it is extracted fine.

    But as you can see it is is of type BITMAP, so I pass RT_BITMAP to function FindResource.


    Problem is all of folowing functions pass fine
    FindResource
    LoadResource
    LockResource

    but resulting extracted data is not actual file that was in the resources, although the size is same but contents are wrong.


    Please review following function and let me know what can be possible problem.

    Code:
    bool ExtractResource(DWORD resID, LPCSTR resType,String file)  { 
    	HRSRC hResInfo = FindResource(GetModuleHandle(NULL), MAKEINTRESOURCE(resID), resType);
        if (hResInfo == NULL) return false;
    	HGLOBAL hResource = LoadResource(GetModuleHandle(NULL), hResInfo);
        if (hResource == NULL) return false;      
        char *pResData = (char*)LockResource(hResource);
        DWORD resSize = SizeofResource(NULL, hResInfo);
       
        if(resSize==0) return false;
    
        FILE *f = fopen(file.c_str(),"w");
        fwrite(pResData,1,resSize,f);
        fclose(f);
        
        UnlockResource(hResource);
    }
    Will be waiting for kind reply.

    regards
    » Please 'Rate This Post' if it helped (encourage us to help you more)
    » Build GUI in minute using rad c++
    » Free IDE + GUI code generator - screenshot
    » Free WINAPI sourcecode and tutorials

  2. #2
    Join Date
    Sep 2004
    Location
    Italy
    Posts
    389

    Re: LockResource might be causing problem

    Use "MYRES" (a string, literally) instead of RT_BITMAP. IMHO RT_BITMAP is just a #define for "BITMAP".

  3. #3
    Join Date
    Nov 2004
    Location
    Pakistan
    Posts
    466

    Smile Re: LockResource might be causing problem

    Sorry your answer is not satisfactory . As I already have stated that these bitmaps included in resources MUST be defined as "BITMAP" nothing else. And I also have stated that above code works with any custom resource type I define. So using "MYRES" is out of requirement. The need is loading the resource from its standard type. Oh I already know that RT_BITMAP is a define and is "BITMAP" that we define in resources file while compiling resources.

    Requirement is:
    1. Define as "BITMAP" in resources
    117 BITMAP DISCARDABLE "p2/ide_icons/splash.bmp"

    2. Load and extract by using same type "BITMAP".
    Which is not working.

    Any answer why does above function fail? where it is fine enough to exract from resources and all functions report properly. But resulting data that is obtained using LockResource is invalid and written to file is not the one in resources.

    regards
    Last edited by Ali Imran; March 25th, 2007 at 09:10 AM.
    » Please 'Rate This Post' if it helped (encourage us to help you more)
    » Build GUI in minute using rad c++
    » Free IDE + GUI code generator - screenshot
    » Free WINAPI sourcecode and tutorials

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: LockResource might be causing problem

    Are you 100% certain that the problem is in the extraction, and not in your file write?????
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Nov 2004
    Location
    Pakistan
    Posts
    466

    Exclamation Re: LockResource might be causing problem

    Somehow, becuase
    Code:
    FILE *f = fopen(file.c_str(),"w");
    fwrite(pResData,1,resSize,f);
    fclose(f);
    is streight C io code, I think it must work, what you say about it?

    I also tried winapi specific filing method, but that writes something incorrect in output file:
    Code:
        HANDLE hFile = CreateFile(file.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
        DWORD dwByteWritten;
        WriteFile(hFile, pResData , resSize , &dwByteWritten , NULL);
        if(dwByteWritten != resSize) return false;
        CloseHandle(hFile);

    waiting for kind reply.
    regards
    » Please 'Rate This Post' if it helped (encourage us to help you more)
    » Build GUI in minute using rad c++
    » Free IDE + GUI code generator - screenshot
    » Free WINAPI sourcecode and tutorials

  6. #6
    Join Date
    Nov 2004
    Location
    Pakistan
    Posts
    466

    Re: LockResource might be causing problem

    Guys you just posted questions and suggestions but I see no solution in your posts.
    Correct the code if there is any bug, atleast I do nto see any.

    Will be waiting for kind response.
    » Please 'Rate This Post' if it helped (encourage us to help you more)
    » Build GUI in minute using rad c++
    » Free IDE + GUI code generator - screenshot
    » Free WINAPI sourcecode and tutorials

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured