Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1257
  • Last Modified:

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;
      }
0
lapucca
Asked:
lapucca
  • 2
1 Solution
 
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
 
lapuccaAuthor Commented:
Thank you very much.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now