Exemple #1
0
 public static extern int SetFunctionPointerString(FunctionPointerType2 pointer);
Exemple #2
0
        static void Main(string[] args)
        {
            //#region PInvoke simpleStruct  2019.2.18
            //Console.ReadLine();
            //SimpleStruct structValue;
            //structValue.intA = 1;
            //structValue.B = 1234.5678 ;
            //Console.WriteLine("[managed]Before SetSimpleStruct, A is" + structValue.intA +
            //    "; B is " + structValue.B);
            //SetSimpleStruct(structValue);
            //Console.WriteLine("[managed]After SetSimpleStruct, A is" + structValue.intA +
            //    "; B is " + structValue.B);
            //Console.ReadLine();

            //#endregion

            //#region PInvoke string  2019.2.18
            //TakesAString("Framework called!");
            //Console.ReadLine();

            //#endregion

            //#region test FireAndForget 2018.7.17
            //_IPortEvents_ReceivedCANMessageEventHandler canServerFireTest = new _IPortEvents_ReceivedCANMessageEventHandler(OnCanMessageReceived);
            //FireAndForget(canServerFireTest, CZCom_MessageType.CZCom_MessageType_CAN29, (short)0,
            //              (short)0, (short)0, (short)0, (short)0, (short)0, "hello");
            //Console.ReadLine();
            //#endregion

            //#region 测试多个给CanServer设置多个Event 2018.7.16
            //_IPortEvents_ReceivedCANMessageEventHandlerArray canServer = new _IPortEvents_ReceivedCANMessageEventHandlerArray(OnCanServerChanged);
            //setFire_ReceivedCANMessage_MulEvent(canServer);

            //_IPortEvents_ReceivedCANMessageEventHandlerArray canServer1 = new _IPortEvents_ReceivedCANMessageEventHandlerArray(OnCanServerChanged1);
            //setFire_ReceivedCANMessage_MulEvent(canServer1);

            //_IPortEvents_ReceivedCANMessageEventHandlerArray canServer2 = new _IPortEvents_ReceivedCANMessageEventHandlerArray(OnCanServerChanged2);
            //setFire_ReceivedCANMessage_MulEvent(canServer2);

            //Can29.CallTimer();
            //Console.ReadLine();

            //#endregion

            //#region String和byte[]的转换
            //string str = "";
            //byte[] byteArray = System.Text.Encoding.Default.GetBytes(str);

            //#endregion

            //#region PInvokeFunction ptr
            //Can29 can29Object = new Can29();
            ////_IPortEvents_ReceivedCANMessageEventHandler ReceivedCANMessage = new _IPortEvents_ReceivedCANMessageEventHandler(OnCanMessageReceived);
            ////can29Object.SetFire_ReceivedCANMessage_CB(ReceivedCANMessage);
            //ReceivedCANMessage += new _IPortEvents_ReceivedCANMessageEventHandlerArray(OnCanMessageReceived);
            //can29Object.SetFire_ReceivedCANMessage_Array(ReceivedCANMessage);

            //#endregion

            //#region PInvokeFunction GetEventPtr, Fail
            ////_IPortEvents_ReceivedCANMessageEventHandler ReceivedCANMessageBack = can29Object.GetReceivedCANMessageEventHandlerFromC();
            ////ReceivedCANMessageBack += new _IPortEvents_ReceivedCANMessageEventHandler(OnCanMessageReceivedBack);

            //Can29.CallTimer();
            //Console.ReadLine();
            //#endregion

            //#region 测试byte[]对应到C++的unsigned char

            //byte[] testbyteArray = new byte[12];

            //string byteString = "";
            //foreach (var item in testbyteArray)
            //{
            //    byteString += item;
            //    byteString += " ";
            //}
            //Console.WriteLine("Before PINVOKE byteString : {0}", byteString);

            ////it will print value in C codes
            //UnsignedArray(testbyteArray.Length, testbyteArray);

            //byteString = ByteArrayToStringConverter1(testbyteArray);
            //Console.WriteLine("After PINVOKE byteString : {0}", byteString);

            //Console.ReadLine();
            //#endregion

            #region 测试byte[]对应到C++的unsigned char, 拷贝越界的情况,会只传部分string
            //byte[] array1 = new byte[20];

            //string string1 = "";
            //foreach (var item in array1)
            //{
            //    string1 += item;
            //    string1 += " ";
            //}
            //Console.WriteLine("Before PINVOKE byteString : {0}", array1);

            //MemcpyArray(array1.Length, array1);

            //string1 = ByteArrayToStringConverter1(array1);
            //Console.WriteLine("After PINVOKE byteString : {0}", string1);

            //Console.ReadLine();
            #endregion

            #region 测试多个out参数的情况
            short  pPoductId     = 0;
            string pName         = "unknown";
            string pSerialNumber = "unknown";
            GetUSBDevicePropertiesByPInvoke(6, out pPoductId, out pName, out pSerialNumber);
            Console.WriteLine("pPoductId: " + pPoductId + ";   pName:" + pName + ";   pSerialNumber:" + pSerialNumber);
            #endregion

            #region ptr to int
            //MTB_DATA listdata = new MTB_DATA();
            //InitialMTBDATA(ref listdata);
            //Console.WriteLine("after cunction, count is set to: {0} ", (int)Marshal.PtrToStructure(new IntPtr(listdata.count.ToInt32()), typeof(int)));
            //Console.ReadLine();
            #endregion

            #region char to ASCII
            //char[] testchar = new char[6] { '0', '9', (char)'.', '0', '0', '6' };
            //string displayString = "";
            //foreach (var item in testchar)
            //{
            //    displayString += (int)item;
            //    displayString += "\n";

            //}

            //Console.WriteLine(displayString);
            //Console.ReadLine();
            #endregion

            #region char[] to byte[]
            //char[] testchar1 = new char[6] { '0', '9', '.', '0', '0', '6' };

            //byte[] result = CharArrayToAsciiConverter(testchar1);

            //string displayString = "";
            //foreach (var item in result)
            //{
            //    displayString += item;
            //    displayString += "\n";

            //}

            //Console.WriteLine(displayString);
            //Console.ReadLine();
            #endregion

            #region byte[] to char[]
            byte[] byteBuf = new byte[3] {
                20, 16, 21
            };
            char[] testchar1     = ByteArrayToCharArrayConverter(byteBuf);
            string displayString = "";
            foreach (var item in testchar1)
            {
                displayString += item.ToString();
                displayString += "\n";
            }

            Console.WriteLine(displayString);
            Console.ReadLine();
            #endregion

            #region PInvokeSimple
            int a = 1;
            int b = 2;
            int c = Divide(b, a);
            int d = Add(a, b);

            Console.WriteLine(c);
            Console.WriteLine(d);
            #endregion

            #region ptr *double,使用 PInvoke 封送嵌入式指针
            double[] parray = new double[10];
            Console.WriteLine("[managed] count = {0}", parray.Length);

            Random r = new Random();
            for (int i = 0; i < parray.Length; i++)
            {
                parray[i] = r.NextDouble() * 100.0;
                Console.WriteLine("array[{0}] = {1}", i, parray[i]);
            }

            MListStruct list;
            int         size = Marshal.SizeOf(typeof(double));

            list.count = parray.Length;
            list.item  = Marshal.AllocCoTaskMem(size * parray.Length);

            for (int i = 0; i < parray.Length; i++)
            {
                IntPtr t = new IntPtr(list.item.ToInt32() + i * size);
                Marshal.StructureToPtr(parray[i], t, false);
            }

            TakesListStruct(list);

            double[] anotherParray = new double[10];
            for (int i = 0; i < anotherParray.Length; i++)
            {
                anotherParray[i] = (double)Marshal.PtrToStructure(new IntPtr(list.item.ToInt32() + i * size), typeof(double));
            }


            Marshal.FreeCoTaskMem(list.item);
            #endregion

            #region ptr *double,使用 PInvoke 封送使用 PInvoke 封送数组
            int[] e = new int[3] {
                11, 33, 55
            };
            Console.WriteLine("[Before P/Invoke function]");
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine("{0} = {1}", i, e[i]);
            }

            TakesAnArray(3, e);

            Console.WriteLine("[After P/Invoke function]");
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine("{0} = {1}", i, e[i]);
            }

            #endregion

            #region 使用 PInvoke 封送函数指针
            //This is OK
            FunctionPointerType1 functionB = new FunctionPointerType1(functionA);
            SetFunctionPointer(functionB);

            FunctionPointerType2 functionC = new FunctionPointerType2(functionCC);
            SetFunctionPointerString(functionC);
            #endregion

            Console.ReadKey();
        }