I am trying to call a method from a C++ dll which gets a JSON string as a parameter. One of the parameters embedded in a json format string is an unsigned int that represents the address of callback function which should be called when the method has been called. here is how it is being called in c++:
Json::Value jsval;
jsval["cmd"] = "logon";
jsval["Arg"]["IP"] = szIP;
jsval["Arg"]["PORT"] = nPort;
jsval["Arg"]["CALLBACK"] = (unsigned int)TheMsgCallback;
Json::FastWriter writer;
std::string sender = writer.write(jsval);
JScmd((char*)sender.c_str())
And here is how I tried to do the same in C#
Delegate callbackDelegate = new Action<int, IntPtr, ulong, int, int>(messageCallback);
IntPtr p = Marshal.GetFunctionPointerForDelegate(callbackDelegate);
string s = Converter.Serialize(new loginRequest() { Arg = new ArgParam() { IP = "10.0.0.121", PORT = 7020, CALLBACK = (uint)p } });
JScmd(s);
The problem is when I tried to do the same in C#, it didn't work, and I get an erro on this line
Marshal.GetFunctionPointerForDelegate
which says
The specified Type must not be a generic type definition
if you have any suggestion to fix this problem please let me know.
Try this:
public delegate void MyDelegate(int i, IntPtr j, ulong k, int l, int m);
...
MyDelegate callbackDelegate = new MyDelegate(messageCallback);
IntPtr p = Marshal.GetFunctionPointerForDelegate(callbackDelegate);