UNICODE usage in MFC Ext DLL


I have problem with UNICODE usage. The following is the scenarion. Its a bit lengthy but I have tried to put the question as clearly as possible.
I have a main application which allows me to load any DLL at runtime and call any exported function from the loaded DLL. The following thing should be noted.

1. Main application is build in non-UNICODE mode
2. The DLL that I am loading (the one giving the problem) is an MFC extension DLL and build in UNICODE mode. It has its own dialog resource and an exported funtion void TestFunc(void) coded as follows

  //Get the current resource handle
  HINSTANCE hInstOld = AfxGetResourceHandle();
  //Set it to the extension DLL's resource handle
 //Test dialog : the resource is in the ext DLL
 CMyDlg dlg;

 //Restore the old resource handle

Now the problem:
1. I load the extension DLL in my application - Works Fine.
2. I call the exported funtion TestFunc - Fails.

The application crashes at this point. I have noted that AfxGetReourceHandle() & AfxGetCurrentInstance() fails which leads to the final crash.

But when my extension DLL is built in non-UNICODE mode, everything works fine !

What could be the problem ? Can't I mix unicode & non-unicode app & DLL like this ?

Thanks for your time
Who is Participating?
Answers2000Connect With a Mentor Commented:
>> What could be the problem ? Can't I mix unicode & non-unicode app & DLL like this ?

1. You CAN mix Unicode and non-Unicode code in an app, but NOT in the way you are attempting to do.
2. The problem is that an MFC extension DLL is supposed to share the same copy of MFC as the EXE which it extends, BUT MFC is compiled differently for Unicode and non-Unicode (i.e. there are different MFC DLLs for Unicode and non-Unicode).  If you think about say the CString class, even for a moment, it should be obvious that a Unicode capable CString is different from a 8bit character set CString.

Here are some possible work rounds assuming you Code you app in MFC as non-Unicode

1. Create your DLL as Unicode, but do not use MFC.   Do not share MFC objects with the host app, instead only share Windows objects (e.g HWNDs instead of CWnds, etc).  In the DLL simply use API calls

2. Make your DLL into an OCX using Unicode.  An OCX has its own copy of MFC (you can't share MFC objects between app and the OCX)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.