在CRL包装器类中使用两个long类型的参数实现C#委托

时间:2019-05-31 10:13:11

标签: c# c++ delegates clr wrapper

要在C ++中调用C#dll,我们使用CLR包装器类。我想在C ++中实现C#委托实现的逻辑。但是委托有2个long类型的参数。从几个站点开始,我就可以使用字符串参数实现委托。

一些代码已在此处写为注释部分

ref类CSharpClassEventsHandler {     私人:List ^ onMessageCallbacks;

public: CSharpClassEventsHandler(CSharpClass^ cSharpClass)
{
            onMessageCallbacks = gcnew List<IntPtr>();
            CSharpClass->OnRequest += gcnew OnRangerRequestDelegate(this, &CSharpClassEventsHandler::OnStartUp);
            CSharpClass->OnNewStateReq += gcnew OnRangerRequestDelegate(this, &CSharpClassEventsHandler::OnNewState);
}

private: void OnStartUp(System::String^ message)
{
             IntPtr ptr = Marshal::StringToHGlobalAnsi(message);

             const char* msg = (const char*)ptr.ToPointer();

             for (int i = 0; i < onMessageCallbacks->Count; i++)
             {
                 void(*ptr)(const char*) = (void(*)(const char*))onMessageCallbacks[i].ToPointer();

                 ptr(msg);
             }

             // Avoid memory leak.
             Marshal::FreeHGlobal(ptr);
}

private: void OnNewState(Int64 State1, Int64 State2)
{
             //required logic to implement here.
             // could you please help me write logic here
}

public: void RegisterOnNewState(void(*callback)(const long, const long))
{
            IntPtr ptr(callback);

            onMessageCallbacks->Add(ptr);
}

public: void RegisterOnStartUp(void(*callback)(const char*))
{
            IntPtr ptr(callback);

            onMessageCallbacks->Add(ptr);
}

};

0 个答案:

没有答案