0

Not sure exactly what to name my question but,

I have some C# code that accesses a C dll using DllImports. In the C# code I have functions that are registered with and called by the C dll. The C dll calls the functions passing a pointer to them.

I have it working using an IntPtr to represent the arg in the C# callbacks.

working callback method:

public static int test(IntPtr lua_State)

Now the question:

I would like the argument type to instead be a class with 1 IntPtr field, and instance methods that would operate on that IntPtr

so the new callback method would look like:

public static int test(Lua lua_State)

and the class would be:

[StructLayout(LayoutKind.Sequential)]
public class Lua
{
    IntPtr lua_State;

    //Some random methods...
}

Unfortunately this is not working, the callback is getting called, but the Classes IntPtr field is not getting the value passed and is instead 0.

Is there a way to get this to work?

edit:

Deriving from SafeHandle works, however I would prefer to do it without the safehandle class as I don't want/need any excess it adds, all I need is the pointer value to be put into the IntPtr field of my class.

2
  • With the first method definition, could you simply pass Lua.lua_State as an IntPtr ? Commented Jan 3, 2014 at 5:31
  • I'm not sure what you mean, the C dll is passing a pointer. The first method, the working callback, receives the pointer as an IntPtr, the class Lua does not exist in that example. I could take the IntPtr argument and make it into an instance of the Lua class if I wanted, but what I want is for the pointer that is passed from C to be directly translated into a class instance. I hesitate to say I want the pointer 'Marshalled' into the being a class because I think that might be incorrect. Commented Jan 3, 2014 at 6:08

1 Answer 1

1

The simplest way to handle this is make Lua derive from SafeHandle

public class Lua : SafeHandleZeroOrMinusOneIsInvalid 
{

     private Lua() : base (true)
     {
     }

     override protected bool ReleaseHandle()
     {
         return NativeMethods.CloseHandle(this.handle); //Or how ever you would normally release the pointer when done
     }

     //Other methods that work on the pointer, it is accessed via "this.handle"

}
Sign up to request clarification or add additional context in comments.

3 Comments

What exactly does deriving from safehandle do? The problem isn't that 0 or -1 is getting passed to the callback, the problem is that the value that is getting passed isn't being translated into the class's IntPtr field.
SafeHandle acts as a IntPtr but it also allows you to add additional methods just like you wanted to do. It also takes care of your cleanup for you when Lua goes out of scope if any cleanup needs to be done.
Thank you, I have gotten this to work, however I would really prefer to do it without SafeHandle as the only thing I actually need is for the pointer to be put into the IntPtr field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.