12.3. How do I build an 'extension DLL'?

Microsoft Visual C++/Microsoft Foundation Classes


12.3. How do I build an 'extension DLL'?

  1. When you're building a 32-bit extension DLL, define _AFXEXT on the compiler command line. If you look in AFXVER_.H, you'll see that this forces _AFXDLL to also be defined. So an "AFXEXT" DLL is an AFXDLL.
  2. When _AFXDLL is defined, AfxGetResourceHandle returns a value stored in MFC's global data, which is shared by the EXE, the extension DLL and the MFC DLL. The handle returned identifies the module which will be searched first when looking for a resource.
(See the source code for AfxFindResourceHandle() if you're curious about the order of the search.)
  1. Strictly speaking, what we need to load a resource is a module handle rather than an instance handle. (Instances share modules --- e.g., code and resources --- but have different data.) A DLL has a module handle which is distinct from the handle of the EXE.
  2. You can use ::GetModuleHandle to get the handle for your DLL, then pass it to AfxSetResourceHandle so that your DLL is the first place searched for resources. But note that this removes the EXE module from of modules searched. You'll probably want to save a copy of the handle returned by AfxGetResourceHandle before calling AfxSetResourceHandle, then restore it once you're done loading the DLL resource.

Charlie Kester, Microsoft Developer Support, MSMFC, 7/19/95