I have written an unmanaged c++ dll that works good (IPinvoke))) There is one resource-consuming function in it – there is a loop with complex time consuming logic. What is the best way to calculate percentage of this loop progress and sending break to this loop – using callbacks or may be passing parameters?
If callbacks is the most good variant – could anyone provide sample?
in dll:
extern "C" _declspec(dllexport) uint8* resourceConsumingFunction(uint8* dataBufer)
{
//there is a loop with many math here
return dataBuffer;
}
in c#
[DllImport("MyLib.DLL", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern byte* resourceConsumingFunction(byte* dataBuf);
//.....
byte* bufbuf = resourceConsumingFunction(data);//there I need to break this function and to get //percentage
Sure, a callback can work. You’ll need a function pointer in the C++ code, something like this:
And the equivalent C# code would be:
Using __stdcall for the callback helps keep the delegate declaration simple. The GC.KeepAlive() call is necessary to stop the garbage collector from collecting the delegate object too soon.