Exchange of C ++ (/CLR) unmanaged and managed types

Source: Internet
Author: User

The following code must contain"Common Language Runtime support (/CLR)AndMFC"Supported

1. Conversion between escape characters

1 // The managed type to the unmanaged type
2 inline cstring managetounmanagestr (System: String ^ cmstr)
3 {
4 using namespace system: runtime: interopservices;
5 cstring CSTR = (char *) (MARSHAL: stringtohglobalansi (cmstr). topointer ();
6 return cstr;
7}
8
9 // from unmanaged type to managed type
10 inline system: String ^ unmanagetomanagestr (const cstring & CSTR)
11 {
12 const char * Cc = (lpctstr) CSTR;
13 system: String ^ cmstr = gcnew system: string (CC );
14 return cmstr;
15}

2. Conversion between objects

1 // _ variant_t to object
2 inline system: Object ^ vartoobject (_ variant_t var)
3 {
4 using namespace system: runtime: interopservices;
5 System: intptr ^ pvar = gcnew system: intptr (& var );
6 system: Object ^ OBJ = MARSHAL: getobjectfornativevariant (* pvar );
7 return OBJ;
8}
9
10 // object to _ variant_t
11 inline _ variant_t * objecttovar (System: Object ^ OBJ)
12 {
13 using namespace system: runtime: interopservices;
14 _ variant_t * Vt = new _ variant_t ();
15 system: intptr ^ pvar = (gcnew system: intptr (void *) VT ));
16 MARSHAL: getnativevariantforobject (OBJ, * pvar );
17 return VT;
18}

For more information, see the classes in the. NET library system. runtime. interopservices namespace, especially the marshal class.

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.