Can you use .net 3.5 Action or Func as Marshalled unmanaged delegates?

こ雲淡風輕ζ 提交于 2019-12-24 03:03:58

问题


After reading Dynamically calling unmanaged dlls in .net

I've been trying to modify the code to my liking. I made a class that implements idisposable to wrap load calls in and free them when needed. However I can't seem to figure out the syntax if it is possible to use anonymous delegates with it.

var loaded=DynamicLibraryLoader.TryLoad("User32.dll");
var beeper=loaded.GetProcAddress("MessageBeep");
var type=typeof(Action<UInt32>);
Action<UInt32> beepAction2=(Action<UInt32>) Marshal.GetDelegateForFunctionPointer(beeper,type);

The last line throws an argument exception saying that the specified Type must not be a generic type definition. Is there a way around this or do I have to provide a named delegate to do anything unmanaged?

For reference of any interested in what you can do by default in windows with unmanaged code - Link (create shortcuts,dynamically load a DLL)


回答1:


As the exception indicates, you must use a non-generic delegate when converting a native function pointer to managed code.



来源:https://stackoverflow.com/questions/1347484/can-you-use-net-3-5-action-or-func-as-marshalled-unmanaged-delegates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!