Converts the content of the byte array to the BSTR type.

Source: Internet
Author: User

Problem description:

Previously, we had an interface similar to int winapi readbiosversion (byte * bios, ulong size, char * version). This interface is packaged in DLL and operated on the underlying BIOS. The person who provides the interface provides 32-bit and 64-bit DLL sets. Our applicationProgramIt is 32bit. 64bit needs to be called to run on 64bit OS. Therefore, our solution is to use the com outside the process described in the previous article. But the problem arises. I provide an interface in COM to encapsulate this interface, for example, int
Winapi readbiosversion (BSTR bios, ulong size, BSTR * ver );

At this time, we are faced with the problem of converting the content of the byte array to the BSTR type and vice versa.

The solutions provided by netizens are as follows:

Reply: solution 1:

 
_ Bstr_t STR = (char *) bytearray; BSTR = Str. getbstr ();

Solution 2:

 
Ccomvariant Var = (char *) bytearray; BSTR = var. bstrval;

Solution 3:

 
Uses_conversion; BSTR = a2bstr (char *) bytearray );

I found a string truncation problem in my project. If the byte array contains a normal string, the Netizen's solution should be fine. It is a coincidence that the byte array contains empty characters, which gives me the motivation to write this topic.

My solution is:

 
BSTR = sysallocstringlen (null, dwsize/2); memcpy (BSTR, pdata, dwsize );

Otherwise, the BSTR type is converted to a byte array

 
Lpbyte Lp = new byte [size]; memcpy (LP, bios, size );

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.