« Return to Thread: C# Callback Function Implementation

Re: C# Callback Function Implementation

by David Piepgrass :: Rate this Message:

Reply to Author | View in Thread

> I am having trouble wrapping some third party C software into C# in
> particular implementing the callback function. The function to be wrapped
> is of the form:
>
> long setCallbackFunction(long fn(const HSIF_DATA_PT *pD1, const long N1,
> const HSIF_DATA_PT*pD2, const long N2));
>
> where HSIF_DATA_PT is a data structure for storing points.
>
>
> As expected without implementing any changes I get the type:
>
> public int setCallbackFunction(
> SWIGTYPE_p_f_p_q_const__HSIF_DATA_PT_q_const__long_p_q_const__HSIF_DATA_PT
> _q_const__long__long fn )
>
>
> which is not very useful.
>
>
>
> I have tried using the swig callback functions in the interface file so
> that:
>
> long setCallbackFunction( long (*op) (const HSIF_DATA_PT * pD1, const long
> N1, const HSIF_DATA_PT * pD2, const long N2) );
>
> %callback("%callback _cb")
> long fn(const HSIF_DATA_PT * pD1, const long N1, const HSIF_DATA_PT * pD2,
> const long N2)
> %nocallback
>
> ...however this doesn't seem to get me anywhere either.

I don't know what %callback is, but I wrote a macro %cs_callback for this purpose:

///////////////////////////////////////////////////////////////////////////
// cs_callback is used to marshall callbacks. It allows a C# function to
// be passed to C++ as a function pointer through P/Invoke, which has the
// ability to make unmanaged-to-managed thunks. It does NOT allow you to
// pass C++ function pointers to C#.
//
// Anyway, to use this macro you need to declare the function pointer type
// TYPE in the appropriate header file (including the calling convention),
// declare a delegate named after CSTYPE in your C# project, and use this
// macro in your .i file. Here is an example:
//
// in C++ header file (%include this header in your .i file):
// typedef void (__stdcall *Callback)(PCWSTR);
// void Foo(Callback c);
//
// in C# code:
// public delegate void CppCallback([MarshalAs(UnmanagedType.LPWStr)]
//                                  string message);
//
// in your .i file:
// %cs_callback(Callback, CppCallback)
//
// As an alternative to specifying __stdcall on the C++ side, in the .NET
// Framework (but not the Compact Framework) you can use the following
// attribute on the C# delegate in order to get compatibility with the
// default calling convention of Visual C++ function pointers:
// [UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
//
// Remember to invoke %cs_callback BEFORE any code involving Callback.
%define %cs_callback(TYPE, CSTYPE)
        %typemap(ctype) TYPE, TYPE& "void*"
        %typemap(in) TYPE  %{ $1 = ($1_type)$input; %}
        %typemap(in) TYPE& %{ $1 = ($1_type)&$input; %}
        %typemap(imtype, out="IntPtr") TYPE, TYPE& "CSTYPE"
        %typemap(cstype, out="IntPtr") TYPE, TYPE& "CSTYPE"
        %typemap(csin) TYPE, TYPE& "$csinput"
%enddef


------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

 « Return to Thread: C# Callback Function Implementation