Use of AddressOf in C # without delegates

2

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());
    
asked by AlvaroMig 19.10.2017 в 15:56
source

1 answer

1

After doing some research, because I was left with the doubt, it seems that you do not need any delegate to do this.

C # implicitly passes the procedure address and creates the delegate.

So the following should work:

ReceiveMessage (GetMessages)
    
answered by 19.10.2017 в 16:05