日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

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

發布時間:2023/12/13 综合教程 30 生活家
生活随笔 收集整理的這篇文章主要介紹了 .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);   //定義一個函數指針,此處必須要定義一個函數指針,如果定義為add(int a,int b,void(*ball)(int))這種方式,則C#回調時將無法返回到C++ DLL中的函數調用。
_declspec(dllexport) int add(int a, int b, LPFUN ball=nullptr)
{
	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);  //定義委托來與DLL中函數指針匹配
    [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

總結

以上是生活随笔為你收集整理的.Net C# 与 非托管C++互操作性的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。