Can you use .net 3.5 Action or Func as Marshalled

2019-07-21 18:18发布

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条回答
对你真心纯属浪费
2楼-- · 2019-07-21 19:07

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

查看更多
登录 后发表回答