How do I get the Variant result to wstring variable?

Hi, Expert,
I have several calls to get different data into a VARIANT var.  I need to assign the result of the var to a few different wstring variables.  I'm not very famililar with wstring usage and I wonder what's the best way to do this.  I currently have a buffer set up to used as a receiveing buffer parameter:
      wchar_t *syDataBuffer = new WCHAR[ONE_K];
      wstring syData = syDataBuffer;

      numCharCopy = GetDlgItemText(hwndDlg, IDC_Indep_Name, syData, ONE_K);

but I feel wrong to just use assignment operator each time for different wstring vaiables.  Does the wstring maks a copy of syData using its own allocated memory space?  Thanks.


bool CUserPage::SaveSyUserData(HWND hwndDlg, int index)
{
      //This method saves the currently display user data of the
      //selected context and push into the vector queue

      int numCharCopy, size;
      wchar_t *syDataBuffer = new WCHAR[ONE_K];
      wstring syData = syDataBuffer;
      CUserContextData newUserData;
      //CString csSyShell;
      bool setRet=true, noData=true, validated=false;

      if(ListView_GetCheckState(hwndListView, index))
            newUserData.symarkEnabled= true;
      else
            newUserData.symarkEnabled= false;

      newUserData.LoginName = NULL;
      newUserData.UID = NULL;

......
            numCharCopy = GetDlgItemText(hwndDlg, IDC_Indep_Name, syData, ONE_K);
            if((numCharCopy <= 0) && (newUserData.symarkEnabled))
            {
                  ::MessageBox(NULL, L"Independent Name is required when you select to use it as the Login Name.", L"Unity - DialogProc", MB_OK);                        
                  setRet = false;
                  goto CleanUp;
            }
            else if(numCharCopy >0)
            {
                  newUserData.LoginName = syData;
            }
      }
      else
            newUserData.bWinLogOn = true;

      numCharCopy = GetDlgItemText(hwndDlg, IDC_SHELL, syData, ONE_K);
      if((numCharCopy <= 0) && (newUserData.symarkEnabled))
      {
            ::MessageBox(NULL, L"Shell specification is mandatory.", L"Unity - SaveSyUserData", MB_OK);
                  setRet = false;
                  goto CleanUp;
      }
      else if(numCharCopy > 0)
      {
            newUserData.shell = syData
            noData = false;
      }
lapuccaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lapuccaAuthor Commented:
Actually, can I use the wstring as a receiving buffer for the call
          numCharCopy = GetDlgItemText(hwndDlg, IDC_Indep_Name, syData, ONE_K);
and change it to something like this?
          numCharCopy = GetDlgItemText(hwndDlg, IDC_Indep_Name, &(newUserData.LoginName), ONE_K);

Would this be OK to do?  Thanks.
0
jkrCommented:
>>but I feel wrong to just use assignment operator each time for different wstring vaiables.  Does the
>>wstring maks a copy of syData using its own allocated memory space?  Thanks.

It won't work that way, since the 'wstring' copyies the buffer contents on assignment - you should rather use

     wchar_t *syDataBuffer = new WCHAR[ONE_K];
 
     numCharCopy = GetDlgItemText(hwndDlg, IDC_Indep_Name, syDataBuffer, ONE_K);
     wstring syData = syDataBuffer;

Otherwise, you will assign an uninitialzed buffer, which at best will be invalid.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lapuccaAuthor Commented:
Thank you very much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.