Solved

UNICODE usage in MFC Ext DLL

Posted on 1998-10-07
1
579 Views
Last Modified: 2013-11-20
Hi,

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

TestFunc(void)
{
  //Get the current resource handle
  HINSTANCE hInstOld = AfxGetResourceHandle();
  //Set it to the extension DLL's resource handle
  AfxSetResourceHandle(TMyExtDLL.hModule);
            
 //Test dialog : the resource is in the ext DLL
 CMyDlg dlg;
 dlg.DoModal();

 //Restore the old resource handle
 AfxSetResourceHandle(hInstOld);
}

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
Prashanth
0
Comment
Question by:fidodido
1 Comment
 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 total points
ID: 1323061
>> 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)

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sumDigits challenge 9 170
notReplace  challenge 53 141
FizzBuzz challenge 9 82
Remove Password Protection from pdf (with password) using an command line executable 5 62
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question