0

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.

jaguar
  • 25
  • 3

4 Answers4

1

The pointer returned by wStr1.c_str() may become invalid when wStr1 is later modified.

The best fix is to stick to C++ types:

std::wstring strings[MAX_PATH];

// ...
MultiByteToWideChar(CP_ACP, 0, str3.c_str(), slength, buf, len);
strings[index] = buf;
delete[] buf;
// ...

or, you could postpone deleting the buffer and just use it in your array:

LPCWSTR lpStr1[MAX_PATH];
// ...
wchar_t* buf = new wchar_t[len];   
MultiByteToWideChar(CP_ACP, 0, str3.c_str(), slength, buf, len);
lpStr1[index] = buf;
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • instead of converting to wstring if we are converting to wchar_t* and assigning to LPCWSTR directly. this is working fine. I am getting the value of lpStr1 outside the for loop. Thank you – jaguar Apr 08 '15 at 06:50
0

There are a few problems here:

  • LPCWSTR lpStr1[MAX_PATH]; is defining an array of pointers to const wchar_t, not an array of const wchar_t as you no doubt intend.
  • lpStr1[index]=wStr1.c_str(); is storing the pointer to the temporary buffer returned by c_str(). This does not copy the string into lpStr[index].
  • I'm not sure what iLimit and jLimit are, but I don't see what those loops are intending to accomplish if you really only want to convert a string value to a wide character array.
zennehoy
  • 6,405
  • 28
  • 55
  • yah, i am also thinking the same. the pointer reference is the problem. How to fix this issue ? iLimit and jLimit you can give any value. take iLimit=2 jLimit=3. – jaguar Apr 08 '15 at 05:44
  • @jaguar In your question you state that you are "converting string value to LPCWSTR", which shouldn't require those loops at all. Please tell us exactly what you are trying to accomplish. – zennehoy Apr 08 '15 at 05:49
  • this loop is not for converting to LPCWSTR. I doing some other operations in this loop. I just mentioned this loop to say when i am converting a string value to LPCWSTR inside a loop after executing the loop the value is cleaning automatically. I resolved this issue as -molbdnilo suggested. instead of converting the string to wstring if directly converting to wchar_t* , then it is working properly. The problem is when we are using wStr1.c_str() . Thank you for your suggestion. The issue you mensioned is correct. – jaguar Apr 08 '15 at 09:37
0

I'd suggest using the following routine for UNICODE conversion:

wstring AsciiToUtf16(const string & str)
{
   if (str.empty())
      return wstring();

   size_t charsNeeded = ::MultiByteToWideChar(CP_ACP, 0, 
      str.data(), (int)str.size(), NULL, 0);
   if (charsNeeded == 0)
      throw runtime_error("Failed converting ASCII string to UTF-16");

   vector<wchar_t> buffer(charsNeeded);
   int charsConverted = ::MultiByteToWideChar(CP_ACP, 0, 
      str.data(), (int)str.size(), &buffer[0], buffer.size());
   if (charsConverted == 0)
      throw runtime_error("Failed converting ASCII string to UTF-16");

   return wstring(&buffer[0], charsConverted);
}
Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
0

Why not follow this:

C++ Convert string (or char*) to wstring (or wchar_t*)

and forget the LPCWSTR altogether?

Community
  • 1
  • 1
Adrian May
  • 2,127
  • 15
  • 24