C++ pointer to method as parameter to C#

View: New views
3 Messages — Rating Filter:   Alert me  

C++ pointer to method as parameter to C#

by linkjuice :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello everybody,

Here is my C++ method I'd like to wrap in C# :

void Foo(Bar* (* method)());

"method" is a class method returning a pointer to a class "Bar"

Swig wrap this as follow in C# :

public void Foo(SWIGTYPE_p_f_method__f___Bar arg0)

"SWIGTYPE_p_f_method__f___Bar arg0" is not really user friendly and I don't know how to instantiate it.

What kind of feature allows to do that in C# and how to handle it with SWIG, Is there something special to do in the swig interface file?

Thx for you help.

Re: C++ pointer to method as parameter to C#

by David Piepgrass :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have developed a SWIG macro for passing callback functions from C# to
C++ (use it in your .i file):

////////////////////////////////////////////////////////////////////////
///
// 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)
//
%define %cs_callback(TYPE, CSTYPE)
        %typemap(ctype) TYPE, TYPE& "void*"
        %typemap(in) TYPE  %{ $1 = (TYPE)$input; %}
        %typemap(in) TYPE& %{ $1 = (TYPE*)&$input; %}
        %typemap(imtype, out="IntPtr") TYPE, TYPE& "CSTYPE"
        %typemap(cstype, out="IntPtr") TYPE, TYPE& "CSTYPE"
        %typemap(csin) TYPE, TYPE& "$csinput"
%enddef
%define %cs_callback2(TYPE, CTYPE, CSTYPE)
        %typemap(ctype) TYPE "CTYPE"
        %typemap(in) TYPE %{ $1 = (TYPE)$input; %}
        %typemap(imtype, out="IntPtr") TYPE "CSTYPE"
        %typemap(cstype, out="IntPtr") TYPE "CSTYPE"
        %typemap(csin) TYPE "$csinput"
%enddef

> -----Original Message-----
> Hello everybody,
>
> Here is my C++ method I'd like to wrap in C# :
>
> void Foo(Bar* (* method)());
>
> "method" is a class method returning a pointer to a class "Bar"
>
> Swig wrap this as follow in C# :
>
> public void Foo(SWIGTYPE_p_f_method__f___Bar arg0)
>
> "SWIGTYPE_p_f_method__f___Bar arg0" is not really user friendly and I
> don't
> know how to instantiate it.
>
> What kind of feature allows to do that in C# and how to handle it with
> SWIG,
> Is there something special to do in the swig interface file?
>
> Thx for you help.


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: C++ pointer to method as parameter to C#

by David Piepgrass :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Oh, I think you can ignore cs_callback2. I was going to use it for
something that didn't pan out...

Notice that the callback typedef must use __stdcall for .NET Framework
compatibility.

> I have developed a SWIG macro for passing callback functions from C#
to
> C++ (use it in your .i file):
>
>
////////////////////////////////////////////////////////////////////////
> ///
> // 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)
> //
> %define %cs_callback(TYPE, CSTYPE)
> %typemap(ctype) TYPE, TYPE& "void*"
> %typemap(in) TYPE  %{ $1 = (TYPE)$input; %}
> %typemap(in) TYPE& %{ $1 = (TYPE*)&$input; %}
> %typemap(imtype, out="IntPtr") TYPE, TYPE& "CSTYPE"
> %typemap(cstype, out="IntPtr") TYPE, TYPE& "CSTYPE"
> %typemap(csin) TYPE, TYPE& "$csinput"
> %enddef
> %define %cs_callback2(TYPE, CTYPE, CSTYPE)
> %typemap(ctype) TYPE "CTYPE"
> %typemap(in) TYPE %{ $1 = (TYPE)$input; %}
> %typemap(imtype, out="IntPtr") TYPE "CSTYPE"
> %typemap(cstype, out="IntPtr") TYPE "CSTYPE"
> %typemap(csin) TYPE "$csinput"
> %enddef


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user