Solved

Using non-Unicode compiled dll with a unicode program

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

747 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

14 Experts available now in Live!

Get 1:1 Help Now