2

Using Call Back Function C# delegate dll in C++

but i have a error how can't i fix it?

this is my c# Code

 public delegate void Callback_Calc(int nResult);
    [Guid("180726E3-7185-4224-BF27-3A7A60E5D1A7")]
    public interface ICal
    {
        int Add(int a, int b);
        void Add_Callback(int a,int b,IntPtr Callback_Result);
    }
    [Guid("9FF07EDD-8DEE-4CCA-9BDF-2C753F7717AC")]
    public class Calc : ICal
    {
        public int Add(int a, int b)
        {
            return (a + b);
        }
        public void Add_Callback(int a, int b, IntPtr Callback_Result)
        {
            int sum = a + b;
            Callback_Calc func = (Callback_Calc)Marshal.GetDelegateForFunctionPointer(Callback_Result, typeof(Callback_Calc));
            func(sum);
        }
    } 
 

this is my C++ Code

 
extern "C" void __stdcall callback_func(int nResult);
void __fastcall TForm2::btn2Click(TObject *Sender)
{
 ICal *m_Calc;
    HRESULT hr = CoInitialize(NULL);
    hr =  CoCreateInstance(CLSID_Calc,NULL,CLSCTX_INPROC_SERVER,IID_ICal,reinterpret_cast<void**>(&m_Calc));
    if(FAILED(hr))
    {
     CoInitialize(NULL);
    }
    int a = m_Calc->Add(2,5);
    lbl1->Caption = a;
    m_Calc->Add_Callback(10,10,(_Callback_Calc *)callback_func);
 CoInitialize();
}
extern "C" void __stdcall callback_func(int nResult)
{
 int a = nResult;
    int b = nResult;
}

this is my C++ error Code

[C++ Error] ComputAddTest.cpp(55): E2034 Cannot convert '_Callback_Calc *' to 'long' [C++ Error] ComputAddTest.cpp(55): E2342 Type mismatch in parameter 'Callback_Result' (wanted 'long', got '_Callback_Calc *') [C++ Error] ComputAddTest.cpp(56): E2193 Too few parameters in call to '__stdcall CoInitialize(void *)'

thank you

1 Answer 1

1

Your Add_Callback function requires a pointer to the function. So in the line:

m_Calc->Add_Callback(10,10,(_Callback_Calc *)callback_func);

you need to change the 3rd parameter to a pointer to callback_func.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.