Solved

How to extract a string from a cOleVariant

Posted on 1998-12-03
4
927 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 53
sumNumber challenge 16 140
move a line in eclipse 3 109
Change to event 1 129
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.

726 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