[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2211
  • Last Modified:

type casting between _variant_t and bitmap

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
sinacetiner
Asked:
sinacetiner
  • 5
  • 3
1 Solution
 
carribusCommented:
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
 
sinacetinerAuthor Commented:
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
 
Daniel JungesCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sinacetinerAuthor Commented:
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
 
Daniel JungesCommented:

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
 
sinacetinerAuthor Commented:
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
 
sinacetinerAuthor Commented:
bitmap *img;
I have tried SafeArrayAccessData( varChunk.parray, ( void **)&img );
but this time an exception occurs
0
 
Daniel JungesCommented:
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
 
sinacetinerAuthor Commented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now