C++ C#互操作

下面是互操作的實例代碼:
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace testconsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("----Show1----");
            Show1();
            Console.WriteLine("----Show2----");
            Show2(Enumerable.Range(1, 10).ToArray(), 10);
            Console.WriteLine("----Show3----");
            Show3(Console.WriteLine);
            Console.WriteLine("----Show4----");
            var data = new Show4Data {Index = 1, Data = new[] {1, 2, 3}};
            Show4(ref data);

            Console.WriteLine("----Show5----");
            var data5_data = new[] {1, 2, 3};

            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Int32)) * data5_data.Length);
            Marshal.Copy(data5_data, 0, ptr, data5_data.Length);
            var data5 = new Show5Data {DataLen = 3, DataPtr = ptr, Index = 1};
            Show5(ref data5);

            Marshal.FreeHGlobal(ptr);

            Console.Read();
        }

        [DllImport("testsdk.dll")]
        public static extern int Show1();

        [DllImport("testsdk.dll")]
        public static extern int Show2([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]
            Int32[] arr, int len);

        public delegate void Delegate_Func1(Int32 val);

        [DllImport("testsdk.dll")]
        public static extern int Show3(Delegate_Func1 func);

        //----傳遞包含定長數組的結構體----
        public struct Show4Data
        {
            public Int32 Index;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
            public Int32[] Data;
        }

        [DllImport("testsdk.dll")]
        public static extern int Show4(ref Show4Data data);

        public struct Show5Data
        {
            public Int32 Index;
            public IntPtr DataPtr;
            public Int32 DataLen;
        }

        [DllImport("testsdk.dll")]
        public static extern int Show5(ref Show5Data data);
    }
}

C++

// testsdk.cpp : 定義 DLL 應用程序的導出函數。
//

#include "stdafx.h"

#define API_DECLSPEC extern "C" _declspec(dllexport) 

API_DECLSPEC void _stdcall Show1()
{
	cout << "Show1" << endl;
}

API_DECLSPEC void _stdcall Show2(INT32 *arr, int len)
{
	cout << "Show2" << endl;
	for (int i = 0; i < len; i++)
	{
		cout << "data " << i << " : " << arr[i] << endl;
	}
}


typedef void(_stdcall *Delegate_Func1)(INT32);

//傳遞委託
API_DECLSPEC void _stdcall Show3(Delegate_Func1 delegate_func)
{
	delegate_func(5);
}

struct Show4Data
{
	INT32 index;
	INT32 data[3];
};

//傳遞結構體
API_DECLSPEC void _stdcall Show4(const Show4Data* data)
{
	for (int i = 0; i < 3; i++)
		cout << data->data[i] << endl;
}

struct Show5Data
{
	INT32 index;
	INT32* data;
	INT32 dataLen;
};

//傳遞結構體
API_DECLSPEC void _stdcall Show5(const Show5Data* data)
{
	for (int i = 0; i < data->dataLen; i++)
		cout << data->data[i] << endl;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章