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 void Ch7ModifyCh7StringAW() { var value = new Ch7StringAW { asValue = "BBBB".PadRight(4, (char)0).ToArray(), wsValue = "你你你你".PadRight(4, (char)0).ToArray(), }; bool ret = Ch7Native.Ch7ModifyCh7StringAW(ref value); }
public static void Ch7ModifyCh7PointerArray2() { //create objects to use var value = new Ch7PointerArray(); int[] iValues = new int[] { 123, 123, 123, 123 }; Ch7StringAW[] ch7StringAWs = new Ch7StringAW[4]; for (int i = 0; i < ch7StringAWs.Length; i++) { ch7StringAWs[i] = new Ch7StringAW { asValue = "BBBB".ToArray(), wsValue = "你你你你".ToArray(), }; } //alloc block memory from process heap IntPtr ptrIValues = Marshal.AllocHGlobal(4 * 4); IntPtr ptrCh7StringAW = Marshal.AllocHGlobal(4 * Marshal.SizeOf(typeof(Ch7StringAW))); //cal each object address for (int i = 0; i < 4; i++) { value.iValuePtrArray[i] = ptrIValues + i * 4; value.pchStringAWPtrArray[i] = ptrCh7StringAW + i * Marshal.SizeOf(typeof(Ch7StringAW)); } //copy managed object memory to native heap for (int i = 0; i < 4; i++) { Marshal.StructureToPtr(iValues[i], value.iValuePtrArray[i], false); Marshal.StructureToPtr(ch7StringAWs[i], value.pchStringAWPtrArray[i], false); } if (Ch7Native.Ch7ModifyCh7PointerArray(value)) { for (int i = 0; i < 4; i++) { ch7StringAWs[i] = (Ch7StringAW)Marshal.PtrToStructure(value.pchStringAWPtrArray[i], typeof(Ch7StringAW)); iValues[i] = (int)Marshal.PtrToStructure(value.iValuePtrArray[i], typeof(int)); } } Marshal.FreeHGlobal(ptrIValues); Marshal.FreeHGlobal(ptrCh7StringAW); for (int i = 0; i < 4; i++) { value.iValuePtrArray[i] = IntPtr.Zero; value.pchStringAWPtrArray[i] = IntPtr.Zero; } ptrCh7StringAW = IntPtr.Zero; ptrCh7StringAW = IntPtr.Zero; }
public static void Ch7ModifyCh7StructArray() { var value = new Ch7StructArray(); bool ret = Ch7Native.Ch7ModifyCh7StructArray(value); }
public static void Ch7ModifyCh7StringPtr() { var value = new Ch7StringPtr(); bool ret = Ch7Native.Ch7ModifyCh7StringPtr(value); }
public static void Ch7ModifyCh7CharAW() { var value = new Ch7CharAW(); bool ret = Ch7Native.Ch7ModifyCh7CharAW(value); }
public static void Ch7ModifyCh7Basic() { var value = new Ch7Basic(); bool ret = Ch7Native.Ch7ModifyCh7Basic(value); }