CString to wstring-VBForums
Results 1 to 8 of 8

Thread: CString to wstring

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 1999
    Location
    Cincinnati, OH
    Posts
    66
    Anybody know of a way to convert a CString into a wstring?
    If you drop a blond and a UNIX box out a window, which one lands first?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If UNICODE is defined, then you can use:
    Code:
    wstring str = LPCTSTR(myCString);
    If not, then you'd have to use an ordinary string.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 1999
    Location
    Cincinnati, OH
    Posts
    66
    WHen I try that it returns:
    error C2440: 'initializing' : cannot convert from 'const char *' to 'class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> >'
    No constructor could take the source type, or constructor overload resolution was ambiguous

    I have defined Unicode like this:
    #ifndef UNICODE
    #define UNICODE
    #endif
    If you drop a blond and a UNIX box out a window, which one lands first?

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It's giving a char error because CString doesn't think it's using Unicode. If you assigned it to a string, then it would be fine.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 1999
    Location
    Cincinnati, OH
    Posts
    66
    im not sure I follow. What am I assigning a string?
    Do you mean:
    string str;
    CString Cstr;
    wstring wstr;
    wstr=Cstr=str;
    ??
    If you drop a blond and a UNIX box out a window, which one lands first?

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    #ifdef _UNICODE
    #define String wstring
    #else
    #define String string
    #endif
    
    String mystring;
    CString myCString = "Hello";
    
    mystring = LPCTSTR(myCString);
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 1999
    Location
    Cincinnati, OH
    Posts
    66
    It fails to recognize String as a datatype. Heres what I think you're getting at:

    #include <windows.h>
    #include <vector.h>
    #include <lm.h>
    #include <stdio.h>

    #ifdef _UNICODE
    #define String wstring
    #else
    #define String string
    #endif


    main()
    {
    String mystring;
    CString myCString = "Hello";

    mystring = LPCTSTR(myCString);
    return 0;
    }

    And heres the build status:

    --------------------Configuration: temp - Win32 Debug--------------------
    Compiling...
    temp.cpp
    C:\temp.cpp(15) : error C2065: 'string' : undeclared identifier
    C:\temp.cpp(15) : error C2146: syntax error : missing ';' before identifier 'mystring'
    C:\temp.cpp(15) : error C2065: 'mystring' : undeclared identifier
    C:\temp.cpp(16) : error C2065: 'CString' : undeclared identifier
    C:\temp.cpp(16) : error C2146: syntax error : missing ';' before identifier 'myCString'
    C:\temp.cpp(16) : error C2065: 'myCString' : undeclared identifier
    C:\temp.cpp(16) : error C2440: '=' : cannot convert from 'char [6]' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    C:\temp.cpp(18) : error C2440: '=' : cannot convert from 'const char *' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    Error executing cl.exe.

    temp.exe - 8 error(s), 0 warning(s)
    If you drop a blond and a UNIX box out a window, which one lands first?

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    AAAAARRRRGGGGHHHH!!!

    Hehehe...you need to tell it you're using the string class...
    Code:
    #include <string>
    
    using std::string;
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.