public static void Ch7ModifyCh7Pointer() { //create objects to use var value = new Ch7Pointer(); var ch7StringAW = new Ch7StringAW { asValue = "BBBB".ToArray(), wsValue = "你你你你".ToArray(), }; int iValue = 123; //alloc memory from process heap value.ch7StringAWPtr = Marshal.AllocHGlobal(Marshal.SizeOf(ch7StringAW)); value.iValuePtr = Marshal.AllocHGlobal(Marshal.SizeOf(iValue)); //copy managed object memory to native heap Marshal.StructureToPtr(ch7StringAW, value.ch7StringAWPtr, false); Marshal.StructureToPtr(iValue, value.iValuePtr, false); //call method if (Ch7Native.Ch7ModifyCh7Pointer(value)) { //copy native heap to managed object memory ch7StringAW = (Ch7StringAW)Marshal.PtrToStructure(value.ch7StringAWPtr, typeof(Ch7StringAW)); iValue = (int)Marshal.PtrToStructure(value.iValuePtr, typeof(int)); } //do not forgot free native heap memory Marshal.FreeHGlobal(value.ch7StringAWPtr); Marshal.FreeHGlobal(value.iValuePtr); value.ch7StringAWPtr = IntPtr.Zero; value.iValuePtr = IntPtr.Zero; }
public static extern bool Ch7ModifyCh7Pointer([In, Out] Ch7Pointer value);