Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using non-Unicode compiled dll with a unicode program

Posted on 2004-10-11
2
Medium Priority
?
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

636 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