简体   繁体   English

VB.NET在可移植类库中使用dllimport

[英]VB.NET Using a dllimport in a portable class library

I wanted to change my usercontrol to a portable class library. 我想将用户控件更改为可移植类库。

However, in this line of my code... 但是,在我的代码这一行中...

<DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Public Function UnhookWindowsHookEx(ByVal idHook As Integer) As Boolean
End Function

... I get the error "Type ''DllImport'' is not defined''. ...我收到错误消息“类型“ DllImport”未定义”。

What am I doing wrong here and how could I fix that? 我在这里做错什么,如何解决? Thank you! 谢谢!

In order for a library to be classified as portable it needs to adhere to a set of restrictions that include not supporting DllImport . 为了将库分类为可移植库,它需要遵守一系列限制,其中包括支持DllImport的限制。 Across the various operating systems that can use a portable class library, directly using native operating system calls isn't supported. 在可以使用可移植类库的各种操作系统中,不支持直接使用本机操作系统调用。

In this case, you would need to break the component into two or more pieces. 在这种情况下,您需要将组件分成两部分或更多部分。 The control that needs the call to UnhookWindowsHookEx would need to use a second platform specific component which implements the functionality, abstracting it from the UserControl. 需要调用UnhookWindowsHookEx的控件将需要使用第二个平台特定的组件来实现该功能,并从UserControl中对其进行抽象。 This could be done for example by using a platform specific class that implements an interface: INativeAccess . 例如,这可以通过使用平台特定的类来实现,该类实现一个接口: INativeAccess At run time, it would use the platform specific class if available. 在运行时,它将使用特定于平台的类(如果有)。 There is more on MSDN about this. MSDN上有更多有关此的内容。

Just like any type you want to use in code, you must have referenced the assembly that the DllImportAttribute class is defined in in order to use it. 就像您要在代码中使用的任何类型一样,您必须已引用DllImportAttribute类定义的程序集才能使用它。 You must also have imported the namespace that it's a member of to use it unqualified. 您还必须导入它所属的名称空间,才能不加限制地使用它。 Have you done both of those things? 你都做完这两件事了吗?

Even if you have, the whole point of a portable class library is that it's portable. 即使有,可移植类库的全部要点是它是可移植的。 Is that API going to be supported on all the platforms you might port it to? 您可能会在所有平台上支持该API吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM