CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2004
    Posts
    28

    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

  2. #2
    Join Date
    Jan 2003
    Location
    Cambridge, UK
    Posts
    752

    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);
    Cheers,

    Alex
    Please rate this post if you find it helpful

  3. #3
    Join Date
    Mar 2004
    Posts
    28

    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

  4. #4
    Join Date
    Jan 2003
    Location
    Cambridge, UK
    Posts
    752

    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
    Cheers,

    Alex
    Please rate this post if you find it helpful

  5. #5
    Join Date
    Mar 2004
    Posts
    28

    Re: unsigned char * (BYTE*) to BSTR and back

    Thank you again, Alex.

    But, I'm afraid I don't understand your comment:

    As for second question, it shoud be operator (BSTR) to get the same
    Could you please elaborate?

    Thanks,

    jim

  6. #6
    Join Date
    Jan 2003
    Location
    Cambridge, UK
    Posts
    752

    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
    Cheers,

    Alex
    Please rate this post if you find it helpful

  7. #7
    Join Date
    Mar 2004
    Posts
    28

    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

  8. #8
    Join Date
    Mar 2004
    Posts
    28

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured