Link to home
Create AccountLog in
Avatar of tommym121
tommym121Flag for Canada

asked on

dealing variant string in C++

I am debugging a program using variant string.
I have a object pItem that has a member variable(m_vValue) which is a variant.  What I try to do is to assign a new string to this variable after I remove the previous string in the variable.
I am not sure what is the proper way to free the old string and reassign the new string into the variant variable.
      char *sval = (char *) "Hello";
      OLECHAR *sOleText;
      sOleText=new OLECHAR[strlen(sval)+1];
      // convert the char-string to OLECHAR-string
      mbstowcs(sOleText,sval,strlen(sval)+1); // +1 means: don't forget the terminating null character
      // if there is already a string in m_vValue free it first before assign new value
      if( V_BSTR( & pItem->m_vValue ) )
            SysFreeString( V_BSTR( & pItem->m_vValue ) );
       // after I free I get problem in next statement ( '&pItem->m_vvalue' )
       // from Visual Studio Watch Window, I have the following info
       // BSTR = 0xcdcdcdcd <Bad Ptr>      tagVARIANT *)
       // I want to free the content of m_vValue, but instead I free the location of m_vValue
      V_BSTR( & pItem->m_vValue ) = SysAllocString( sOleText );
                  delete sOleText;
       // any thought will greatly appreciated.

Avatar of tommym121
Flag of Canada image


p_Item belong to following Class

class A
       VARIANT m_vValue;
Avatar of jkr
Flag of Germany image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Or, in your case using  aclass member:
#include <comutil.h>

class A
       _variant_t m_vValue; // never bother about allocations and assignment any more ;o)

Open in new window

Unfortunately, that is not my code, I can not simply change it, it may have big impact on other codes that are here.
Thanks. I thought of another way to do what I need. Thanks for your response