Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将带参数的c#回调方法传递给c++;dll导致System.ExecutionEngineeException_C#_C++_Dllimport - Fatal编程技术网

将带参数的c#回调方法传递给c++;dll导致System.ExecutionEngineeException

将带参数的c#回调方法传递给c++;dll导致System.ExecutionEngineeException,c#,c++,dllimport,C#,C++,Dllimport,我正在使用一个c++dll来做一些后台计算,并试图让它向我调用的ccode报告进度 为此,我注册了一个回调方法,该方法接受StringBuilder作为参数(在web上发现这是一种正确的方法) 这是我的C++代码: // -------------------------------------------- // ----------------- C++ CODE ----------------- // ------------------------------------------

我正在使用一个
c++
dll来做一些后台计算,并试图让它向我调用的
c
code报告进度

为此,我注册了一个回调方法,该方法接受
StringBuilder
作为参数(在web上发现这是一种正确的方法)

这是我的C++代码:

// --------------------------------------------
// ----------------- C++ CODE ----------------- 
// --------------------------------------------
// ----------------- dll api methods
// a custom class to contain some progress report stuff... basically, most important is
// that it contains the callback as ProgressCallback _callback;
CustomEventHandler*  _eventHandler = NULL;

// definition of the callback type
typedef void(__stdcall* ProgressCallback)(char* log);

// method to register the callback method
int __stdcall SetCallbackFunction(ProgressCallback callback) {
    // from https://stackoverflow.com/a/41910450/2490877
    #pragma EXPORT_FUNCTION
    // I encapsulated the callback into a custom class
    _eventHandler = new CustomEventHandler();
    _eventHandler->setEventHandler(callback);
    // test all is ok => no problem at this stage, all works great, the
    // passed-in callback is called with correct message. 
    logToCallback("All is ok while testing the method. So far so good!!");
    return 0;
}

// the long and slow method (note that I might call it several times from c# during the
// one run
int __stdcall DoLooongStuff() {
    // from https://stackoverflow.com/a/41910450/2490877
    #pragma EXPORT_FUNCTION
    // ------ this is a LOOOONG method that regualrly logs stuff via the callback,
    // here an example....
    char buf[1000];
    sprintf_s(buf, "This is a sample progress log with some formats :%i %i %g", 1, 2, 3.1415);
    logToCallback(buf);
    // --- the above works a few times without any problem
    return 0;
}

//--- this is a static method I use to send progress messages back
static void logToCallback(char* message) {
    if (_eventHandler) {
        _eventHandler->logToCallback(message);
    }
}

// --------------- CustomEventHandlerClass
// ------- class declaration ------
class CustomEventHandler {
    public:
        void setEventHandler(ProgressCallback callback);
        void logToCallback(char* message);
    protected:
        ProgressCallback _callback;
}

// ----- class implementation
// set the callback method
void CustomEventHandler::setEventHandler(ProgressCallback callback) {
    _callback = callback;
}
void CustomEventHandler::logToCallback(char* message) {
    if (_callback) {
        _callback(message); // <========= this is where the debugger stops:
        // no more info than the annoying System.ExecutionEngineException...
        // I've tried to pass a constant message like "1234" but got the same issue...
        //_callback("1234");
        // if however I remove the call to the callback, I don't get errors 
        // (I know this doesn't mean I don't have any...)
    }
}
//--------------------------------------------
/------------C++代码------------------------------------------------------------------------------------------------------------------
// --------------------------------------------
//--------------dll api方法
//包含某些进度报告内容的自定义类。。。基本上,最重要的是
//它包含作为ProgressCallback\u回调的回调;
CustomEventHandler*\u eventHandler=NULL;
//回调类型的定义
typedef void(u stdcall*ProgressCallback)(char*log);
//方法来注册回调方法
int\uu stdcall setcallback函数(ProgressCallback){
//从https://stackoverflow.com/a/41910450/2490877
#pragma导出函数
//我将回调封装到一个自定义类中
_eventHandler=新的CustomEventHandler();
_eventHandler->setEventHandler(回调);
//测试一切正常=>在这个阶段没有问题,一切都很好
//使用正确的消息调用传入的回调。
logToCallback(“测试方法时一切正常,到目前为止还不错!!”;
返回0;
}
//长而慢的方法(注意,在测试过程中,我可能会多次从c#调用它)
//一次运行
int_uustdcall DoLooongStuff(){
//从https://stackoverflow.com/a/41910450/2490877
#pragma导出函数
//----这是一个looong方法,通过回调定期记录内容,
//这里有一个例子。。。。
char-buf[1000];
sprintf_s(buf,“这是一个具有某些格式的示例进度日志:%i%i%g”,1、2、3.1415);
对数回拨(buf);
//---上述方法可以正常工作几次
返回0;
}
//---这是我用来发回进度消息的静态方法
静态无效logToCallback(字符*消息){
if(_eventHandler){
_eventHandler->logToCallback(消息);
}
}
//------CustomEventHandlerClass
//------类别声明------
类CustomEventHandler{
公众:
void setEventHandler(ProgressCallback回调);
无效logToCallback(字符*消息);
受保护的:
ProgressCallback\u callback;
}
//----类实现
//设置回调方法
void CustomEventHandler::setEventHandler(ProgressCallback回调){
_回调=回调;
}
void CustomEventHandler::logToCallback(char*消息){
如果(_回调){
_回调(消息);//太好了,它能工作!
SetCallbackFunction_Dll=GetDelegate(“SetCallbackFunction”);
//在DLL中设置回调,成功调用委托一次。。。
SetCallbackFunction(cSharpCallback);
等待doTask();
}
异步任务点任务(){
//启动长dll方法,该方法触发回调以监视进度
while(someConditionIsMet){
doLoongStuff();//=>使用已注册的回调调用dll!
}
}
//实际回调
布尔cSharpCallback(StringBuilder strBuilder){
//这会被成功调用几次,并显示预期的消息!
Console.WriteLine(strBuilder.ToString());
返回true;
}
我已经搜索了不同的线程以找出错误。我有一个错误,因为“buf”大小太小,所以我只是确保它足够大。我还测试了“callback”总是指向同一个地方(它是!)


我没有搜索选项,任何帮助都将不胜感激。请注意,我是dll集成、编组等方面的专家,我将参考资料放在我找到的提示中!

我找到了问题的答案,感谢:

为了使非托管函数指针保持活动状态(防止GC),需要在变量中保存委托的实例

因此,修改后的代码只有C语言#

/--------------以前的代码
异步任务loadDllMethods(){
//加载代理=>很好,它可以工作!
SetCallbackFunction_Dll=GetDelegate(“SetCallbackFunction”);
//在DLL中设置回调,成功调用委托一次。。。
SetCallbackFunction(cSharpCallback);
等待doTask();
}
//------工作代码!!!!
//添加静态引用。。。。
静态CallbackFunction _callbackInstance=newcallbackfunction(cSharpCallback);//太好了,它可以工作了!
SetCallbackFunction_Dll=GetDelegate(“SetCallbackFunction”);
//创建回调!!!

如果您在C++中使用回调,则需要确保传递给C++的委托是生存的。只要使用相应的C++回调,您就可以将委托C对象保存起来:

someField=newcallbackfunction(cSharpCallback);
SetCallbackFunction(someField);

更好的是,只需使用它,就可以生成C++到C绑定(包括回调)免责声明:我是。

使用
string
而不是
StringBuilder
@DavidHeffernan的作者。我尝试了它,并在同一时间得到了完全相同的问题…可能错误在其他地方,但如果是的话,不知道在哪里…确实是的-正如我在上面的回答中所述!
// --------------------------------------------
// ----------------- C# CODE ------------------ 
// --------------------------------------------
// ----- the delegate type to be passed to the dll
public delegate bool CallbackFunction([MarshalAs(UnmanagedType.LPStr)] StringBuilder log);


// ----- prepare to load the dll's methods (I only show the SetCallback code here, other api methods
//       are declared and loaded the same way)
private delegate int _SetCallbackFunction_(CallbackFunction func);
private _SetCallbackFunction_ SetCallbackFunction_Dll;

public int SetCallbackFunction(CallbackFunction func) {
  return SetCallbackFunction_Dll(func);
}

// loading methods
private T GetDelegate<T>(string procName) where T : class {
  IntPtr fp = GetProcAddress(_dllHandle, procName);
  return fp != IntPtr.Zero ? Marshal.GetDelegateForFunctionPointer(fp, typeof(T)) as T : null;
}

async Task loadDllMethods() {
    // load the delegates => great, it works!
    SetCallbackFunction_Dll = GetDelegate<_SetCallbackFunction_>("SetCallbackFunction");
    // set callback in DLL, calls the delegate once successfully...
    SetCallbackFunction(cSharpCallback);
    await doTask();
}

async Task doTask() {
    // start the long dll method, that fires the callback to monitor progress
    while (someConditionIsMet) {
        DoLooongStuff(); // => calls the dll with registered callback!
    }
}


// the actual callback
bool cSharpCallback(StringBuilder strBuilder) {
    // this is called a few times successfully with the expected message!
    Console.WriteLine(strBuilder.ToString());
    return true;
}
// -------------- PREVIOUS CODE
async Task loadDllMethods() {
    // load the delegates => great, it works!
    SetCallbackFunction_Dll = GetDelegate<_SetCallbackFunction_>("SetCallbackFunction");
    // set callback in DLL, calls the delegate once successfully...
    SetCallbackFunction(cSharpCallback);
    await doTask();
}

// -------------- WORKING CODE!!!!
// add static reference....
static CallbackFunction _callbackInstance = new CallbackFunction(cSharpCallback); // <==== Added reference to prevent GC!!! 
async Task loadDllMethods() {
    // load the delegates => great, it works!
    SetCallbackFunction_Dll = GetDelegate<_SetCallbackFunction_>("SetCallbackFunction");
    // create callback!!!
    SetCallbackFunction(_callbackInstance); // <====== pass the instance here, not the method itself!!!
    await doTask();
}