 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Maurice Anderson Guest
|
Posted: Wed Apr 25, 2007 8:10 am Post subject: convert Variant to unsigned char* |
|
|
Hello,
Yet another binary type question.
I have Variant FData filled with binary data, something like
FFD8FFE000104A46. The size of the data is 200 Bytes.
So I want to do:
Variant FData;
..
..
unsigned char* Test = new char[200];
Test = FData.
But it doesnt work and I get an exception thrown.
When I do a ShowMessage(FData.Type()), I get 2011 which appears to be a
combo of varArray 0x2000 and varByte 0x0011.
My question is how do I get the contents of FData into Test?
Thanks
I want to place |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Wed Apr 25, 2007 8:10 am Post subject: Re: convert Variant to unsigned char* |
|
|
"Maurice Anderson" <mauriceanderson (AT) hotmail (DOT) com> wrote in message
news:462ed59d$1 (AT) newsgroups (DOT) borland.com...
| Quote: | When I do a ShowMessage(FData.Type()), I get 2011 which
appears to be a combo of varArray 0x2000 and varByte 0x0011.
|
That is exactly what it is (more accurately, a combination of the
VT_ARRAY and VT_UI1 types, which the VCL mimics).
| Quote: | how do I get the contents of FData into Test?
|
When the VT_ARRAY (varArray) flag is present, the Variant contains a
SAFEARRAY, which is an Automation-compatible array (it can be
marshalled across thread/process boundaries, etc). The other flags
tell you the data type that is contained inside the array (in this
case, an array of bytes), and whether the Variant contains the array
itself or a reference to an array elsewhere in memory. So you have to
extract the SAFEARRAY first, and then you can access the raw data that
is inside of it. Variant has methods for working with array data to
hide those details from you, for example:
Variant FData;
// ...
if( (FData.IsArray() && (FData.Type() & varByte) )
{
int lBound = FData.ArrayLowBound();
int hBound = FData.ArrayHighBound();
if( hBound >= lBound )
{
unsigned char *bytes = (unsigned char *)
FData.ArrayLock();
int numBytes = ((hBound - lBound) + 1);
// use data up to numBytes as needed...
FData.ArrayUnlock();
}
}
Gambit |
|
| Back to top |
|
 |
Maurice Anderson Guest
|
Posted: Wed Apr 25, 2007 8:10 am Post subject: Re: convert Variant to unsigned char* |
|
|
Thanks Remy. Only one thing;
| Quote: | // use data up to numBytes as needed...
|
I am not sure how to use the data. For example, how could I display it in a
Memo Control? |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Wed Apr 25, 2007 9:25 pm Post subject: Re: convert Variant to unsigned char* |
|
|
"Maurice Anderson" <mauriceanderson (AT) hotmail (DOT) com> wrote in message
news:462eefc3$1 (AT) newsgroups (DOT) borland.com...
| Quote: | I am not sure how to use the data. For example, how
could I display it in a Memo Control?
|
{
char *chars = (char *) FData.ArrayLock();
int numChars = ((hBound - lBound) + 1);
Memo1->Text = AnsiString(chars, numChars);
FData.ArrayUnlock();
}
Gambit |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|