发送回调函数从c#到C结构中的dll
Send callback function from C# to C dll in struct
我试图发送一个结构体,其中包括从c#回调函数到c++本地DLL的引用,我正在做什么有什么问题>?,结构体通过!,所有的回调引用都不是空的,但当调用它时,我没有从DLL到达c#函数:(
class Native_Wrraper
{
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int LogMessageCallback(string mMyStruct);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int ActionCompleteCallback(int actionId, int actionResult);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate string GetFilePathCallback(string filter);
其结构:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct InitCallBacksStruct
{
[MarshalAs(UnmanagedType.FunctionPtr)]
public LogMessageCallback logMessageCallback;
[MarshalAs(UnmanagedType.FunctionPtr)]
public ActionCompleteCallback actionCompleteCallback;
[MarshalAs(UnmanagedType.FunctionPtr)]
public GetFilePathCallback getPath;
//UpdaterCallback updaterCallback;
};
[DllImport("_Wrraper.dll", CharSet = CharSet.Unicode)]
public static extern int InitDll(InitCallBacksStruct mMyStruct);
[DllImport("_Wrraper.dll", CharSet = CharSet.Unicode)]
public static extern int StartTestAPIDll(string TestHeader, int someNumber);
这里是DLL应该调用的回调函数(enter)
public static int FuncLogMessageCallback(string mMyStruct)
{
return 1;
}
public static int FuncActionCompleteCallback(int actionId, int actionResult)
{
return 1;
}
public static string FuncGetFilePathCallback(string filter)
{
return "The Call back worked!";
}
}
测试按钮:
private void btnTest_Click(object sender, EventArgs e)
{
Native_Wrraper.InitCallBacksStruct str = new Native_Wrraper.InitCallBacksStruct();
str.actionCompleteCallback = new Native_Wrraper.ActionCompleteCallback(Native_Wrraper.FuncActionCompleteCallback);
str.getPath = new Native_Wrraper.GetFilePathCallback(Native_Wrraper.FuncGetFilePathCallback);
str.logMessageCallback = new Native_Wrraper.LogMessageCallback(Native_Wrraper.FuncLogMessageCallback);
Native_Wrraper.InitDll(str);
Native_Wrraper.StartTestAPIDll(DateTime.Now.ToShortTimeString(), 0xEE);
}
:
结构:#define DLL __declspec(dllexport)
typedef void (__stdcall * ActionCompleteCallback)(int actionId, int actionResult);
typedef char* (__stdcall * GetFilePathCallback)(char* filter);
typedef void (__stdcall * LogMessageCallback)(char* message);
//This structure include all the references to a call backs functions
struct InitCallBacksStruct
{
LogMessageCallback logMessageCallback;
ActionCompleteCallback actionCompleteCallback;
GetFilePathCallback getPath;
};
#endif
API: #ifdef __cplusplus
extern "C"
{
#endif
#define DLL __declspec(dllexport)
DLL int InitDll(InitCallBacksStruct mMyStruct);
DLL int StartTestAPIDll( __in LPWSTR TestHeader, int someNumber);
#ifdef __cplusplus
}
#endif
从DLL调用的例子:
if(this->mInitCallBacksStruct.actionCompleteCallback)
{
this->mInitCallBacksStruct.actionCompleteCallback(someNumber,0xCC);
}
对不起,伙计们,代码工作完美!
问题是c#中的断点!!:)
添加对话框。
所以这将是一个模板和/或答案的人谁正在寻找一个解决方案:
如何从c#发送回调函数引用列表,使用结构内部的一个函数调用到本机DLL。
ONLY,请注意,你应该在c#中全局地保持回调,最好是静态的,而不是在btnTest_Click函数中!为了确保引用不会消失,意味着对回调函数的引用应该是全局变量或静态变量,例如:
static Native_Wrraper.ActionCompleteCallback refFunction = new Native_Wrraper.ActionCompleteCallback(Native_Wrraper.FuncGetFilePathCallback);
然后发送到DLL
str.actionCompleteCallback = refFunction
祝你好运!约瑟夫。
相关文章:
- C++结构到德尔福记录dll调用
- PowerShell 使用结构类型参数调用 C++ DLL 的导出函数
- 对需要双精度数组和结构作为输入C++ DLL 函数的 C# 调用
- 如何将结构从 C# 传递到C++ DLL?
- 在C 功能执行后,使用GCHANDLE CRALSES将大型结构从C#Unity脚本传递到C DLL
- 如何在 LabVIEW 的 DLL 中打开 OpenCV 结构化边缘检测器的模型文件?
- C++从 DLL 创建结构
- 如何将struct从c#管理到c 不受管理的dll并获得结构
- 使用 char* 将结构传递给 C# 中的 C++ dll 函数
- DLL导入纯虚拟结构和unique_ptr由于Compressed_pair而无法编译
- 如何从Java中的C DLL加载和使用结构和功能
- ATL结构从dll上断开COM对象
- 使用P/Invoke从MFC DLL函数中读取VB.NET中的结构化数据
- 将嵌套结构从 C# 编组到C++ DLL
- 将结构(或类)从C DLL传递到C#(Unity 3D)
- 将 char*[] 从 C++ DLL 结构体传递到 C#
- visual如何在c++中使用dll中的结构
- 将结构从 c++ dll 返回到 Python
- 使用Node.js中包含char数组的结构调用C++dll
- 将结构的列表从C#应用程序传递到C++DLL