Solved

How to extract a string from a cOleVariant

Posted on 1998-12-03
4
896 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:pizarro
  • 2
4 Comments
 
LVL 2

Expert Comment

by:wyy_cq
Comment Utility
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;
0
 

Author Comment

by:pizarro
Comment Utility
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 "?".
0
 
LVL 6

Accepted Solution

by:
snoegler earned 200 total points
Comment Utility
sPais = V_BSTRT(&oPais);

This is like it is done in the class CCrack, which is part of the
MSVC documentation. Just browse the help for 'CCrack', then
you'll see how the MSVC programmers did it.
0
 

Author Comment

by:pizarro
Comment Utility
Just how I like it... Nice and easy :) Thanks.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now