wrapping a C-style interface with a function pointer argument in C#

Carlos Frederico Biscaya <[email protected]>
Newsgroups gmane.comp.programming.swig
Message-ID <CANWOLaaoc5KniKXV4oPg-dsCcMh2043rVyz0enY+Sv5q2+7aww@mail.gmail.com>
Hi guys,

I don't know if the email I sent yesterday to the mailing list made it,
since it had attachments. So I am resending it with a few corrections, that
I made when I prepared to post this also on stackoverflow.

I've started using swig a few months ago and after a few hickups had my
first functioning c# wrapper. Now I need to create a wrapper in C# that
wrapps a C interface that takes a function ptr argument. I used a typedef
for the function ptr type and the swig macro cs_callback that I found while
researching how to get this done. This macro is needed to get swig to
generate a usable function ptr type (and name). Otherwise swig will create
a SWIGTYPE_p_f_p_InfoBarData__void class which makes things ugly and
complicated.

Unfortunatelly I seem to be stuck now.
AFAIK one of  two things should be happening, which aren't.

1) the delegate corresponding to my typedef for my function ptr is not
generated anywhere

typedef void (_stdcall *uiDataObserverFunc)(uiData*);

2) If I try using a typemap for inserting the delegate member into the
generated class, nothing happens.

I seem to be way out of my depth here and hope, someone can provide me with
some insight as to what is going wrong. I've attached a minimal example
that reproducibly gives me headaches and the swig project file needed. In
my development environment I am using swig 4.0.1.

**UiData.h**
```
#pragma once

extern "C" {

struct UiData
{
double temperature;
};

}
```

**UiDataProvider**
```
#pragma once

#include "UiData.h"

typedef void (_stdcall *uiDataObserverFunc)(InfoBarData*);


#ifdef UI_EXPORTS
#define UI_API __declspec(dllexport)
#else
#define UI_API __declspec(dllimport)
#endif


extern "C" {

UI_API void UI_Init();

UI_API void UI_RegisterObserver(uiDataObserverFunc callbackFunc);
}
```

**UiModule_cs.i**
```
#define UI_EXPORTS

%module UiWrapper

// TYPE: name of the C function ptr typedef
// CSTYPE: name of the corrseponding delegate
%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


%rename("%(strip:[UI_])s") "";

%cs_callback(uiDataObserverFunc, cppUiDataObserverCallback)

%typemap(cscode) UiWrapper %{
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
    public delegate void cppUiDataObserverCallback(UiData* uiDataUpdate);
%}

%{
#include "UiData.h"
#include "UiDataProvider.h"
%}

// these headers have to come before anything else - otherwise syntax
error(1) will be thrown
%include <windows.i>
%include <carrays.i>
%include <cpointer.i>

%include "UiData.h"
%include "UiDataProvider.h"
```


```
call "%swig%\swig.exe" -v -debug-typedef -c++ -csharp -o .\UiWrapper.cpp
-outdir .\UiModuleSwig UiModule_cs.i
```

I've tried

 - using -E to see whether something suspicious show up after
preprocessing, but to no avail.
 - reordering swig instruction blocks. If anything happened at all, it was
a worsening effect.
 - not using the typedef for the delegate (assuming I may have overridden
swig's behaviour in a wrong way) -> no effect


Best regards and thank you for your time,
Fred

_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.