7

I'm writing a DLL and want to be able to switch between the unicode and multibyte setting in MSVC++2010. For example, I use _T("string") and LPCTSTR and WIN32_FIND_DATA instead of the -W and -A versions and so on.

Now I want to have std::strings which change between std::string and std::wstring according to the unicode setting. Is that possible? Otherwise, this will probably end up getting extremely complicated.

1

1 Answer 1

12

Why not do like the Win32 API does: Use wide characters internally, and provide a character-converting facade of DoSomethingA functions which simply convert their input to Unicode.

That said, you could define a tstring type like so:

#ifdef _UNICODE
typedef std::wstring tstring;
#else
typedef std::string tstring;
#endif

or possibly:

typedef std::basic_string<TCHAR> tstring;
Sign up to request clarification or add additional context in comments.

10 Comments

Note that you'll want to do the same for iostream types as well; e.g. instead of using std::fstream, make a typedef for std::basic_fstream<TCHAR> and use that instead.
@Felix : There's no such thing as a 'narrow' BSTR -- BSTR is, by definition, always UTF-16.
BSTR is always 16 bit on Windows: msdn.microsoft.com/en-us/library/ms221069.aspx
If you're using just a regular DLL export, you might be dealing with an ordinary string, not a BSTR - BSTRs are generally passed across COM interfaces. I haven't worked much with VB6 bindings, so I can't say for sure, but that might be the cause of your issues. In any case, BSTR will still be 16-bit even if you build for MBCS.
If you declare a function in VB6 as taking a String argument, then VB6's UTF-16 string will be converted to an "ANSI" string during the call. Declare the argument as ByVal … As Long, and pass the StrPtr, then the pointer to the original UTF-16 data is passed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.