Answered LPWSTR to string

  • Tuesday, July 08, 2008 4:36 AM
     
     
    How to convert LPWSTR into string??

All Replies

  • Tuesday, July 08, 2008 4:45 AM
     
     
    Into what kind of specific string?  Can you give some code samples?
  • Tuesday, July 08, 2008 5:48 AM
     
     
    yes...

    string temp;
    LPWSTR str1 = L"sometext";

    Now i want some procedure to take content of str1 into temp.
  • Tuesday, July 08, 2008 5:56 AM
     
     Answered
    string MyString = CW2A (L"sometext");

    Also, consider using wstring instead of string.
    • Marked As Answer by Yan-Fei Wei Thursday, July 10, 2008 4:34 AM
    •  
  • Tuesday, July 08, 2008 6:04 AM
     
     
    Yes, I would also suggest using wstring.  I believe you have the potential to lose data based on what language you are using or at least member functions of the string type will not function correctly.
  • Tuesday, July 08, 2008 6:14 AM
     
     
     Ok.... Now i explain , what actually i need..
    I am useing Net API.. and following network mgmt structure.

    typedef struct _SERVER_INFO_101 {
      DWORD sv101_platform_id;
      LPWSTR sv101_name;
      DWORD sv101_version_major;
      DWORD sv101_version_minor;
      DWORD sv101_type;
      LPWSTR sv101_comment;
    } SERVER_INFO_101,
    *PSERVER_INFO_101,
    *LPSERVER_INFO_101;

    So there is one function in my code that accept parameter of type "string".
    I want to pass   LPWSTR sv101_name to that function.

    So in this case , how can i convert    sv101_name (which is of type   LPWSTR ) into string.
  • Tuesday, July 08, 2008 6:31 AM
     
     
    The basic_string constructor can build a wstring from a null-terminated string.

    E.g.

    wstring str(L"Foo");
  • Tuesday, July 08, 2008 6:35 AM
     
     
    i want to build null terminated string from wstring
  • Tuesday, July 08, 2008 6:47 AM
     
     Answered
    I think you're confused.

    But to answer your question, the basic_string::c_str() member gives you a null-terminated string from a basic_string.

    E.g.

    wstring str(L"Foo"); // build a basic_string

    const wchar_t *szFoo = str.c_str(); // and reverse the process
    • Marked As Answer by Yan-Fei Wei Thursday, July 10, 2008 4:34 AM
    •  
  • Tuesday, July 08, 2008 6:55 AM
    Moderator
     
     Answered
    If you don't like CW2A, you could use WideCharToMultiByte() or wcstombs().
    Hans Passant.
    • Marked As Answer by Yan-Fei Wei Thursday, July 10, 2008 4:34 AM
    •