Learn how to a build a cloud-first strategyRegister Now

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

What's the safe and best way to assing the VARIANT to wstring?

I have several similar code to following.  I would like to know what 's the best way to get the result form a VARIANT variable to a wstring variable.  Here the temp is a _bstr_t.  I'm affarid that the address I assign to my wstring will be deleted since temp is a local variable and it will be automatically clean up once out of the scope, right?  Thanks.

            if(!newUserData.bWinLogOn)//if not using winlogin then get the ind loginname
            {
                  hResult =ptSyUserObject->Get(CComBSTR("symark-LoginName"), &var);
                  if(FAILED(hResult))//New Sy User
                  {
                        DisplayError( hResult, L"Error obtaining symark-LoginName. - RetrieveUserAtt");
                        goto CleanUp;
                  }
                  temp = var;
                  if(temp.length()>0)
                  {
                        newUserData.LoginName.c_str() = temp.operator const wchar_t *();

                  }
                  else
                        newUserData.LoginName =NULL;
                  VariantClear(&var);
            }
0
lapucca
Asked:
lapucca
  • 2
1 Solution
 
jkrCommented:
>>I'm affarid that the address I assign to my wstring will be deleted since temp is a local variable and
>>it will be automatically clean up once out of the scope, right?

No, the variable you assign it to (if it is a std::wstring) will copy the contents, so they'r not lost.

BTW,

                    newUserData.LoginName.c_str() = temp.operator const wchar_t *();

should be

                    newUserData.LoginName = temp;

then you won't run into problems.
0
 
lapuccaAuthor Commented:
Wow, that's great!  So, I can assign a _bstr_t variable directly to a wstring variable?  Can I also do that with PWSTR or LPWSTR types of variables?   newUserData.contextName  is wstring, contextNames is LPWSTR.  So I assign a ointer type of variable but wstring variable, newUserData.contextName, will make a copy in its own allocated memory space?

            newUserData.contextName = contextNames;
0
 
jkrCommented:
>>Wow, that's great!  So, I can assign a _bstr_t variable directly to a wstring variable?

Yes. Ig youu wanted to explicitly call the conversion operator, just use

                    newUserData.LoginName = (wchar_t*) temp;

which will invoke '_bstr_t::operator wchar_t*( )'

>>Can I also do that with PWSTR or LPWSTR types of variables?

Since both of them are typedef'd as 'wchar_t*', sure you can. That will work.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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