您可以使用 .net 3.5 Action 或 Func 作为编组非托管委托吗?
我一直在尝试根据自己的喜好修改代码。我创建了一个实现 idisposable 的类来包装加载调用并在需要时释放它们。但是,我似乎无法弄清楚是否可以使用匿名委托的语法。
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);
最后一行抛出一个参数异常,表明指定的 Type 不能是泛型类型定义。有没有办法解决这个问题,或者我是否必须提供一个指定的委托来执行任何非托管的操作?
供任何对使用非托管代码在 Windows 中默认执行的操作感兴趣的任何人参考 - 链接(创建快捷方式,动态加载DLL)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如异常所示,在将本机函数指针转换为托管代码时必须使用非泛型委托。
As the exception indicates, you must use a non-generic delegate when converting a native function pointer to managed code.