Unicode LPCTSTR Problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Unicode LPCTSTR Problem

  1. #1
    Join Date
    Sep 2005
    Posts
    7

    Unicode LPCTSTR Problem

    Hello,

    I have an Unicode application and need to call an DLL with Multibyte Interface. The Interface have an Function:

    ThisFunction(LPCTSTR name)

    How I can now give an Multibyte LPCTSTR from an Unicode application to that DLL. Everything I tried gave me the linker error that there is no Function available with wchar_t *. This is right cause the DLL expect const char *

    So how to call it outside the Unicode application with an call like:

    ThisFunction("Dialog");

    With _T("Dialog") and L"Dialog it do not work (linker problem).

    Ocrana

  2. #2
    Join Date
    Dec 2005
    Posts
    642

    Re: Unicode LPCTSTR Problem

    If the parameter is a constant then just call it with
    Code:
    ThisFunction("Dialog");
    If the parameter is not a constant, use WideCharToMultiByte() to convert your Unicode string to a multibyte string (of type char*) and pass it to ThisFunction().

  3. #3
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Unicode LPCTSTR Problem

    Quote Originally Posted by Ocrana007
    ThisFunction(LPCTSTR name)

    How I can now give an Multibyte LPCTSTR from an Unicode application to that DLL.
    This should be called as follows -

    Code:
    ThisFunction (_T ("A TCHAR Constant String"));
    And...
    Quote Originally Posted by Ocrana007
    Everything I tried gave me the linker error that there is no Function available with wchar_t *. This is right cause the DLL expect const char *
    You mean compiler error (and not linker error)?
    Quote Originally Posted by Ocrana007
    So how to call it outside the Unicode application with an call like:
    With _T("Dialog") and L"Dialog it do not work (linker problem).
    Post the actual error you see.

  4. #4
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Unicode LPCTSTR Problem

    Quote Originally Posted by Ocrana007
    How I can now give an Multibyte LPCTSTR from an Unicode application to that DLL.
    BTW, there is no such thing as a Multibyte LPCTSTR.

    LPCTSTR is a typedef defined as -
    Code:
    typedef const TCHAR* LPCTSTR;
    Where TCHAR is defined as -
    Code:
    #ifdef _UNICODE
    typedef wchar_t TCHAR;
     #else
     typedef char_t TCHAR;
    ...put simply.

    So, as you can see, depending on the definition of the UNICODE macro, LPCTSTR can be a wchar_t* or a char*

    MBCS (Multi-Byte Character String) is basically a char* string that allows a simple character string to hold international characters by allowing a variable width of characters per symbol (alphabet).

    So, a LPCTSTR can be a Wide Character String, or a Simple Character string depending on the definition of the UNICODE macro. If the latter, it can optionally be a MBCS string depending on the definition of the MBCS macro (that redirects all _t* string functions to mb* functions). So, a LPCTSTR being multi-byte is a special case of a LPCTSTR, and one must not pass a pure-MBCS string as a LPCTSTR - it will not compile for UNICODE settings.

    Essentially, if your function accepts a LPCTSTR, supply a LPCTSTR string wrapped in the _T macro, and let the pre-processor definition convert the string for you - i.e. _T gets mapped as L when _UNICODE is defined (making the string literal a wide-character one), and ignored otherwise (making it a character string).
    Last edited by Siddhartha; December 17th, 2005 at 07:33 AM.

  5. #5
    Join Date
    Oct 2005
    Posts
    199

    Re: Unicode LPCTSTR Problem

    Have you compiled the DLL with unicode -option as well? If not, you have to convert the unicode string passed to DLL-function to multibyte string. Since they are separate binaries, they don't automatically adapt to each other's settings.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center