0

I'm using C++ (Windows environment). I have a :

LPCWSTR mystring;

This works :

mystring = TEXT("Hello");

But how to do this ? :

mystring = ((((create a new string with text = the content which is in another LPCWSTR 'myoldstring'))))

Thanks a lot in advance!

PS :

mystring = myoldstring; 

would work , but it would not create a new string, it would be the same pointer. I want to create a new string !

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Basj
  • 41,386
  • 99
  • 383
  • 673

3 Answers3

2

To use C++ standard strings, you need to include the <string> header. Since you're dealing with LPCWSTR (emphasis on the W part of that) you're dealing with wide characters, so you want to use wide strings (i.e., std::wstring instead of std::string).

#include <string>
#include <iostream>
#include <windows.h>

int main() { 
    LPCWSTR x=L"This is a string";

    std::wstring y = x;
    std::wcout << y;
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • I don't know why, but with this solution, there is crash ! – Basj Dec 09 '12 at 14:58
  • @JosBas: That would point to a problem with the standard library you're using, or somewhere else in your code. Of the two, a problem elsewhere in your code seems a lot more likely to me. I've edited in a slightly more complete example that should compile and display the copied string (it does for me anyway). If it works, your problem is probably elsewhere in your code. – Jerry Coffin Dec 09 '12 at 15:04
  • If I remove this line, no crash. If I add it, there is a crash. I have used another variable names that are used *nowhere else*, such that the crash cannot come from anywhere else. – Basj Dec 09 '12 at 15:10
  • 1
    @JosBas: That sounds like fairly typical results from having undefined behavior: some seemingly unrelated change suddenly leads to a crash. Without seeing your other code, it's impossible to guess where it might have come from though. Of course, it *is* always possible your standard library is defective -- `std::wstring` probably isn't nearly as well tested as `std::string` (but it's still much more likely the problem is in your code). – Jerry Coffin Dec 09 '12 at 15:16
2
LPTSTR mystring;
mystring = new TCHAR[_tcslen(oldstring) + 1];
_tcscpy(mystring, oldstring);

... After you are done ...

delete [] mystring;

This is a complete program

#include <tchar.h>
#include <windows.h>
#include <string.h>

int main()
{
    LPCTSTR oldstring = _T("Hello");

    LPTSTR mystring;
    mystring = new TCHAR[_tcslen(oldstring) + 1];
    _tcscpy(mystring, oldstring);

    // Stuff

    delete [] mystring;


}

It compiles fine with cl /DUNICODE /D_UNICODE a.cpp

I used tchar macros. If you don't want to use it, then

#include <windows.h>
#include <string.h>

int main()
{
    LPCWSTR oldstring = L"Hello";

    LPWSTR mystring;
    mystring = new WCHAR[wcslen(oldstring) + 1];
    wcscpy(mystring, oldstring);

    // Stuff

    delete [] mystring;


}

Compiles fine with cl a.cpp

user93353
  • 13,733
  • 8
  • 60
  • 122
0

what about

string myNewString = std::string(myOldString);

Just using the copy constructor of the string library.

cowboydan
  • 1,062
  • 7
  • 15
  • `'string' : identificateur introuvable !` (Can't find `string`) I use C++ (no .NET ) – Basj Dec 09 '12 at 14:39
  • @JosBas `#include ` ... and then make sure the `std` namespace is properly used, either though prefixing (`std::string` was omitted in one of the two cases above) or through `using namespace std;`. – Branko Dimitrijevic Dec 09 '12 at 14:58