Solved

type casting between _variant_t and bitmap

Posted on 2004-08-27
9
2,203 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert char array to number in c 5 85
Windows 7 - Fuzzy Fonts 11 72
C++ :Change value from  DisableCMD registry 4 60
Why isn't object file created? 6 59
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

773 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