6

( complete duplicate of http://old.nabble.com/C%2B%2B-pointer-to-method-as-parameter-to-C--td17645155.html , but couldn't make the proposed macro work)

I've got the following C++ function (simplified) :

InputPort addInputPort(void(*callback)(InputPort));

Problem : the signature of the generated C# function is :

public InputPort addInputPort(SWIGTYPE_p_f__InputPort____void callback)

SWIGTYPE_p_f__InputPort____void is not a delegate (and has no public constructor anyway), so I can't use addInputPort.

How do I tell SWIG to use a delegate instead ? If the solution involves %typemap, please be extra patient with me...

Calvin1602
  • 9,413
  • 2
  • 44
  • 55
  • 1
    I guess C++/CLI is not an option? –  Jul 06 '11 at 19:27
  • I must investigate further, but I fear it's not : big precompiled pieces of middleware ahead ! – Calvin1602 Jul 07 '11 at 07:20
  • The same issue. Initially I used PInvoke (Details in question http://stackoverflow.com/questions/11909484/how-to-use-c-sharp-backgroundworker-to-report-progress-in-native-c-code). Now I am trying to use SWIG, but code become very cumbersome... and I also can not realize how to work with delegates. – sergtk Aug 26 '12 at 00:06
  • @Calvin1602 Have you obtained working code with using info at http://old.nabble.com/C%2B%2B-pointer-to-method-as-parameter-to-C--td17645155.html ? Thanks. I still can not use C++ function pointer wrappers in C# – sergtk Aug 26 '12 at 00:49
  • I don't remember, and I don't have the code at hand, but I DO remember that the code at oldnabble really didn't work. Poke me in 3 weeks if you still have the issue... – Calvin1602 Aug 27 '12 at 09:53

1 Answers1

1

The solution involves a %typemap. The effect you are seeing is the default useless mapping for unknown types. In your case, the unknown type is the function type void *(InputPort).

In fact, all of SWIG's translation is based on %typemaps which have already been written and live in standard SWIG libraries. You can investigate the initial %typemaps for C# in this Subversion location.

In particular, look for SWIGStringHelper in csharphead.swg. That code maps a delegate to a C callback. The trick is to add a helper callback implementation in the SWIG module.

Pedro Lamarão
  • 531
  • 5
  • 22