Solved

type casting between _variant_t and bitmap

Posted on 2004-08-27
9
2,207 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
Industry Leaders: 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 150 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

728 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