Link to home
Start Free TrialLog in
Avatar of pizarro
pizarro

asked on

How to extract a string from a cOleVariant

Hello,

I'm kind of new to MFC so this might be quite an easy question for some, but I haven't been able to find the answer anywhere.  

I'm writing an application that uses DAO to access an indexed database and read the contents of a field. This is the procedure I'm following:
(1) set the index
(2) call the seek method
(3) If matches are found, get a particular field from the matching record.

Now, I know that that field will give me a string, but the GetField method of the DaoRecordset class returns a cOleVariant variable with the information inside.  I desperately need to extract the string out of the cOleVariant object but don't know how.  Please help if you can.

Thanks.
Avatar of wyy_cq
wyy_cq

COleVariant::operator LPCVARIANT
returns a VARIANT structure .

VARIANT is a structure with a flag and a union.
in the union many type variable. bstrVal is a string.
but when you access it, you 'd better check the flag first.

when you get the VARIANT you can do like this to get the string.
CString s;
s=pVAR->bstrVal;
Avatar of pizarro

ASKER

I tried what you said and I don't get an error any longer.  However,  I keep getting a "?" for something I know is a string, which means, somehow the string is not being taken out of the COleVariant object correctly.  This is the code I'm using:

void CFiltroImagenExtension::EnviarBandera(CHttpServerContext* pCtxt,
                                        LPTSTR usuario, LPTSTR banner)
{
    int iFound;
      COleVariant oPais;
      CString sPais;
      const VARIANT *vPais;
      COleVariant oIndexField;
      CDaoDatabase dbBannerManager;
      CDaoRecordset dbUsuarios(&dbBannerManager);
   
      oIndexField=COleVariant(usuario,VT_BSTRT);

      dbBannerManager.Open("c:\\BannerManager.mdb", false, true);
              dbUsuarios.Open(dbOpenTable, "Usuarios");
   
      dbUsuarios.SetCurrentIndex("Usuario");
      iFound=dbUsuarios.Seek("=", &oIndexField);
   
      StartContent(pCtxt);
      WriteTitle(pCtxt);

      if( iFound > 0 ){
            //Return Info
            oPais=dbUsuarios.GetFieldValue("pais");
            vPais= LPCVARIANT(oPais);
            sPais = vPais->bstrVal;  //Instead of getting a string for sPais,
                                                                //I'm always getting the "?" sign.
      }else{
            //Return failed Info
            //Do something else
      }

}

Instead of getting a comprehensible string for sPais (Which I know what it is in anticipation) I get a question mark "?".
ASKER CERTIFIED SOLUTION
Avatar of snoegler
snoegler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pizarro

ASKER

Just how I like it... Nice and easy :) Thanks.