none
GetFunction​PointerFor​Delegate-> ​GetDelegate​ForFunction​Pointer = ​null RRS feed

  • Question

  • OK, I have a bit of a strange situation. In the past I have created a managed plugin for an unmanaged C++ API. I use C# with reverse P/Invoke so that the unmanaged "host" can treat my managed DLL as if it has C entry points. That part works fine.

    Now onto my next adventure, I'm creating a managed host myself that can load these unmanaged plugin DLLs. In theory, my managed DLL that exports these C functions should be able to be loaded up without issue, yet what I am finding is that if I do this:

     

    var callback = Marshal.GetFunctionPointerForDelegate(myDelegate);
    var del = Marshal.GetDelegateForFunctionPointer(callback, ...)
    

     

    ...that del is null. In theory it should not be null, but the documentation states that you cannot do this (doesn't say why specifically), and my theory is that this is some sort of security mechanism where the GetDelegateForFunctionPointer call somehow checks to see that the pointer did not originate from a managed method.

    Is there any other way to call into an unmanaged function pointer from managed code without resorting to C++/CLI? Or is there a way to force GetDelegateForFunctionPointer to ignore the fact that the pointer originated from a managed method?

     

    EDIT: The documentation specifically states:


    • "You cannot pass an invalid function pointer to GetDelegateForFunctionPointer. In addition, you can only use this method for pure unmanaged function pointers. You cannot use this method with function pointers obtained through C++ or from GetFunctionPointer. You cannot use this method to create a delegate from a function pointer to another managed delegate."

    OK, but it doesn't say why, and if you do have a situation that you require such functionality, how would one work around this issue?

     

    EDIT: Another update...

    As it turns out, in this case GetDelegateForFunctionPointer does not return null, but instead of returning the type you ask for in the second parameter, it returns Delegate. I'm not sure why it does that though. I didn't initially notice that it wasn't returning null because I was casting the delegate to the type I was asking for.

    Interestingly enough, the Delegate instance has all the info regarding the original managed method, even though that method was passed along as an IntPtr from GetFunctionPointerForDelegate, and the assembly that the method is in is only references as if it is an unmanaged DLL.

    I can actually call Delegate.DynamicInvoke and it works, but this at least 10x slower than calling a delegate non-dynamically so it isn't ideal. In the meantime I can live with this until I can figure out how to improve its performance (maybe with DynamicMethod?).


    Sunday, April 24, 2011 7:07 PM

Answers

  •  

    Hi,

     

    In my opinion, it is a little needless to use as your code. according to your code, "myDelegate" is a delegate in managed code. why do you need to convert it to a function pointer callable from unmanaged code, then re-convert the function pointer to a delegate in managed code. I mean if you want to call the delegate, just call the "myDelegate".

     

    This restriction is caused by some technical difficulities, and the product team will do their best to improve it in future releases.
    Currently, as it is documented, we’d better avoid the way to use this method.

     

     


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Marked as answer by Paul Zhou Tuesday, May 3, 2011 6:59 AM
    Thursday, April 28, 2011 9:16 AM