?
Solved

type casting between _variant_t and bitmap

Posted on 2004-08-27
9
Medium Priority
?
2,209 Views
Last Modified: 2013-12-03
hi,
I'm trying to retrieve values from a database.

This part works perfectly;
(_bstr_t)pRecordset->Fields->GetItem("mydatabase_names")->Value;
mydatabase_names variable type is varchar so this conversion works free of problems

Also a column holds pictures of the people in the data base. this time I can't make type casting like below;

(Bitmap*)pRecordset->Fields->GetItem("mydatabase_names")->Value;        //  or
(Image*)pRecordset->Fields->GetItem("mydatabase_names")->Value;

pRecordset->Fields->GetItem("mydatabase_names")->Value is a _variant_t object.
Also I couldn't find how to use this object's ChangeType() methods

I get compiler error that type cast 'cannot convert'

How can I retrieve and hold these pictures?
0
Comment
Question by:sinacetiner
[X]
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
  • 5
  • 3
9 Comments
 
LVL 2

Expert Comment

by:carribus
ID: 11911102
You can't just cast blob data like that.
When you get to a field that you know contains blob data, you will need to first read teh blob data into a buffer and only then pack into into a bitmap. Unfortunately, there are no native functions to do that for you...
0
 

Author Comment

by:sinacetiner
ID: 11911535
ok, but are there any other way to read data from a database except using _recordsetptr if exists what kind of buffer do I have to use?
0
 
LVL 14

Expert Comment

by:Daniel Junges
ID: 11911587
the sample below shows how to read blobs from database:


_variant_t var_column_value;
field->get_Value(&var_column_value); // field is received from  Recordset

SAFEARRAY FAR *psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
                                    
long len = field->ActualSize;
//Create a safe array to store the array of BYTES  
rgsabound[0].cElements = len;
psa = SafeArrayCreate(VT_UI1,1,rgsabound);
                                    
_variant_t varChunk = field->GetChunk(len);
      
unsigned char* data;
SafeArrayAccessData( varChunk.parray, ( void **)&data );
// -----> now you can construct you bitmap here
SafeArrayUnaccessData( varChunk.parray );

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sinacetiner
ID: 11912009
pardon I couldn't understand the type of field if it is _recordsetptr there is no get_Value method exits. And also where was the parray defined? Is parray means psa?(maybe wrongly you used like that)
While constructing bitmap How can I use safearray.
0
 
LVL 14

Accepted Solution

by:
Daniel Junges earned 450 total points
ID: 11912705

parray is a member of class _variant_t  // defined in comutil.h

// field definition
ADODB::Fields* fields;
spRS->get_Fields(&fields);

for(int index=0; index<fieldsCount; index++){
  ADODB::Field* field;
  fields->get_Item(_variant_t((long)index), &field);
  ...
  ...
}
 
0
 

Author Comment

by:sinacetiner
ID: 11941614
excuse me this is my last question.  How can I convert safearray into stream
None of the constructors allow creating a bitmap or image with safearray
0
 

Author Comment

by:sinacetiner
ID: 11941877
bitmap *img;
I have tried SafeArrayAccessData( varChunk.parray, ( void **)&img );
but this time an exception occurs
0
 
LVL 14

Expert Comment

by:Daniel Junges
ID: 11945459
unsigned char* data;
SafeArrayAccessData( varChunk.parray, ( void **)&data );

//
// Here you have access to the binary data to construct you img, you have to copy the buffer
// if you want to use the img after calling the function bellow.
//

SafeArrayUnaccessData( varChunk.parray );
0
 

Author Comment

by:sinacetiner
ID: 11950841
can you give an example all you said is true but I get exception.  I can understand
SafeArrayAccessData(varChunk.parray, ( void **)&data) gives start of the blobs object but after that by using Bitmap(data) isnt valid and also using Bitmap(*data) is nonsense. also I tried to use IO::Stream and Byte array but they arent also useless and causes exception.
After that I look earlier lines in my code. I think usage of
pCommand->CommandText = "Select pictures From Student"; //pcommand is defined as _Commandptr
is true.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

765 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