using std::string unicode build
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: using std::string unicode build

  1. #1
    Join Date
    Dec 1999
    Posts
    90

    using std::string unicode build

    Let us take a simple function
    ::MessageBox(0,data.c_str(),"log",0);
    where data is a std::sting type.

    When i change the build type to unicode ,this will spit out error
    error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char *' to 'LPCWSTR'

    How can i feed in the underlying data of a std::string type(const char *),so that it builds smoothly in both MBCS and UNICODe builds.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: using std::string unicode build

    For UNICODE you have std::wstring. You will also have a problem with strings such as "log". They should be replaced with L"log". To compile both with ANSI and UNICODE use the _T() macro to wrap all the strings and chars in your code.

    Take a look at this FAQ, http://www.codeguru.com/forum/showthread.php?t=231165.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Dec 1999
    Posts
    90

    Post Re: using std::string unicode build

    thats where the twist comes in ;i dont want to do a ifdef at each line where i have used std::string for the different build-types.
    And if i do a _T on c_str member ,it wont accept in UNICODE.
    So what is a least-painful way to make this std::string work in such situations - we use std::string at a lot of places in the code ;and we cant use a ifdef/std::wstring option easily?

  4. #4
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,716

    Re: using std::string unicode build

    Why Not you Follow Cilu Suggestion and Proceed in a Simple Way Note T("string") or TEXT("string") are macros which become L"string" if UNICODE is defined, and "string" if UNICODE is not defined.and to defined unicode you can use #define unicode before you include windows.

    here is how you can convert your Unicode to ASCII string and ASCII to Unicode.
    Code:
    // Convert an ASCII string to a Unicode String
    char SomeAsciiStr[] = "Ascii!";
    wchar_t SomeUnicodeStr[1024];
    MultiByteToWideChar(CP_ACP, 0, SomeAsciiStr, -1, SomeUnicodeStr, 1024);
    
    // Convert a Unicode string to an ASCII string
    char SomeAsciiStr[1024];
    wchar_t SomeUnicodeStr[] = L"Unicode!";
    WideCharToMultiByte(CP_ACP, 0, SomeUnicodeStr, -1, SomeAsciiStr, 1024, NULL, NULL);
    for more details have a look on
    http://en.wikibooks.org/wiki/Windows...amming/Unicode

    Thanx
    Last edited by humptydumpty; September 21st, 2006 at 01:33 AM.

  5. #5
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: using std::string unicode build

    Quote Originally Posted by ranadhir nag
    thats where the twist comes in ;i dont want to do a ifdef at each line where i have used std::string for the different build-types.
    You can do a single typedef:
    Code:
    typedef basic_string<TCHAR> tstring;
    Then tstring is defiend as string or wstring depending on build-type.

    - petter

  6. #6
    Join Date
    Dec 1999
    Posts
    90

    Re: using std::string unicode build

    got it.Actually what i was seeknig is the A2T macro

  7. #7
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: using std::string unicode build

    Quote Originally Posted by ranadhir nag
    got it.Actually what i was seeknig is the A2T macro
    IMO it's better to store the strings in their correct format than to convert them every time they're needed.

    - petter

  8. #8
    Join Date
    Dec 1999
    Posts
    90

    Re: using std::string unicode build

    Sure.i also believe the same.
    A couple of questions on this issue:
    'typedef basic_string<TCHAR> tstring' expands to a wstring for UNICODE and string for MBCS - is that right?
    So,c_str member returns a LPCTSTR(char* in MBCS and wchar* in UNICODE).
    is that correct?

  9. #9
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: using std::string unicode build

    Yes, that is correct.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  10. #10
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: using std::string unicode build

    That's correct.

    Example of code that should work in both UNICODE and non-UNICODE build.
    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <string>
    
    #pragma comment(lib, "user32.lib")
    
    typedef std::basic_string<TCHAR> tstring;
    
    int main()
    {
        tstring str = _T("hello");
        ::MessageBox(0, str.c_str(), _T("log"), 0);
    }
    - petter

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center