Solved

Using non-Unicode compiled dll with a unicode program

Posted on 2004-10-11
2
269 Views
Last Modified: 2006-11-17
We have a dll compiled in unicode having for example a follwing function header:

void foo(TCHAR* pChar);

the side using the dll 'sees' this as an unsigned short while in actuality it was compiled in the dll as a char. As a result the linker throws an 'unresolved external' cuz it can't find the implementation it's looking for.

We tried hacking this by doing something like the follwing

#ifdef _UNICODE
#undef _UNICODE
void foo(TCHAR* pChar);
#define _UNICODE

so that the app side will think it's a char too, but apperently the TCHAR is evaluated before this (Precompiled headers??) so it still thinks it's an unsigned short. What can we do about this?

Just so I'm clear, we want to be able to send a char* to foo().

cheers,
RL
0
Comment
Question by:RandomLogic
2 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12276477
>>we want to be able to send a char* to foo().

Then, you'll need to use a wrapper for that function, e.g.

void fooA(char* pChar) {

wchar_t* pwsz = new wchar_t [strlen(pChar) + 1];

wsprintfW ( pwsz, L"%S", psz);

foo(pwsz); // call with UNICODE string

delete [] pwsz;
}
0
 
LVL 14

Accepted Solution

by:
wayside earned 250 total points
ID: 12281468
>We have a dll compiled in unicode having for example a follwing function header:...
> ...actuality it was compiled in the dll as a char.
> Just so I'm clear, we want to be able to send a char* to foo().

Well, you contradict yourself because you say the dll is compiled as unicode but you need to pass the dll function a char *.

So I am assuming your calling program is compiled as unicode and the dll is not.

Your hack fails because tchar.h will have already been included by the time you undefine _UNICODE, so the value set for TCHAR is already established. You'd have to undefine _UNICODE before tchar.h is included, this will probably break lots of other stuff in your code.

I see two possible solutions:

1) change the header file to define the functions as taking char *, thus removing the ambiguity

2) have a separate header file with the correct type, one for the dll and one for the calling program

TCHAR itself is a typedef, I don't think you can change it or you get compile errors. So you can't do much with that.

If you want to have only one header file, you could do something heinous like

#ifdef COMPILING_IN_DLL
void foo(TCHAR *pChar);
#else
void foo(char *pChar);
#endif

at least this way the definitions are all in one place, you'd be less likely to miss something if you needed to change the definition.

I think the best bet is #1 above; if you really need a char *, why use TCHAR?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

829 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