VB.NET中的DllImport与声明 [英] DllImport vs Declare in VB.NET

查看:573
本文介绍了VB.NET中的DllImport与声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MSDN文档中注意到,有多种方式在VB.NET程序中声明对外部DLL中的函数的引用。

I notice in the MSDN documentation that there are multiple ways to declare a reference to a function in an external DLL from within a VB.NET program.

令人困惑的是,MSDN声称您只能使用 DllImportAttribute 类与共享函数原型 在极少数情况下,但我找不到此声明的解释,尽管您可以简单地使用 声明 关键字。

The confusing thing is that MSDN claims that you can only use the DllImportAttribute class with Shared Function prototypes "in rare cases", but I couldn't find the explanation for this statement, while you can simply use the Declare keyword instead.

为什么这些不同,在每种情况下我应该在哪里使用?

Why are these different, and where would I appropriately use each case?

推荐答案

声明实际上是在尝试维护 P / Invoke 语法,对于转换为 VB.NET 。它具有许多与P / Invoke相同的功能,但是某些类型(尤其是字符串)的编组有很大不同,并且可能会对熟悉DllImport规则的人们造成一些混乱。

Declare is really an attempt to maintain a P/Invoke syntax which would be more familiar to Visual Basic 6.0 users converting to VB.NET. It has many of the same features as P/Invoke but the marshalling of certain types, in particular strings, are very different and can cause a bit of confusion to people more familiar with DllImport rules.

我不确定文件中所谓稀有的含义。我在VB.NET和C#中都在代码中经常使用DllImport,而不会出现问题。

I'm not entirely sure what the documentation is alluding to with the "rare" distinction. I use DllImport in my code frequently from both VB.NET and C# without issue.

通常,除非您来自Visual Basic 6.0,否则我会在Declare上使用DllImport。 。 DllImport的文档和示例要好得多,并且有许多旨在生成DllImport声明的工具。

In general, I would use DllImport over Declare unless you come from a Visual Basic 6.0 background. The documentation and samples for DllImport are much better and there are many tools aimed at generating DllImport declarations.

这篇关于VB.NET中的DllImport与声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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