Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I've developed a small C++ application that calls a managed .NET C# COM object using the guide lines in this article:

Calling Managed .NET C# COM Objects from Unmanaged C++ Code[^]

Now I would like to establish a callback function in my C++ application that is called from my .NET application. I want to create a function on the .NET side - RegisterCppCallback that is called from the C++ app. and takes some kind of function pointer as argument.

My idea is that is should look something like this on the .NET side:

C#
public delegate void CppCallbackFn();
CppCallbackFn myCallbackFunc;
public void RegisterCppCallback(IntPtr pfn)
        {
            myCallbackFunc = (CppCallbackFn) (Marshal.GetDelegateForFunctionPointer(pfn, typeof(CppCallbackFn))) as CppCallbackFn;
            myCallbackFunc(); // test it
        }


and on the CPP side:

typedef int (__stdcall* CallbackFunc)();

CallbackFunc * mInstance;

C++
int CallbackHandler()
    {
        // some code

        return 0;
    }

// call the RegisterCppCallback function in .NET
mInstance = new CallbackFunc((CallbackFunc) CallbackHandler);
pDotNetCOMPtr->RegisterCppCallback((long) mInstance);


My problem is that the callback function in CPP isn't called.
What am I doing wrong ? Any help is highly appreciated
Posted
Updated 23-Sep-13 20:13pm
v2
Comments
Sergey Alexandrovich Kryukov 23-Sep-13 11:10am    
Any more detail? Such as the signatures of C++ function and callback function used in the call?
—SA
Evan Haahr Hansen 23-Sep-13 14:43pm    
The callback function should take a string as parameter and return another string.
Evan Haahr Hansen 24-Sep-13 2:17am    
I've now improved my question / added more details. In the first place I just want to make it work. In the end the callback function should take a string as parameter and return another string.
V.Lorz 24-Sep-13 2:41am    
Who manages memory allocation/deallocation for the resulting string?
Dimitry Arsenev 20-Apr-23 7:44am    
Your example crash my app. Maybe you show all your solution.

I've found the solution for my problem. Just change the last line in the above listed code to:

pQGISInterfacePtr->RegisterCppCallback((long) CallbackHandler);
 
Share this answer
 
Might this How to make a callback to C# from C/C++ code[^] article help you?
EDIT(0):
This covers the oposite case: pinvoke-c-callback-from-c[^].
EDIT(1):
For establishing callbacks from the C++ side (registering them in a managed class) I've always used mixed mode C++ assemblies. That way I was able to have access to both worlds (managed class instances from unamanged C++ code and viceversa).

p.d. forget about this solution and downvote it.
 
Share this answer
 
v4
Comments
Evan Haahr Hansen 24-Sep-13 3:10am    
No, the article doesn't help me that much because the callback goes from C++ to C#. I want to go from C# to C++.
Evan Haahr Hansen 24-Sep-13 5:37am    
@V.Lorz - The other article ( pinvoke-c-callback-from-c[^]) calls the C++ function using DllImport. I want to pass a unmanaged function from C++ to .NET
Evan Haahr Hansen 24-Sep-13 6:26am    
@V.Lorz - Could you please give me a good example of how you do that ? It would be great if your example includes my code ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900