.h文件如下
#include "StdAfx.h"
#ifdef RTWRE_EXPORTS
#define RTWRE_API __declspec(dllexport)
#else
#define RTWRE_API __declspec(dllimport)
#ifdef _DEBUG
#pragma comment(lib,"FunctionDll.lib")
#else
#pragma comment(lib,"FunctionDll.lib")
#endif
#endif
//int MarkID——标志点ID号,1——角点,2——边点
//向量 double Va, double Vb, double Vc
typedef void (CALLBACK * CHECKCLCFUNCTION) (int MarkID, double Va, double Vb,double Vc);
CHECKCLCFUNCTION m_pCheckFunction; //回调函数指针
//返回input+2
extern "C" RTWRE_API int FDllTest(int input);
//设置回调函数
/*
SetCheckFunctionPoint
功 能
设置回调函数。
格 式
void SetCheckFunctionPoint(CHECKFUNCTION1 pCheckFuntion);
参 数
CHECKFUNCTION1 pCheckFuntion 回调函数名,具体定义见回调函数说明
返 回 值
无
*/
extern "C" RTWRE_API void SetCheckFunctionPoint(CHECKCLCFUNCTION pCheckFuntion);
具体调用的函数如下
C#代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace CSharpCallBackC
{
//FunctionDll.dll
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void ControlData(int a, double b,double c,double d);
[DllImport(@"FunctionDll.dll", EntryPoint = "SetCheckFunctionPoint", CallingConvention = CallingConvention.Cdecl)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
主要问题在于最后声明函数的时候不会写,在 public static extern 语句后面总会报错,还有就是如果使用结构体应该怎么写。