unsigned char * (BYTE*) to BSTR and back
*MUCH* has been written on BSTR/_bstr_t conversion, but I've been unable to get my head around how it applies here.
Perhaps I just need a nudge.
My 3rd party encryption/decryption routine APIs are
BYTE* EncryptString(const BYTE* data);
BYTE* DecryptString(const BYTE* data);
and I need to store the encrypted data in a SQL Server database.
The data is truly binary. So upon advice from elsewhere in the forum, I chose BSTR and ADO.
Code:
_bstr_t encryptedBSTR = pRst->Fields->GetItem("data")->Value;
BYTE* encryptedData <<<< encryptedBSTR;
BYTE* decryptedData = DecryptString(encryptedData);
...
BYTE* encryptedData = EncryptString(decryptedData);
encryptedBSTR <<<< encryptedData;
pRst->Fields->GetItem("data")->Value = encryptedBSTR;
But what do I use in place of <<<< ???
Thanks for any hints
jim
Re: unsigned char * (BYTE*) to BSTR and back
i guess something like this should work:
Code:
BYTE bt[] = { 1,2,3,4 };
BSTR bstrBuff = SysAllocStringLen((LPOLESTR)bt,sizeof(bt));
_bstr_t bstrt(bstrBuff);
BSTR& btOut = bstrt.GetBSTR();
BYTE *pOut = (BYTE*)(char*)btOut;
SysFreeString(bstrBuff);
Re: unsigned char * (BYTE*) to BSTR and back
Ta, Alex, for your helpful reply.
This seems to be leading me in the right direction; however, I do have some questions/issues.
1)
Because I have to work with BYTE*, I've modified your suggestion like this
Code:
BYTE* bt = new BYTE [256];
for (int i = 0; i < 256; i++) //my initialization for testing
{
bt[i] = i + 1;
}
BSTR bstrBuff = SysAllocStringLen((LPOLESTR)bt,256); //sizeof(bt) in this case being only 8
Is this valid?
2)
I neglected to mention that I'm using VC++ 6.0. Perhaps that explains this error message:
Code:
_bstr_t bstrt(bstrBuff);
BSTR& btOut = bstrt.GetBSTR();
error C2039: 'GetBSTR' : is not a member of '_bstr_t'
Thanks for any additional ideas.
jim
Re: unsigned char * (BYTE*) to BSTR and back
Yes, initialization is valid, you may check it once all stuff begins to work. As for second question, it shoud be operator (BSTR) to get the same
Re: unsigned char * (BYTE*) to BSTR and back
Thank you again, Alex.
But, I'm afraid I don't understand your comment:
Quote:
As for second question, it shoud be operator (BSTR) to get the same
Could you please elaborate?
Thanks,
jim
Re: unsigned char * (BYTE*) to BSTR and back
I meant that _bstr_t class has something to return BSTR, so have a look at its header to discover
Re: unsigned char * (BYTE*) to BSTR and back
For anyone interested, the VC++ 6.0 version of Alex's .NET suggestion:
Code:
_bstr_t bstrt(bstrBuff);
BSTR& btOut = bstrt.GetBSTR();
BYTE *pOut = (BYTE*)(char*)btOut;
SysFreeString(bstrBuff);
seems to be
Code:
_bstr_t bstrt(bstrBuff);
BSTR btOut = bstrt.copy();
BYTE *pOut = (BYTE*)(char*)btOut;
SysFreeString(bstrBuff);
If anyone disagrees, I'd love to know about it.
jim
Re: unsigned char * (BYTE*) to BSTR and back
For anyone who may be interested:
The original issue for me was to get an encrypted BYTE* array of binary values stored into a SQL Server database.
I chose BSTR as the vehicle but that seems to have been incorrect.
I received from invaluable help from Krzemo in the CodeGuru Database forum.
I was told that although BSTR can contain binary values, it cannot be used to store binary values into a database.
I was advised to convert my BYTE* array to SAFEARRAY and use the SAFEARRAY to write to/read from the database.
To convert to/from BYTE* and SAFEARRAY, I adapted code from the sample in
http://www.codeproject.com/database/...select=1608797
All seems to be well.
jim