Solved

type casting between _variant_t and bitmap

Posted on 2004-08-27
9
2,201 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
  • 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
maze travler 6 47
Delphi 2010 Export to pdf 2 253
Prototype for MessageDlg in Embacadero XE 2 2 31
gdb doesn't stop on breakpoint 2 10
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 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

20 Experts available now in Live!

Get 1:1 Help Now