在 VB.NET 中使用 DLL 导入/声明以及 .NET 未使用的参数类型

发布于 2024-09-06 20:52:20 字数 886 浏览 9 评论 0原文

我正在开发一个项目,必须将 DLL 文件导入到几年前创建的 VB 项目中。 DLL是用C++创建的,看起来像:

void CoordinateConversionService::convert( SourceOrTarget::Enum sourceDirection, SourceOrTarget::Enum targetDirection, CoordinateTuple* sourceCoordinates, Accuracy* sourceAccuracy, CoordinateTuple& targetCoordinates, Accuracy& targetAccuracy )

我是我工作中的实习生,我还没有使用过这个,所以我的理解非常有限,还有我对VB的使用(我是C++/ C# 家伙)。这里有几个问题:

1)看看Dllimport,似乎参数之外的最后一部分是返回类型。来自另一个站点的示例代码:

<DllImport("advapi32.dll")> _
  Public Function GetUserName( _
     ByVal lpBuffer As StringBuilder, _
     ByRef nSize As Integer) As Boolean

返回类型是“As Boolean”吗?如果是这样,我尝试使用“Sub”,它显示“关键字未命名类型”。因此,我为什么要考虑声明,因为我似乎可以返回 void/sub 作为返回类型。

2)尝试使用类型“CooperativeTuple”和“Accuracy”给我带来了问题,说它们没有定义。既然我认为我无法真正定义它们,那么我该如何解决这个问题,而且它们是指针这一事实又如何呢?另外 - 我无法以任何方式修改 C++ 代码,所以我所拥有的就是我所拥有的。

I am working on a project where I have to import a DLL file into a VB project that was created a few years back. The DLL was created in C++, and looks like:

void CoordinateConversionService::convert( SourceOrTarget::Enum sourceDirection, SourceOrTarget::Enum targetDirection, CoordinateTuple* sourceCoordinates, Accuracy* sourceAccuracy, CoordinateTuple& targetCoordinates, Accuracy& targetAccuracy )

I am an intern at my job, and I haven't had to use this yet, so my understanding is extremely limited, along with my usage of VB (I'm a C++/C# guy). Here are a few questions:

1) Looking at Dllimport, it seems like the last part outside of the parameters is a return type. Example code from another site:

<DllImport("advapi32.dll")> _
  Public Function GetUserName( _
     ByVal lpBuffer As StringBuilder, _
     ByRef nSize As Integer) As Boolean

Is "As Boolean" the return type? If so, I tried using "Sub" and it says "Keyword does not name a type." Hence why I looked into declare, because it seems I CAN return void/sub as a return type.

2) Trying to use the types "CoordinateTuple" and "Accuracy" give me problems, saying that they aren't defined. How do I get around this since I don't think I can really define them, and what about the fact that they're pointers? Also - I cannot modify the C++ code in any way, so what I have is what I have.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮世清欢 2024-09-13 20:52:20

在 VB 中,您可以说 Public Function What (params) As ReturnType (与 C# 中的 public ReturnType What(params) 相同)或 Public Sub What ( params) 用于不返回任何内容的内容(在 C++/C# 中返回 void)。

如果您的函数/子函数采用自定义类型,您还需要声明这些类型的 .NET 等效项。这会使 PInvoke 工作困难。 互操作助手等工具可以提供帮助。

In VB you say either Public Function Whatever (params) As ReturnType (which is the same as public ReturnType Whatever(params) in C#) or Public Sub Whatever (params) which is for things that don't return anything (return void in C++/C#).

If your function/sub takes custom types you will need to declare .NET equivalents to those as well. This can make PInvoke hard work. Tools like the interop assistant can help.

绮烟 2024-09-13 20:52:20

对于自定义类型,您可能必须在托管 C++ 中创建一个包装器,它既能够使用本机 C++ API,又能够公开托管 API。尽管已过时,但可以在此处找到演练:

For the custom types, you may have to create a wrapper in managed C++, which has the ability both to consume native C++ APIs and expose managed APIs. Although dated, a walkthrough can be found here:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文