Fwd: accessing wrapped data

Carlos Frederico Biscaya <[email protected]>
Newsgroups gmane.comp.programming.swig
Message-ID <CANWOLaZ==u+WO6BqJj++Tg+P9eHa26yiaFTmFBo_+296FYYtxw@mail.gmail.com>
Hi there,

First of all, I do apologize, if I have sent this before, but I am unsure,
that my message actually arrived at the mailing list.

I have a data struct from C++:

struct UiData
{
    double temperature;
}
, for which swig has generated a C# wrapper, UiData.cs. This generated
wrapper is part of a dll and I have a question on how to access it, when
dealing with callbacks.

I have defined a function that takes a callback with said struct as an
arguement in C++ that looks like this:

typedef void(_stdcall *uiDataObserverFunc)(UiData*);
__declspec(dllexport) void UI_RegisterObserver(uiDataObserverFunc
callbackFunc);

and a C# callback signature that looks like this:

    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    public delegate void PureCallbackData(UiData data);

    [DllImport("PureCppDll", EntryPoint = "UI_RegisterObserver")]
    public static extern void
RegisterObserverData([MarshalAs(UnmanagedType.FunctionPtr)]
PureCallbackData callbackPointer);

The actual callback function is this:

PureManagedDll.PureCallbackData cbfred = (UiData data) =>
            {
                Console.WriteLine("hello from the RegisterObserverData
func: {0}", data.temperature);
            };
PureManagedDll.RegisterObserverData(cbfred);

I don't know why, but at this point I get no output at all, and if I go
into the debugger of VisualStudio I get an access violation. Am I doing
something completely absurd? I also tried using a ref UiData with the
callback, but the result is the same.
In a previous version of my test project, I had no separation between the
wrapper and the test program, so I used

 internal UiData(global::System.IntPtr cPtr, bool cMemoryOwn)

and a callback that would take an IntPtr to achieve my goal. But this is no
longer possible, when the wrapped code is in a separate DLL, so I am pretty
much stuck.

if anyone knows how to do this, please help. I have no idea, what the
problem is.

Br,
Fred

P.S. If you want, I can provide a buildable VisualStudio project and the
swig project file for my dilemma.

_______________________________________________
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.