Using Dialog Controls in a DLL

You can use a dialog box that is defined in a DLL. To do so, you must inform the dialog routines that the dialog resource is located in the DLL, rather than in the main application. The dialog routines will look for the dialog resource in the main application by default.

To do this, initialize your dialog box using DlgInitWithResourceHandle rather than DlgInit. As compared to DlgInit, DlgInitWithResourceHandle takes an additional argument named "hinst". The "hinst" argument is the module instance handle in which the dialog resource can be found. For a DLL, this handle is passed into the DLL entry point, DllMain.

An example of a DllMain function follows:

  module dll_globals
     integer(4) ghInst    ! DLL instance handle
  end module dll_globals

  !********************************************************************
  !*   FUNCTION: DllMain(HANDLE, DWORD, LPVOID)
  !*
  !*   PURPOSE:  DllMain is called by Windows when
  !*     the DLL is initialized, Thread Attached, and other times.
  !*     Refer to SDK documentation, as to the different ways this
  !*     may be called.
  !*
  !*     The DllMain function should perform additional initialization
  !*     tasks required by the DLL.  DllMain should return a value of 1
  !*     if the initialization is successful.
  !*
  !*********************************************************************

  integer(4) function DllMain (hInst, ul_reason_being_called, lpReserved)
  !DEC$ IF DEFINED(_X86_)
  !DEC$ ATTRIBUTES STDCALL, ALIAS : '_DllMain@12' :: DllMain
  !DEC$ ELSE
  !DEC$ ATTRIBUTES STDCALL, ALIAS : 'DllMain' :: DllMain
  !DEC$ ENDIF

     use dll_globals

     integer(4) hInst
     integer(4) ul_reason_being_called
     integer(4) lpReserved

     !  Save the module instance handle in a global variable
     !  This would typically be in a Module or a COMMON block.
     ghInst =  hInst

     DllMain = 1
     return
  end

One way to use DlgInitWithResourceHandle is to build a resource-only DLL. A resource-only DLL contains an .RC file, but no code. It is useful for building an application that supports multiple languages. You can create a main application and several resource-only DLLs (one for each language) and call the Win32 LoadLibrary routine at the beginning of your application to load the appropriate resource-only DLL. To use a dialog box from the resource-only DLL, first call LoadLibrary (see the Platform SDK online documentation) to return the instance handle that you can use when you call DlgInitWithResourceHandle.

When you create a Fortran DLL project, you can create a resource-only DLL using the Fortran Dynamic Link Library AppWizard:

  1. In the Fortran Dynamic Link Library AppWizard, select "An empty DLL application."
  2. Complete creating the project.
  3. In the Project menu, select Add to Project...Files to add your .RC file and the RESOURCE.H file that defines the identifiers of the controls.
  4. In the Project menu: