I am a beginner cpp programmer. I am converting string value to LPCWSTR. When i am trying to access this value, it is giving a null value. Please check this code attached below. I think this is because of memory reference value is clearing out after the scope of the variable.
std::wstring string2wString(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
void main(){
string str1,str2,str3;
wstring wStr1;
LPCWSTR lpStr1[MAX_PATH];
int index=0;
for(int i=0;i<iLimit;i++)
{
str1="String 1";
//do operations
for(int j=0;j<jLimit;j++)
{
// do operations
str2=" String 2";
str3= str1+str2;
wStr1= string2wString(str3); //converting to wstring
lpStr1[index]=wStr1.c_str();
index++
}
}
cout << lpStr1[0] << endl;
}
Please help me to resolve this issue.