question about using SWIGTYPE_p_void in a callback

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

question about using SWIGTYPE_p_void in a callback

Gonzales, Jeff

Hi Everyone,

 

I am new to SWIG.  I have some C code I have compiled using the Android NDK that I need to call from C#. It seems to be working as I can create basic types and have C invoke callbacks on my C# delegates.  One of the parameters in my callback is of type SWIGTYPE_p_void.  I am trying to pass a pointer to a C# object, which the C code does nothing with except pass it back to C# via the callback. 

 

Unfortunately, when the callback (C# delegate) is called, every parameter that comes in from C is valid except for the one of type SWIGTYPE_p_void.  It comes back as null.

 

I have tried several ways to create the pointer, such as casting a handle from GCHandle.Alloc() and also using GCHandle.ToIntPtr().  It doesn’t matter what I try as this parameter is still null upon returning from the C code.  I suspect maybe I don’t have my class definition created correctly, or perhaps the C code is mangling the parameter somehow.

 

Here is what my delegate (callback) looks like:

 

[System.Runtime.InteropServices.UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.Cdecl )]

        delegate void drawBarCallback(double left, double top, double width, double height, SWIGTYPE_p_void privateContext);

 

Here is the class definition for the object I’m trying to pass in privateContext:

 

public class TestPrivateContext
    {
        public int TestMyPrivateContext()
        {
            // return test value
            return 69;
        }
    }

 

 

When the callback in invoked getCPtr() returns null:

 

private static void HandleDrawBarCallback(double left, double top, double width, double height, SWIGTYPE_p_void privateContext)
{
    try
    {
        System.Runtime.InteropServices.HandleRef hRef = SWIGTYPE_p_void.getCPtr(privateContext);
        System.Runtime.InteropServices.GCHandle handle2 = (System.Runtime.InteropServices.GCHandle)hRef.Handle;
 
        TestPrivateContext testPC = (handle2.Target as TestPrivateContext);
 
        int returnValue = testPC.TestMyPrivateContext(); 
    }
    catch (Exception ex)
    {
        Log.Info("ex=" + ex.ToString());
    }

 

 

Does anyone have any idea what I should try next?

 

Thanks,

Jeff

 

 

 

 


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: question about using SWIGTYPE_p_void in a callback

William S Fulton
Hard to say, I suggest you debug the C++ and C# code to see where the
pointer changes to NULL. It should work.

Also consider applying the VOID_INT_PTR typemaps, see
http://swig.org/Doc3.0/CSharp.html#CSharp_void_pointers. However, this
collection of named typemaps are missing the csdirectorin and
csdirectorout typemaps. Grab the additions in the csharp.swg file in
this commit: https://github.com/swig/swig/commit/e9e6a99f0fed20fc631c912075f0f94eed3bae66.

William

On 14 October 2016 at 20:21, Gonzales, Jeff <[hidden email]> wrote:

> Hi Everyone,
>
>
>
> I am new to SWIG.  I have some C code I have compiled using the Android NDK
> that I need to call from C#. It seems to be working as I can create basic
> types and have C invoke callbacks on my C# delegates.  One of the parameters
> in my callback is of type SWIGTYPE_p_void.  I am trying to pass a pointer to
> a C# object, which the C code does nothing with except pass it back to C#
> via the callback.
>
>
>
> Unfortunately, when the callback (C# delegate) is called, every parameter
> that comes in from C is valid except for the one of type SWIGTYPE_p_void.
> It comes back as null.
>
>
>
> I have tried several ways to create the pointer, such as casting a handle
> from GCHandle.Alloc() and also using GCHandle.ToIntPtr().  It doesn’t matter
> what I try as this parameter is still null upon returning from the C code.
> I suspect maybe I don’t have my class definition created correctly, or
> perhaps the C code is mangling the parameter somehow.
>
>
>
> Here is what my delegate (callback) looks like:
>
>
>
> [System.Runtime.InteropServices.UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.Cdecl
> )]
>
>         delegate void drawBarCallback(double left, double top, double width,
> double height, SWIGTYPE_p_void privateContext);
>
>
>
> Here is the class definition for the object I’m trying to pass in
> privateContext:
>
>
>
> public class TestPrivateContext
>
>     {
>
>         public int TestMyPrivateContext()
>
>         {
>
>             // return test value
>
>             return 69;
>
>         }
>
>     }
>
>
>
>
>
> When the callback in invoked getCPtr() returns null:
>
>
>
> private static void HandleDrawBarCallback(double left, double top, double
> width, double height, SWIGTYPE_p_void privateContext)
>
> {
>
>     try
>
>     {
>
>         System.Runtime.InteropServices.HandleRef hRef =
> SWIGTYPE_p_void.getCPtr(privateContext);
>
>         System.Runtime.InteropServices.GCHandle handle2 =
> (System.Runtime.InteropServices.GCHandle)hRef.Handle;
>
>
>
>         TestPrivateContext testPC = (handle2.Target as TestPrivateContext);
>
>
>
>         int returnValue = testPC.TestMyPrivateContext();
>
>     }
>
>     catch (Exception ex)
>
>     {
>
>         Log.Info("ex=" + ex.ToString());
>
>     }
>
>
>
>
>
> Does anyone have any idea what I should try next?
>
>
>
> Thanks,
>
> Jeff
>
>
>
>
>
>
>
>
>
>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
> _______________________________________________
> Swig-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/swig-user
>

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user