In C # code, declare your own C ++ method using the DLLImport attribute and call this method from the BackgroundWorker.ProgressChanged handler. Strike>
DISCLAIMER: I have not tested this code, and it may not be the best approach, but, at least in theory, I think it will work. Hopefully one of the more experienced members here can check if this is really correct.
It is assumed that you are running a background worker from C #, and you want the ProgressChanged event in C # (I assume that it is, since your user interface is in C #).
You can still use BackgroundWorker in C #, but just call your native method using the DLLImport mentioned above. You can also change the signature of your method to take a pointer to a function that matches the signature for ReportProgress , and then call that delegate from your own code.
MSDN has articles on Routing Delegates and Function Pointers (although all examples use C ++ / CLI). You can also see the documentation for DLLImport and the MarshalAs and UnmanagedType listing.
For example, if your own method was
void foo(int arg1, BOOL arg2) {
you define the type of function pointer in your native code as
// Corresponds to void BackgroundWorker.ReportProgress(int progress, object state) typedef void (*NativeReportProgress) (int, void*);
and change your own signature to
void foo(int arg1, BOOL arg2, NativeReportProgress progressPtr) {
Your DLLImport for foo will look like
Then your worker will look like
void DoWork(object sender, DoWorkEventArgs e) { var worker = (BackgroundWorker)sender;
Hope this made some sense (and hope it is right!)