博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.Net C# 与 非托管C++互操作性
阅读量:4493 次
发布时间:2019-06-08

本文共 1368 字,大约阅读时间需要 4 分钟。

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

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

image

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

image

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

image

调试如下图

3333

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

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://www.cnblogs.com/lihuali/p/10539152.html

你可能感兴趣的文章
CI Weekly #21 | iOS 持续集成快速入门指南
查看>>
Jquery获取输入框属性file,ajax传输后端,下载图片
查看>>
docker运行环境安装-后续步骤(二)
查看>>
Python学习——02-Python基础——【3集合与函数】
查看>>
NPOI导出excel表格应用
查看>>
tensorflow从入门到放弃-0
查看>>
解锁scott用户
查看>>
多态的理解
查看>>
AspNet Core 发布到Linux系统和发布IIS 注意项
查看>>
Windows添加.NET Framework 3.0 NetFx3 失败 - 状态为:0x800f0950
查看>>
隐藏显示终端的光标(shell echo,linux c printf)
查看>>
SQL Server 存储过程
查看>>
JSP 标准标签库(JSTL)(JSP Standard Tag Library)
查看>>
导入项目遇到的问题: Some projects cannot be imported because they already exist in the workspace....
查看>>
华为:字符集合
查看>>
用Okhttp框架登录之后的Cookie设置到webView中(转)
查看>>
Java_Activiti5_菜鸟也来学Activiti5工作流_之入门简单例子(一)
查看>>
设计模式(一)工厂模式Factory(创建型)
查看>>
linux中安装软件的集中方法
查看>>
java获取当前路径的几种方法
查看>>