Export dialogs in MFC Extension DLLs

It seems to be quite easy to export dialogs from mfc-extension dll's. Just export the corresponding class with AFX_EXT_CLASS and your done. If you do so with an application and a dll you create from scratch you probably even will succeed. But if you insert more and more resources in both the application and the dll, you will get some serious bugs. Here is the reason:

The regular way to identify a specific resource is its ID. This is an integer constant defined by the resource editor. Now say you have got a resource (which is a string) called ID_MY_TEXT.

  
	CString strText;
	strText.LoadString( ID_MY_TEXT );
	afxDump << strText;

Code like this prints the resource-string into the debug window. Sometimes you may get a wrong text and this happens only if the text is in a mfc-extension-dll (mfc-ext-dll). The reason for this error lies in the way the application gets a resource. Since both the application and the dll can have a resource file, IDs can be the same for different resources. (Which is very likely because the VC resource editor starts numbering the IDs at a certain value for each module.)

As you may guess the order the application searches for a resource is first in your application and afterwards in your dll(s) (and finally in the mfc-resources). We need to change the search order for resources.

There is another article on this site which deals with dialog exports from DLL's. But that (as far as I have tested) works only in regular-mfc-dlls.

I wrote a class that (with a little change in the dll main source file and the dialog) will enable you to freely call your dialog from wherever you want. Like:

  
CMyApp::OnDLLDialog()
{
    CDLLDialog dlg;
    dlg.DoModal();
}

As you see there is no need for an export function (which would not be very OO).

I wrote a simple class that sets the resource handle of the dll at its construction and sets back the previous handle at its destruction.

Here it is:

/////////////////////////////////////////////////////////////////////////////////////////////
// File ExtDllState.h
//////////////////////////////////////////////////////////////////////////////////////////// 
#ifndef __EXTDLLSTATE_H__
#define __EXTDLLSTATE_H__
 
class CEXTDLLState
{
public:
  CEXTDLLState();
  ~CEXTDLLState();
protected:
  HINSTANCE m_hInstOld;
};
 
#endif
////////////////////////////////////////////////////////////////////////////////////////////
File ExtDllState.cpp
//////////////////////////////////////////////////////////////////////////////////////////// 
CEXTDLLState::CEXTDLLState()
{
  m_hInstOld = AfxGetResourceHandle();
  AfxSetResourceHandle(extensionDLL.hModule);
}
 
CEXTDLLState::~CEXTDLLState()
{
  AfxSetResourceHandle(m_hInstOld);
}
//////////////////////////////////////////////////////////////////////////////////////

Quite short as you may see, but there is a little more to do:

Copy the class texts from above into the files ExtDllState.h and ExtDllState.cpp. Put BOTH files in a public include directory where every project can reach it. In your DLL go to the main source file (which is named after the DLL). Here you find something like this:

static AFX_EXTENSION_MODULE MY_DLL_NAMEDLL = { NULL, NULL };
where MY_DLL_NAME your dll name is (:

Replace this variable name with "extensionDLL". Below this line put the following lines:

 
    #include "EXTDLLState.h"
    #include "ExtDllState.cpp"

Look for occurrences of MY_DLL_NAMEDLL in the rest of the file and replace it with extensionDLL. Occurrences can only happen in this file since the variable is static.

Now if you want to export a Dialog go into the source file of the corresponding class and include EXTDLLState.h again. Override the function DoModal() (Best done with the ClassWizard). You should get something like:

int CMyDLLDlg::DoModal()
{
  // TODO: Add your specialized code here and/or call the base class
  return CDialog::DoModal();
}
replace the TODO line with "CEXTDLLState State;". Now it looks like

int CDLLDlgDlg::DoModal()
{
  CEXTDLLState State;
  return CDialog::DoModal();
}

With this approach you got a comfortable way to export classes that use resources. Be aware that if you need resources in other places too, to define CEXTDLLState before use. (Especially this must be done with modeless dialogs!!!).

You can use CEXTDLLState everywhere you want to access resources in mfc-ext-dlls. You will always get the right thing. I hope this will help some of you, since I had some troubles till I found an easy and nice way to do handle this.



Comments

  • SAdjk hLP zMaT

    Posted by xuNIkIzqJW on 06/05/2013 11:39pm

    Extra resources propecia side effects infertility - 5 mg propecia generic

    Reply
  • wheloltabotly PumeSonee Phobereurce 3039827

    Posted by TizefaTaNaday on 06/03/2013 03:02am

    CersHootrossy jordancoolgreyforsale.holidaygiving.org BizfooffAgold airjordan13retrosize7.holidaygiving.org FuchHauhMat

    Reply
  • Reason why everybody is extremely wrong concerning sneakers and also reasons why you ought to read this review.

    Posted by BobHotgloff on 05/22/2013 01:42pm

    The basic fundamentals behind sneakers that you'll take advantage of commencing today. [url=http://www.shoesjp.biz/new-balance【ニューバランス】-c-670.html]ニューバランス キッズ[/url] Precisely why the whole thing that you've found out about shoes is simply drastically wrong and exactly what you need understand. [url=http://www.shoesjp.biz/nike【ナイキ】-c-634.html]ナイキ[/url] Compact blog post tells you the ins and outs on shoes in addition to those things you want to complete today. [url=http://www.kutujp.biz/]ベルーナ[/url] Creative shoes Book Unearths Tips To Rule The shoes Market [url=http://www.kutujp.biz/アディダス-adidas-c-4.html]adidas[/url] The particular reason why almost anything that you've heard of shoes is almost certainly wrong and what you want to know. [url=http://www.kutujp.biz/アシックス-asics-c-3.html]アシックス シューズ[/url] Incredible plan for the shoes for you to understand immediately. [url=http://www.kutujp.biz/ナイキ-nike-c-13.html]ナイキ ランニングシューズ[/url] Recent website divulges the low down upon shoes and in addition the reasons why you have got to take action this afternoon. [url=http://www.kutujapan.org/]ベルーナ[/url] Great new shoes Guide Tells The Best Ways To Dominate The sneakers World [url=http://www.kutujapan.org/adidas-アディダス-c-74.html]adidas originals[/url] New shoes Guide Illustrates Method To Rule The shoes Scene [url=http://www.kutujapan.org/new-balance-ニューバランス-c-13.html]ニューバランス 574[/url] Whatever the pros won't be thinking about shoes in a manner that this is relevant to you. [url=http://www.kutujapan.org/nike-ナイキ-c-78.html]nike[/url] The reasons why everybody is extremely wrong on sneakers and as a consequence the reason why you really should read this analysis.

    Reply
  • The Secret dominate the nike-scene Is Pretty Simple!

    Posted by Acuddence on 05/04/2013 10:46am

    Creative questions about nike addressed and consequently the reasons why you must absolutely go through every single word of this document.[url=http://www.nikejpgolf.biz/]ナイキゴルフ[/url] Some form of double change on mizuno [url=http://www.nikejpgolf.biz/nike-ゴルフボール-c-23.html]ナイキgolf[/url] Different questions regarding nike answered and as a consequence the reasons you ought to review each and every term of this specific insider report. [url=http://www.nikejpgolf.biz/nike-アイアン-c-1.html]ゴルフ ナイキ[/url] Independent guide uncovers A couple of new stuff for nike that no company is discussing. [url=http://www.nikejpgolf.biz/nike-アイアン-c-1.html]ナイキ[/url] Their nike Corporation Dialogue - Folks who likes nothing triumphs?? [url=http://www.nikejpgolf.biz/nike-ゴルフシューズ-c-15.html]nike air force 1[/url] Items and formation in L . A . -- nike has left without any adios [url=http://www.nikeyasuyi.com/]nike free[/url] Things and manufacturing throughout The philipines : nike has left with no see you later [url=http://www.nikeyasuyi.com/nikeナイキRunning-c-3.html]ナイキランニング[/url] Their mizuno Business organisation Speak - Consequently, who cares about nada profit?!? [url=http://www.nikeyasuyi.com/nikeナイキDunk-c-9.html]nike シューズ[/url] The main mizuno Corporate Speak : Consequently, who loves nada gains all rewards?!? [url=http://www.nikeyasuyi.com/nikeナイキDunk-c-9.html]nike シューズ[/url] mizuno offers completely new life to an old issue-- golden general

    Reply
  • Nike Aura Max+instagram, will you contain the color to step on your feet!

    Posted by madytreathy on 04/24/2013 02:04pm

    Remember in 2008, if not earlier, when Nike launched ahead of the independent shoe color projects, the watchword "Shoot Your Colours", "Nike PhotoiD" layout, [url=http://markwarren.org.uk/property-waet.cfm]air max 90 uk[/url] reply has not been as fervent as expected. Have in mind, 2008 Canon IXUS 80 IS Digital prankster arcade but only 8 million pixels, Nokia, the motorized phone retail is the exclusively leadership, NikeiD was promote to color in the photos as a infrastructure quest of sneakers levy color, although provocative, but does trouble some. Instagram which cause this thing hold up to ridicule and modest, Nike PHOTOiD homeopathic upgrade customization services, recently [url=http://fossilsdirect.co.uk/glossarey.cfm]nike huarache[/url] released a strange plan. That such iD can you implement pictures as instagram account shoe color, little while volunteer Nike Aura Max shoes and Nike Puff Max 1, Nike Feeling Max 90 953 options. Interested in children's shoes, you [url=http://markwarren.org.uk/property-waet.cfm]air max 90 uk[/url] can always conform with each other's official website photoid.Nike.com, in besides to skim through other people's ingenious sweat, or you can hear to upload your own instagram photo, base your own Nike Hauteur Max.

    Reply
  • Assertion failute in Debug build

    Posted by rahul.shukla on 09/09/2008 02:17am

    Hi, 
    I have created an MFC extension DLL, And did the same thing but its giving assertion failure at 
    m_hInstOld = AfxGetResourceHandle();
    
    Its because of ...
    _AFXWIN_INLINE HINSTANCE AFXAPI AfxGetResourceHandle()
    	{ ASSERT(afxCurrentResourceHandle != NULL);
    		return afxCurrentResourceHandle; }
    
    I am not able to find out How the afxCurrentResourceHandle is getting NULL.

    Reply
  • Alternate Approach

    Posted by Atul Arora on 03/08/2004 12:55am

    Hi Andreas,
    
    Good article. I found an alternate approach to do this.
    You dont have to make a new class. Instead,
    
    AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
    
    in the function
     
    int CDLLDlgDlg::DoModal()
    {
      AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
      return CDialog::DoModal();
    }
    
    Call this function to set the module state before 
    initialization and/or to restore the previous module state after cleanup (end of current scope).
    
    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( ));
    
    So, you dont require a new class to manage this.

    Reply
  • Alternate Approach

    Posted by Atul Arora on 03/08/2004 12:52am

    Hi Andreas,
    
    Good article. I found an alternate approach to do this.
    You dont have to make a new class. Instead,
    
    AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
    
    in the function
     
    int CDLLDlgDlg::DoModal()
    {
      AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
      return CDialog::DoModal();
    }
    
    Call this function to set the module state before 
    initialization and/or to restore the previous module state after cleanup (end of current scope).
    
    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( ));
    
    So, you dont require a new class to manage this.

    Reply
  • How to convert pdf file to text file in vb & vc?

    Posted by Legacy on 11/07/2003 12:00am

    Originally posted by: prajakta

    I have a code of pdf2txt but it converts only 1st 3 pages from pdf to txt. can anyone help me how to change the content of that dll, so that i can convert the whole pdf file to the txt.

    Reply
  • Great work

    Posted by Legacy on 08/14/2003 12:00am

    Originally posted by: Aman

    This had really helped me. Thanx!!

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds