Solved

How to extract a string from a cOleVariant

Posted on 1998-12-03
4
907 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
ID: 1325744
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
ID: 1325745
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
ID: 1325746
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
ID: 1325747
Just how I like it... Nice and easy :) Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET how to use the Vertical ScrollBar 5 90
Error on moodle after upgrade 3 124
Complete beginner needs help making a cron job 9 110
scoreUp challenge 14 57
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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…

863 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

27 Experts available now in Live!

Get 1:1 Help Now