Skip to content

Latest commit

 

History

History
85 lines (71 loc) · 2.17 KB

how-to-modify-reference-class-in-a-native-function.md

File metadata and controls

85 lines (71 loc) · 2.17 KB
descriptiontitlems.customms.datehelpviewer_keywordsms.assetid
Learn more about: How to: Modify Reference Class in a Native Function
How to: Modify Reference Class in a Native Function
get-started-article
11/04/2016
platform invoke, reference class
reference types, modifying in a C++ native function
c701145b-62a0-4c4b-b32a-db8d69a59720

How to: Modify Reference Class in a Native Function

You can pass a reference class with a CLR array to a native function, and modify the class, using PInvoke services.

Examples

Compile the following native library.

// modify_ref_class_in_native_function.cpp// compile with: /LD #include<stdio.h> #include<windows.h>structS { wchar_t* str; int intarr[2]; }; extern"C" { __declspec(dllexport) int bar(S* param) { printf_s("str: %S\n", param->str); fflush(stdin); fflush(stdout); printf_s("In native: intarr: %d, %d\n", param->intarr[0], param->intarr[1]); fflush(stdin); fflush(stdout); param->intarr[0]=300;param->intarr[1]=400; return0; } };

Compile the following assembly.

// modify_ref_class_in_native_function_2.cpp// compile with: /clrusingnamespaceSystem;usingnamespaceSystem::Runtime::InteropServices; [StructLayout(LayoutKind::Sequential, CharSet = CharSet::Unicode)] ref classS { public: [MarshalAsAttribute(UnmanagedType::LPWStr)] String ^ str; [MarshalAsAttribute(UnmanagedType::ByValArray, ArraySubType=UnmanagedType::I4, SizeConst=2)] array<Int32> ^ intarr; }; [DllImport("modify_ref_class_in_native_function.dll", CharSet=CharSet::Unicode)] intbar([In][Out] S ^ param); intmain() { S ^ param = gcnew S; param->str = "Hello"; param->intarr = gcnew array<Int32>(2); param->intarr[0] = 100; param->intarr[1] = 200; bar(param); // Call to native functionConsole::WriteLine("In managed: intarr: {0}, {1}", param->intarr[0], param->intarr[1]); }
str: Hello In native: intarr: 100, 200 In managed: intarr: 300, 400 

See also

Using C++ Interop (Implicit PInvoke)

close