2

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)

Maslow
  • 18,464
  • 20
  • 106
  • 193

1 Answers1

1

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

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454