Solved

UNICODE usage in MFC Ext DLL

Posted on 1998-10-07
1
571 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now