首页 > 编程语言 >C++定义函数指针,回调C#

C++定义函数指针,回调C#

时间:2024-07-02 17:19:52浏览次数:1  
标签:C# System C++ int delegate func using 函数指针 public

C++定义函数指针。

typedef int (__stdcall * delegate_func)(int a, int b);

暴露接口:int __stdcall CPPcallCSharp(delegate_func func);

方法实现:int __stdcall CPPcallCSharp(delegate_func func) { return func(1,2); }

头文件calculator.h

#ifndef LIB_CALCULATOR_H
#define LIB_CALCULATOR_H


typedef int(__stdcall *delegate_func)(int a, int b);

int __stdcall CPPcallCSharp(delegate_func func);

#endif

源文件calculator.h

// calculator.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "calculator.h"

int __stdcall CPPcallCSharp(delegate_func func) 
{
    return func(1, 2);
}

C#定义委托。

public delegate int delegate_func(int a, int b);

添加方法:public int callBackFunc(int a, int b) { Return a+b; }

C#中实现C++接口:

[DllImport("CppLib.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]

public static extern int CPPcallCSharp(delegate_func func);

C#中调用C++接口,实现回调

public event delegate_func callBack_event =null; callBack_event += new delegate_func(callBackFunc); CPPcallCSharp(callBack_event); 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace csharpCallCpp
{

    public partial class Form1 : Form
    {
        public delegate int delegate_func(int a,int b);

     //导入C++接口
        [DllImport("calculator.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
        public static extern int CPPcallCSharp(delegate_func func);

        public Form1()
        {
            InitializeComponent();
        }

        public  event delegate_func callBack_event =null;
        private void button1_Click(object sender, EventArgs e)
        {
            callBack_event += new delegate_func(callBackFunc);//指定回调方法callBackFunc
            int iret=CPPcallCSharp(callBack_event);//执行该代码时,方法callbackFunc被执行,且返回(1+2)
            MessageBox.Show("function return:" + iret);
        }
        public  int callBackFunc(int a, int b)
        {
            return a+b;
        }
    }
}

实现效果

 

标签:C#,System,C++,int,delegate,func,using,函数指针,public
From: https://www.cnblogs.com/ZM191018/p/18280207

相关文章

  • 《DNK210使用指南 -CanMV版 V1.0》第七章 基于CanMV的MicroPython语法开发环境搭建
    第七章基于CanMV的MicroPython语法开发环境搭建1)实验平台:正点原子DNK210开发板2)章节摘自【正点原子】DNK210使用指南-CanMV版V1.03)购买链接:https://detail.tmall.com/item.htm?&id=7828013987504)全套实验源码+手册+视频下载地址:http://www.openedv.com/docs/boards/k210/......
  • mac的其他内存怎么清理,全方位清理Mac其他内存必备
    对于很多职场朋友来说,一台mac电脑上标配,它的高效性能与优秀的交互体验总能让工作变得更加得心应手,但有时候也会发现,随着时间的推移,Mac运行变得缓慢,处理工作不似最开始的丝滑,那这可能是因为你的mac内存占用过多。那么mac的其他内存怎么清理呢,今天一文带大家搞定mac内存清理。......
  • 音频---查看声卡和pcm设备
    目录查看声卡和pcm设备一.查看声卡和PCM设备的信息相关指令二.音频设备命名规则查看声卡和pcm设备声卡和pcm设备的关系:https://blog.csdn.net/Ciellee/article/details/101752604一.查看声卡和PCM设备的信息相关指令1.查看当前的声卡:cat/proc/asound/cards2.查看pcm设......
  • C#基础2024.07.02
    目录1.变量的作用域有哪些?2.成员变量和静态变量的区别?成员变量(实例变量)静态变量(类变量)3.利用递归,写个文件目录遍历,打印出文件名、扩展名、文件大小4.简述访问修饰符有几种,各有什么不同?(1)public(2)private(3)protected(5)internal(6)protectedinternal5.重点比较public、pr......
  • wpf关于Resource,Style的定义与引用,滑动按钮
    当我们使用wpf框架去搭建自己的程序时,一般都会重写wpf原生的一些样式,以达到软件风格的统一于美化,以下介绍一下常见的几种添加Style的方式我们以一个滑动按钮为例1.对当前控件的样式进行更改<ToggleButton><ToggleButton.Style><StyleTarg......
  • LeetCode 算法:二叉树展开为链表 c++
    原题链接......
  • 单元测试@BeforeAll和@BeforeEach的区别
    @BeforeAll和@BeforeEach是JUnit5中的注解,用于在执行测试方法之前执行某些操作。@BeforeAll注解表示在所有测试方法执行之前执行一次,并且必须是静态方法。通常用于初始化测试环境,例如启动一个数据库连接池或者读取测试数据文件。@BeforeEach注解表示在每个测试方法执......
  • 「杂题乱刷」AT_abc360_d
    题目链接AT_abc360_d(luogu)AT_abc360_d(atcoder)解题思路一个性质是,往左边走的蚂蚁无论怎么样都追不到左边的蚂蚁,而往右边走的蚂蚁无论怎么样都追不上右边的蚂蚁。因此我们考虑将蚂蚁分为往左往右走的两堆。发现对于每个蚂蚁都能走过一段区间,因此直接二分将右端点减去左......
  • 把采集的PCM音频数据填充到AVFrame中
    目录1.AVFrame结构体中部分音频参数说明2.和实际录音时音频属性的对应关系1.AVFrame结构体中部分音频参数说明typedefstructAVFrame{#defineAV_NUM_DATA_POINTERS8uint8_t*data[AV_NUM_DATA_POINTERS];//指向音频数据的指针数组intlinesize[AV_NUM_DATA_POI......
  • A tour of C++ 读书笔记
    第一章:C++只是个编译型语言,需要源文件编译成目标文件,再通过链接各种库到可执行文件1.6常量  const  constexpr这个代表是要在编译的时候估值,性能会有所增加吧2.4联合体(union)  联合体所有的成员都是分配在同一地址上面,所以联合体所占的空间是跟其自身内部成员所......