I have an external dll that I have to use in C #. This external dll has a method to which a pointer to a method of my code is passed. In the old code the function AddressOf of VB was used but now I have to do it with C #. All I've read is using delegates, but can it be done in some other way?
The declaration of the original method in VB is as follows:
Private Declare Sub ReceiveMessage Lib "xxx.dll" (ByVal lngProcAddress As Long)
And the use of this method:
ReceiveMessage AddressOf GetMessages
How can I use the method of this dll in C #?
I edit to add the method statement in C #:
[DllImport("xxx.dll", EntryPoint = "ReceiveMessage", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern void ReceiveMessage(long lngProcAddress);
The GetMessages method is as follows:
public void GetMaMessages(int command)
{
switch (command) {
case 100: Show(); break;
case 501: Exit(); break;
}
}
Thank you very much.
I edit to give the solution I found:
One delegate is needed:
public delegate void MyMessages(int command, long from);
Use:
ReceiveMessage(Marshal.GetFunctionPointerForDelegate(new MyMaMessages(GetMaMessages)).ToInt64());