可以使用.net 3.5 Action或Func作为编组的非托管代理? [英] Can you use .net 3.5 Action or Func as Marshalled unmanaged delegates?
问题描述
我一直在尝试修改代码,以符合我的喜好。我创建了一个实现idisposable的类来封装加载调用,并在需要时释放它们。然而,如果可以使用匿名委托,我似乎无法弄清楚语法。
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?
对于任何有兴趣的人,你可以在Windows中默认做什么,非托管代码 - 链接(创建快捷方式,动态加载DLL)
For reference of any interested in what you can do by default in windows with unmanaged code - Link (create shortcuts,dynamically load a DLL)
推荐答案
正如异常所示,在将本机函数指针转换为托管代码时,必须使用非泛型委托。
As the exception indicates, you must use a non-generic delegate when converting a native function pointer to managed code.
这篇关于可以使用.net 3.5 Action或Func作为编组的非托管代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!