Re: passing a string to a dll
- From: "Giovanni Dicanio" <giovanni.dicanio@xxxxxxxxxx>
- Date: Tue, 18 Sep 2007 10:17:02 +0200
"SteveR" <srussell@xxxxxxxxxxxxxxxxxxxxxx> ha scritto nel messaggio
news:O0bV%23OZ%23HHA.1212@xxxxxxxxxxxxxxxxxxxxxxx
I am working with the first dll I've ever created. I am testing with this
function:
bool DLLRect::PullWhisker(CString s)
{
if(s != _T("12345") )
::Beep(1000,200);
return true;
}
In my view:
DLLRect r;
r.PullWhisker(_T("12345") );
Why am I getting that beep, i.e. why is the string not recognized?
As others have written, it may be an error about ANSI/Unicode mismatch.
But with modern CString class (which is a short-cut typedef for some more
complicated template-based CStringT, which has traits, too, and these traits
can be MFC traits and different ATL traits...), I would avoid to use CString
at DLL boundaries.
The interface for the DLL exported function could be like so:
// Don't pass CString, just pass a pointer to chars
bool Test( const TCHAR * s )
{
// Build CString as helper inside
CString str( s );
// Use CString instance
if ( str != _T("12345") )
...
}
Moreover, in a DLL, I would also avoid TCHARs in the DLL interface (there
may be exceptions, it depends on your particular project, too).
In fact, if you would like to do a TCHAR-based DLL, you should do like
Windows Win32 SDK headers, i.e. export a pair of functions from the DLL, one
in ANSI mode, the other in Unicode mode, and use a preprocessor #define
based on _UNICODE flag, e.g.
<code>
// ANSI version
bool TestA( const char * s );
// Unicode version
bool TestW( const wchar_t * s );
#ifdef _UNICODE
#define Test TestW
#else
#define Test TestA
#endif // _UNICODE
</code>
So the DLL user can assume that there is a Test( LPCTSTR s ), but there are
actually two different functions exported from the DLL, and the C
preprocessor expands Test as TestA/W basing on _UNICODE flag.
Frankly speaking, in these days, I would forget about ANSI and use only
Unicode for strings.
I think that in these days those ANSI strings are something from the
computer-prehistory :)
Also consider that in Vista, there are Unicode-only versions for several new
APIs.
And even if you use ANSI strings, these are converted in Unicode internally
by Windows (so, if you use ANSI, you also pay performance hits for ANSI -->
Unicode conversion).
ANSI strings should be deprecated.
I would write the DLL interface just like so:
// Just Unicode strings
bool Test( const wchar_ t * s );
If the caller want Unicode/ANSI behaviour, he can use some conversion helper
like CT2CW, e.g.
<code>
TCHAR someTString[ ... ];
// Convert from generic T to Unicode
CT2CW newString( someTString );
// Call the DLL
Test( newString );
</code>
Giovanni
.
- References:
- passing a string to a dll
- From: SteveR
- passing a string to a dll
- Prev by Date: Re: OTOH, you folks are smart ...
- Next by Date: Re: How will I print from the begining of the page?
- Previous by thread: Re: passing a string to a dll
- Next by thread: Re: passing a string to a dll
- Index(es):
Relevant Pages
|
Loading