Solved

Using non-Unicode compiled dll with a unicode program

Posted on 2004-10-11
2
264 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

777 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