This topic demonstrates one facet of C++ interoperability. For more information, see Using C++ Interop (Implicit PInvoke).
The following code examples use the
Example
The following example demonstrates passing a structure from a managed to an unmanaged function, both by value and by reference. Because the structure in this example contains only simple, intrinsic data types (see
В | ![]() |
---|---|
// PassStruct1.cpp // compile with: /clr #include <stdio.h> #include <math.h> using namespace System; using namespace System::Runtime::InteropServices; #pragma unmanaged struct Location { int x; int y; }; double GetDistance(Location loc1, Location loc2) { printf_s("[unmanaged] loc1(%d,%d)", loc1.x, loc1.y); printf_s(" loc2(%d,%d)\n", loc2.x, loc2.y); double h = loc1.x - loc2.x; double v = loc1.y = loc2.y; double dist = sqrt( pow(h,2) + pow(v,2) ); return dist; } void InitLocation(Location* lp) { printf_s("[unmanaged] Initializing location...\n"); lp->x = 50; lp->y = 50; } #pragma managed int main() { Location loc1; loc1.x = 0; loc1.y = 0; Location loc2; loc2.x = 100; loc2.y = 100; double dist = GetDistance(loc1, loc2); Console::WriteLine("[managed] distance = {0}", dist); Location loc3; InitLocation(&loc3); Console::WriteLine("[managed] x={0} y={1}", loc3.x, loc3.y); } |
The following example demonstrates passing a structure from an unmanaged to a managed function, both by value and by reference. Because the structure in this example contains only simple, intrinsic data types (see
В | ![]() |
---|---|
// PassStruct2.cpp // compile with: /clr #include <stdio.h> #include <math.h> using namespace System; // native structure definition struct Location { int x; int y; }; #pragma managed double GetDistance(Location loc1, Location loc2) { Console::Write("[managed] got loc1({0},{1})", loc1.x, loc1.y); Console::WriteLine(" loc2({0},{1})", loc2.x, loc2.y); double h = loc1.x - loc2.x; double v = loc1.y = loc2.y; double dist = sqrt( pow(h,2) + pow(v,2) ); return dist; } void InitLocation(Location* lp) { Console::WriteLine("[managed] Initializing location..."); lp->x = 50; lp->y = 50; } #pragma unmanaged int UnmanagedFunc() { Location loc1; loc1.x = 0; loc1.y = 0; Location loc2; loc2.x = 100; loc2.y = 100; printf_s("(unmanaged) loc1=(%d,%d)", loc1.x, loc1.y); printf_s(" loc2=(%d,%d)\n", loc2.x, loc2.y); double dist = GetDistance(loc1, loc2); printf_s("[unmanaged] distance = %f\n", dist); Location loc3; InitLocation(&loc3); printf_s("[unmanaged] got x=%d y=%d\n", loc3.x, loc3.y); return 0; } #pragma managed int main() { UnmanagedFunc(); } |