Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to extract a string from a cOleVariant

Posted on 1998-12-03
4
920 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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