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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值