Description and routine of incoming and outgoing strings through the COM interface

Source: Internet
Author: User

Except for basic non-pointer types (such as ing, long, And bool), other parameters passed in through the COM interface must use the specific COM type --
For strings, The BSTR type should be used. This type is a fuzzy type. Microsoft does not disclose the internal structure, and all the supported APIs must be used to manipulate it.
Other types include variant, safearray, and so on. You can refer to the materials for Developing COM components using ATL or VC.

For youCodeConvert the input string to uppercase and return the result:

The interface is defined
[ID (1), helpstring ("method upper")] hresult upper ([in] BSTR instr, [out, retval] BSTR * outstr );

Implementation Code:

Stdmethodimp ccstr: Upper (BSTR instr, BSTR * outstr)
{
/*
 
Related type conversion:
Conversion from a BSTR string to a unicode string (for example, lpolestr) is not required, that is, conversion is not required. It can be used as a unicode string.
Conversion from a unicode string to a BSTR string must go through the corresponding API function sysallocstring.

To convert a string from a standard C to a unicode string, use the multibytetowidechar function.
The widechartomultibyte function is required to convert Unicode strings to Standard C strings.

Conclusion: The Unicode string can be used as an intermediary to convert each other between the string of Standard C and the string of BSTR!

Function process:
Convert the input string instr to the corresponding standard C string szinstr through the conversion function, and convert the szinstr to uppercase using the standard C Conversion Function.
Converts szinstr to the bstr string outstr by using the conversion function and outputs it as the return value.


Fill:
Conversion from a standard C string to a BSTR string. If ccombstr class is used in ATL, the conversion can be quite simplified and only one line of code is required.
// Convert from char * To BSTR
// Ccombstr bstrstring (szstdstring );
// Done !!
*/

// The following describes how to directly use APIs without using classes in ATL.
//

// Obtain the string length
//
Long nlen = widechartomultibyte (cp_acp, 0, instr,-1, null, null );
Char * szinstr = new char [nlen];

// Convert a unicode string to a C standard string
//
Widechartomultibyte (cp_acp, 0, instr,-1, szinstr, nlen, null, null );
Szinstr = strupr (szinstr );

// The safest function for processing multiple character sets is lstrlen (instead of strlen)
//
Nlen = lstrlen (szinstr) + 1; // The reason for adding 1 is to add two characters ending with null (next, multiply by 2)
Int nalloc = 2 * nlen;

// Allocate space to Unicode strings
//
Lpolestr sznewolestring;
Sznewolestring = (olechar *) New wchar_t [nalloc];

// Convert the C standard string to the Unicode string
//
Multibytetowidechar (cp_acp, 0, szinstr,-1, znewolestring, nlen); // note that nlen is used instead of nalloc !!

// Convert a unicode string to a BSTR string
BSTR bstrtemp;
Bstrtemp = sysallocstring (sznewolestring );
* Outstr = bstrtemp;

Return s_ OK;
}

// The following code uses the ccombstr class method as described in the supplement:
//
Long nlen = widechartomultibyte (cp_acp, 0, instr,-1, null, null );
Char * szinstr = new char [nlen];
Widechartomultibyte (cp_acp, 0, instr,-1, szinstr, nlen, null, null );
Szinstr = strupr (szinstr );

MessageBox (null, szinstr, "", mb_ OK );

// Directly convert from standard C string to BSTR string
Ccombstr bstrtemp2 (szinstr );
* Outstr = bstrtemp2.detach ();

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.