GetFunction​PointerFor​Delegate-> ​GetDelegate​ForFunction​Pointer = ​null


ok, have bit of strange situation. in past have created managed plugin unmanaged c++ api. use c# reverse p/invoke unmanaged "host" can treat managed dll if has c entry points. part works fine.

now onto next adventure, i'm creating managed host myself can load these unmanaged plugin dlls. in theory, managed dll exports these c functions should able loaded without issue, yet finding if this:

 

var callback = marshal.getfunctionpointerfordelegate(mydelegate); var del = marshal.getdelegateforfunctionpointer(callback, ...) 

 

...that del is null. in theory should not null, documentation states cannot (doesn't why specifically), , theory sort of security mechanism getdelegateforfunctionpointer call somehow checks see pointer did not originate managed method.

is there other way call unmanaged function pointer managed code without resorting c++/cli? or there way force getdelegateforfunctionpointer ignore fact pointer originated managed method?

 

edit: documentation states:


  • "you cannot pass invalid function pointer getdelegateforfunctionpointer. in addition, can use method pure unmanaged function pointers. cannot use method function pointers obtained through c++ or getfunctionpointer. you cannot use method create delegate function pointer managed delegate."

ok, doesn't why, , if have situation require such functionality, how 1 work around issue?

 

edit: update...

as turns out, in case getdelegateforfunctionpointer not return null, instead of returning type ask in second parameter, returns delegate. i'm not sure why though. didn't notice wasn't returning null because casting delegate type asking for.

interestingly enough, delegate instance has info regarding original managed method, though method passed along intptr getfunctionpointerfordelegate, , assembly method in references if unmanaged dll.

i can call delegate.dynamicinvoke , works, @ least 10x slower calling delegate non-dynamically isn't ideal. in meantime can live until can figure out how improve performance (maybe dynamicmethod?).


 

hi,

 

in opinion, little needless use code. according code, "mydelegate" delegate in managed code. why need convert function pointer callable unmanaged code, re-convert function pointer delegate in managed code. mean if want call delegate, call "mydelegate".

 

this restriction caused technical difficulities, , product team best improve in future releases.
currently, documented, we’d better avoid way use method.

 

 


paul zhou [msft]
msdn community support | feedback us
get or request code sample microsoft
please remember mark replies answers if , unmark them if provide no help.




.NET Framework  >  Common Language Runtime Internals and Architecture



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'