Ejemplo n.º 1
0
        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;
        }
Ejemplo n.º 2
0
 public static extern bool Ch7ModifyCh7PointerArray(Ch7PointerArray value);