How to set a LPWSTR var

Hi,


I am using pointer to this structure

typedef struct __theStruct
{
/* [string] */ LPWSTR szPath;
} __theStructDEF;

Client code seems like that :
__theStructDEF * arrayOfStruct;

// init
myPointer=(__theStructDEF *) malloc(ITEMS_ARRAY_SIZE * sizeof(__theStructDEF ));
// initialize
for ( int i = 0; i < 1024; ++i )
{
arrayOfStruct[i].szPath=L"Data" ;
}
// assignation that works
arrayOfStruct[0] =L"Data/path1";
arrayOfStruct[1] =L"Data/path2";

// assignation that does not work why ?
std::string s("Data/path1");
wchar_t SomeUnicodeStr[1024];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, SomeUnicodeStr, 1024);
arrayOfStruct[0]=SomeUnicodeStr;

The second method does not work and I can't set variable wide string to the LPWSTR member of struct __theStructDEF.

Currently, I am forced to assign the array with L"path" into the code directly
LPWSTR is just a pointer. It actually is just a #define for wchar_t*

If you make it point to a buffer like 'SomeUnicodeStr', the pointer will only be valid as long as long as that buffer remains in scope.

A better option might be to use actual strings instead of pointers and buffers. Like std::wstring instead of LPWSTR. But of course then you can't use malloc/memset/etc on your struct (but of course, you shouldnt' be doing that anyway! malloc has no place in C++!)
Topic archived. No new replies allowed.