Pointer to objects in C#

I have an unmanaged C++ library that I call from managed code. One of its methods takes in a structure of function pointers and a pointer to void. Its basically a callback scenario.

The problem is that the unmanaged code calls the function in managed code using a "void*". I don't know how to I define a managed function to accept a "void*" for the call from C++. Can it even be done

The error I get states that I cant!
Error: Cannot take the address of, get the size of, or declare a pointer to a managed type
Is there a way around this

Mega Thanks in advance for any advice!!!

-----------Managed

public struct CallBackStruct
{
public delegate void Func1(IntPtr Class1)
public Func1 Func1_CallBack;
.
...
}

public class CallBack
{
Func1()
{
//callback code
}
}

public class Class1
{
[DllImport("CPP.dll", EntryPoint = "API1"]
public static extern void API1(ref CallBackStruct, [ MarshalAs( UnmanagedType.AsAny )] Object CallBack)

public static void Main()
{
CallBackStruct s;
s.Func1_CallBack = Func1_CallBack;

Callback cb = new Callback;

API1(s, cb);
}

// This is where the problem is..
// When the unmanaged code call this function how do I marshal the "void*" back
// Can I even do this
unsafe public static void Func1_CallBack(Callback* c)
{
*c.Func1();
}
.
...
}


-----------Unmanaged

typedef struct CallBackStruct CallBackStruct;
struct CallBackStruct
{
void (*Func1_Callback)(void *CallBack);
.
...
};

API1(CallBackStruct* s, void* CallBack)
{
s->Func1_Callback(CallBack);
}




Answer this question

Pointer to objects in C#

  • badegine


    If you are registering a pointer to a function (As you seem to be), you can use the magic of the Marshaller and use a Delegate instead of trying to pass a pointer (delegates are marshalled as pointers to functions)

    [DllImport("CPP.dll", EntryPoint = "API1"]
    public static extern void API1(ref CallBackStruct, CallBackDelegate function)

    public delegate void CallbackDelegate();

    then

    {
    CallBackDelegate func = new CallBackDelegate ( ...);
    API1( cbs, func);
    ...
    }

    Make sure your delegate (func above) lives long enough! It can be collected anytime, which may cause surprising results. (Use can use GC.KeepAlive() or make it a member of a a longer-lived object)



  • enric vives

    That doesn't seem to work.
  • sully blue

    Pelase give me proper c# class.

    -Thanks



  • okieflier

    You can try substituting IntPtr for void*.

  • skurge23423

    Thanks for all you input but here was the answer. In my callback function in managed code I have to properly cast the object back to the correct type.

    unsafe public static void Func1_CallBack(IntPrt c)
    {
    CallBack c2;
    c2 = (CallBack)Marshal.PtrToStructure(c,typeof(CallBack));
    c2.Func1();
    }


  • Dave Holmes

    Hi,

    Please post your valid c# calss to check, what ever the thing above is giving some syntax error.

    -Thanks

    Mahesh



  • Pointer to objects in C#