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);
            }
lapuccaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

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.
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;
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.

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
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.