Re: passing a string to a dll




"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


.



Relevant Pages

  • Re: D6 and COM
    ... descriptions of the IDL, right? ... Dolphin discovered in the DLL are not? ... Unicode strings. ...
    (comp.lang.smalltalk.dolphin)
  • Re: C++ Version 6 app using c++ version 8 dll
    ... that your VC6 app is an ANSI build, but your VC8 DLL is a Unicode build. ... Does the project have to be converted to Unicode, ... The problem I am having is related to STRINGS. ... generated linker errors. ...
    (microsoft.public.vc.mfc)
  • Re: Call C DLL from VB.net-problem..array gains 4 bytes!
    ... No, Unicode strings don't, by definition, have a header with the size. ... you're writing code in C++, for example, the only difference between an ANSI ... which take an array of bytes. ... > Remember the DLL declaration... ...
    (microsoft.public.windowsce.app.development)
  • Re: How to check variables for uniqueness ?
    ... characters is the sequence SS. ... is simply capitalizing strings. ... The fact that case mapping in English /is/ simple is neither here not ... That is a fair criticism of the Unicode position. ...
    (comp.lang.java.programmer)
  • Re: Dangerous behavior of CString
    ... If I'm reading a data file or serial port or something, if the raw data are multibyte but the compilation is Unicode or vice-versa, then sometimes the converting constructors in CString are convenient. ... I did not actually write code like this; in fact I was pretty careful always to use the _T macro with any literal strings. ... But it does the conversion using the current 8-bit code page, which is not what I want. ...
    (microsoft.public.vc.mfc)

Loading