dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Loading a Resource at run-time

  1. #1
    Simon Hudson Guest

    Loading a Resource at run-time

    I'm trying to load a bitmap from the resource table at runtime using
    FindResource(), SizeofResource() and LoadResource() but FindResource always
    returns zero.

    BOOL CSCPic::Load(UINT nIDRes)
    {
    HINSTANCE hInst = AfxGetResourceHandle();
    HRSRC hRsrc = ::FindResource(hInst, MAKEINTRESOURCE(nIDRes), "IMAGE");
    if (!hRsrc)
    return FALSE; // always fails here

    etc...
    }

    The unsigned int passed to this function is the IDB_MYBITMAP resource
    identifier generated by DevStudio so the usage is simply:-

    if !(pPic->Load("somefile.bmp")) // the above function's preferred
    sibling
    bResult = pPic->Load(IDB_MYBITMAP); // :o(

    Any ideas?


    Simon

    (I'm doing this because, under normal circumstances, the bitmap I want is
    loaded from disk, but if the load fails then this code is called to display
    a backup bitmap resource.)




  2. #2
    Jane Zeikel Guest

    Re: Loading a Resource at run-time

    OOps! I meant to reply here... Doh!

    Is the resource in the same module and are you making any calls from
    another module / non-mfc code?

    From MSDN...
    By default, MFC uses the resource handle of the main application to load the
    resource template. If you have an exported function in a DLL, such as one
    that launches a dialog box in the DLL, this template is actually stored in
    the DLL module. You need to switch the module state for the correct handle
    to be used. You can do this by adding the following code to the beginning of
    the function:
    AFX_MANAGE_STATE(AfxGetStaticModuleState());


    "Simon Hudson" <si@thecomplex.f9.co.uk> wrote in message
    news:3c6ba492@10.1.10.29...
    > I'm trying to load a bitmap from the resource table at runtime using
    > FindResource(), SizeofResource() and LoadResource() but FindResource

    always
    > returns zero.


    Good Luck

    Jane





  3. #3
    Simon Hudson Guest

    Re: Loading a Resource at run-time

    "Jane Zeikel" <nSpmjanzania@yahoo.co.uk> wrote in message
    news:3c6c0b06$1@10.1.10.29...

    > OOps! I meant to reply here... Doh!
    >
    > Is the resource in the same module and are you making any calls from
    > another module / non-mfc code?


    Yes the resource is in the same module (just an AppWizard generated MFC EXE)
    and there are no calls from external sources.

    The HINSTANCE returned by AfxGetResourceHandle() immediately preceding the
    call to FindResource appears to be valid (0x00400000). I don't understand
    what the MAKEINTRESOURCE macro is doing though. Alright, so it's making an
    unsigned int into a resource name, but all it does is cast the UINT into an
    LPSTR, resulting in a pointer with the value (in my case) 0x00000067. Is
    that valid? Surely, if it's accessed directly it will cause an access
    violation. Is it just an offset into the resource table or something?


    >
    > From MSDN...
    > By default, MFC uses the resource handle of the main application to load

    the
    > resource template. If you have an exported function in a DLL, such as one
    > that launches a dialog box in the DLL, this template is actually stored in
    > the DLL module. You need to switch the module state for the correct handle
    > to be used. You can do this by adding the following code to the beginning

    of
    > the function:
    > AFX_MANAGE_STATE(AfxGetStaticModuleState());





  4. #4
    Antony Guest

    Re: Loading a Resource at run-time


    "Simon Hudson" <si@thecomplex.f9.co.uk> wrote:
    >"Jane Zeikel" <nSpmjanzania@yahoo.co.uk> wrote in message
    >news:3c6c0b06$1@10.1.10.29...
    >
    >> OOps! I meant to reply here... Doh!
    >>
    >> Is the resource in the same module and are you making any calls from
    >> another module / non-mfc code?

    >
    >Yes the resource is in the same module (just an AppWizard generated MFC

    EXE)
    >and there are no calls from external sources.
    >
    >The HINSTANCE returned by AfxGetResourceHandle() immediately preceding the
    >call to FindResource appears to be valid (0x00400000). I don't understand
    >what the MAKEINTRESOURCE macro is doing though. Alright, so it's making

    an
    >unsigned int into a resource name, but all it does is cast the UINT into

    an
    >LPSTR, resulting in a pointer with the value (in my case) 0x00000067. Is
    >that valid? Surely, if it's accessed directly it will cause an access
    >violation. Is it just an offset into the resource table or something?
    >


    The MAKEINTRESOURCE macro simply ensures that the high-order word of the
    int is Zero. This tells the function taking the int that it is an int and
    not a long pointer to a string (a custom name/type in the resource table)

    as for this line:
    HRSRC hRsrc = ::FindResource(hInst, MAKEINTRESOURCE(nIDRes), "IMAGE");

    The third param "IMAGE" sugests that you have inserted a custom resource
    type. Check that the branch in your resource viwer is also in Quotes. If
    it is a standard BITMAP then you need to use RT_BITMAP instead.

    Antony


  5. #5
    Simon Hudson Guest

    Re: Loading a Resource at run-time


    "Antony" <antony_briggs@hotmail.com> wrote in message
    news:3c6cd2a3$1@10.1.10.29...


    > The third param "IMAGE" sugests that you have inserted a custom resource
    > type. Check that the branch in your resource viwer is also in Quotes. If
    > it is a standard BITMAP then you need to use RT_BITMAP instead.


    That was it, RT_BITMAP worked a treat. Thanks very much.

    Unfortunately, it looks like OleLoadPicture() won't load the 16 colour
    standard bitmap that is stored in the resources. Is this the case? If so,
    I'll have to resort to the old LoadBitmap(IDB_...) code when required.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center