Home > ATL, MFC > Set resource handle in MFC and ATL

Set resource handle in MFC and ATL

When working in MFC/ATL and then using a resource dll can at times cause bugs because resource handle is not properly set. For e.g. if you have a dialog resource in a dll and you instantiate a CDialog which results in an error because resource cannot be located in this application or dll since it’s in the resource dll. So solution for above problem will be to set correct resource handle.

MFC provides two functions…

  1. AfxGetResourceHandle
  2. AfxSetResourceHandle

pretty obvious names. 🙂

So we should first save our current resource handle and then set new resource handle before loading such a resource. Also don’t forget to set our old handle since its good practice.

Sample code snippet.

extern HINSTANCE hResDll = NULL;
HINSTANCE hDefInstance = AfxGetResourceHandle();
CBitmap Bmp;
Bmp.LoadBitmap( IDB_NIBUS_FACE );


Internally MFC calls FindResource and LoadResource using this handle, so if it’s not properly set this will cause failure in loading resources.

In ATL it’s quite similar except that we’ve got a new function called AddResourceInstance which adds our new resource handle to existing list of handles. So when a look up is done given resource handle is also used. Following functions are provided in ATL to work with resource handles…

  1. AddResourceInstance
  2. GetResourceInstance – Similar to AfxGetResourceHandle
  3. SetResourceInstance – Similar to AfxGetSetResourceHandle

For newbies this is always a painful bug to resolve as they don’t know what went wrong since they expect this to be automagically done.

  1. February 17, 2011 at 8:19 pm

    I’d wrap the MFC calls into a class (ex. CResourceScope(cHandle)). In the constructor, cache the current resource handle and set the new one. In the destructor, revert the change. That way it’s exception safe.

    • February 17, 2011 at 8:37 pm

      That’s perfect arons. I do something similar too. Thank you for posting your idea here. Please note that I’ve my blog to http://www.ntcoder.com/bab.

  2. Softy
    April 22, 2009 at 8:02 pm

    The dialogue is in the same resource, but there is no dllmain.

  3. Softy
    April 22, 2009 at 3:34 am

    I am having this problem in a .dll with a dialogue box when I call doModal(). When AfxGetResourceHandle() is called my handle is NULL. Using your suggestion, how can I get a good instance handle to begin with? In other words if hDefInstance is null as the result of calling AfxGetResourceHandle(), and hResDll stays NULL, then the debug assertion is thrown. How can I overcome this problem?

    • April 22, 2009 at 9:59 am

      Hi Softy,

      So where is this dialog resource? If its in the same dll then store the handle that we get in DllMain. This will be your resource handle. So backup old resource handle (a good practice) before calling DoModal and then set this new handle by calling AfxSetResourceHandle. So when DoModal is done restore old resource handle.

      Does this help?


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: