1

I am using a c++ dll to do some background computation and I am trying to get it to report progress back to my calling C# code.

To do that, I've registered a callback method that accepts a StringBuilder as a parameter (found on the web that this was a proper way of doing it).

Here is my ´c++´ code :

// --------------------------------------------
// ----------------- 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...)
    }
}

Now for the calling c# code, I'm using the following code:

// --------------------------------------------
// ----------------- 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;
}

I've searched over differrent threads to find out the error. I had an error due to too small ´buf´ sizes, so I just made sure it is large enough. I've also teted that ´&_callback´ is always pointing at the same place (it is!).

I am out of search options, any help will be much appreciated. Note that I am note an expert in dll integration, Marshalling etc, I put references I found the hints!

neggenbe
  • 1,697
  • 2
  • 24
  • 62

2 Answers2

1

You need to insure the delegate you pass to C++ is alive as long as the callback is being used in C++. You are responsible for holding delegate C# object alive as long as the corresponding C++ callback is used:

someField = new CallbackFunction(cSharpCallback);
SetCallbackFunction(someField);

Better yet, just use Scapix Language Bridge, it generates C++ to C# bindings (including callbacks), completely automatically. Disclaimer: I am the author of Scapix Language Bridge.

Boris Rasin
  • 448
  • 4
  • 12
0

I found the answer to my question, thanks to this post:

In order to keep the unmanaged function pointer alive (guarding against GC), you need to hold an instance of the delegate in a variable

So the modified code is ONLY in C#

// -------------- 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();
}

NOTE: I also changed StringBuilder to string !

neggenbe
  • 1,697
  • 2
  • 24
  • 62