Solved

UNICODE usage in MFC Ext DLL

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pressing cancel button with C# UI Automation 3 52
Define unique primary key 9 89
Window placement 17 75
SQUD PROXY SERVER, UNIX, SLL/HTTPS 5 76
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

867 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

22 Experts available now in Live!

Get 1:1 Help Now