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.