你可以使用.NET 3.5的行动或Func键为编组非托管的代表? [英] Can you use .net 3.5 Action or Func as Marshalled unmanaged delegates?

查看:255
本文介绍了你可以使用.NET 3.5的行动或Func键为编组非托管的代表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看完<一href="http://blogs.msdn.com/jonathanswift/archive/2006/10/03/Dynamically-calling-an-unmanaged-dll-from-.NET-%5F2800%5FC%5F23002900%5F.aspx"相对=nofollow>动态调用.NET

我一直在试图修改code合我的胃口。我做了实现IDisposable包装在负载要求并在需要时释放它们的类。不过,我似乎无法找出语法是否可以使用匿名委托吧。

  VAR加载= DynamicLibraryLoader.TryLoad(的User32.dll);
变种蜂鸣器= loaded.GetProcAddress(的MessageBeep);
VAR类型= ty​​peof运算(动作&LT; UInt32的&GT;);
动作&LT; UInt32的&GT; beepAction2 =(动作&LT; UInt32的&GT;)Marshal.GetDelegateForFunctionPointer(蜂鸣器,类型);
 

最后一行抛出参数异常说,指定的类型不能为泛型类型定义。有没有解决的办法还是我必须提供一个名为委托做任何非托管?

对于任何有兴趣的,你可以在默认情况下在Windows与非托管code做参考 - 链接(创建快捷方式,动态地加载DLL)

解决方案

由于异常指示,必须在本地函数指针转换为托管code当使用非泛型委托。

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)

解决方案

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

这篇关于你可以使用.NET 3.5的行动或Func键为编组非托管的代表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆