• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

.Net C# 与 非托管C++互操作性

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

.Net C# 与 非托管C++互操作性

(一).Net互操性调试环境配置

1.1  将C++ DLL文件输出到主EXE所在目录

1.2  配置C++ DLL项目属性的命令调试器类型两项

1.3   配置.NET Winform项目属性调试选项。一定要选择“启用本地代码调试”

调试如下图


(二)互操作性的事件处理

1.1   C++ DLL代码如下:

代码// Bsr.Hardware.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
typedef void(*Action)();
typedef void (__stdcall *LPFUN)(int);   
_declspec(dllexport) int add(int a, int b, )
{
	int ret = a + b;
	if (ball != nullptr) {
		ball(ret);
	}
	
	return ret+100;
}

_declspec(dllexport) int add1(int a, int b)
{
	int ret = a + b;

	return ret;
}

1.2   C#调用代码

代码public partial class Form1 : Form
{
    public delegate void AddEvent(int x);  
    [DllImport("Bsr.Hardware.DLL", EntryPoint = "add", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    public static extern int add(int a, int b, AddEvent act);
    [DllImport("Bsr.Hardware.DLL", EntryPoint = "add1", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    public static extern int add1(int a, int b);
   
    public static void ActEvent(int x)
    {
        MessageBox.Show(x.ToString());

    }

    private void button1_Click(object sender, EventArgs e)
    {
        AddEvent ev = new AddEvent(ActEvent);
        int ret = add(100, 100, ev);
        int y = 100;
        MessageBox.Show("f1=" + ret.ToString());
    }
}

 

 

https://blog.csdn.net/yanlinembed/article/details/78920276

posted @ 2019-03-15 19:48 李华丽 阅读(...) 评论(...) 编辑 收藏

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
关于C#正则表达式MatchCollection类的总结发布时间:2022-07-10
下一篇:
C#生成txt文件,并将数据写入进去(分列)发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap