Search code examples
c#c++pinvoke

Pass void to void with PInvoke


I had a problem passing a managed void to a c++ void. Do you kow how to properly do it? Here's my c++ code which gets a void(__cdecl* disph)() as parameter

extern "C" __declspec(dllexport) void Display(void(__cdecl* disph)());

And my question is how can I pass a simple void() (for example public static void Display()) to that c++ void.


Solution

  • You are talking about passing function pointers, right? voids are not passed, as there is nothing to be passed because it is, errr, void.

    If I understood correctly, you need to pass a pointer to a function which takes no parameters and returns nothing, right? I am not at a Windows machine at the moment so this is untested, but something like this should work:

    public delegate void VoidFnDelegate();
    public value struct MyDLL
    {
        [DllImport("MyDll.dll")]
        static public void Display(VoidFnDelegate fn);
    }
    ...
    
    void SomeFn() { ... }
    
    MyDll.Display(new VoidFnDelegate(SomeFn));