在 C 中调用 Visual Basic DLL,第 2 部分

发布于 2024-09-18 20:05:31 字数 1140 浏览 4 评论 0原文

此问题是上一个问题(此处)的后续问题。

我从第三方供应商处获取了一个在 Visual Basic 中创建的 DLL(Sensor DLL.dll)。该 DLL 包含与传感器通信的函数,我需要从我正在编写的 Visual C++ 程序中调用这些函数。供应商不会提供头文件,而且我不懂Visual Basic。

我有关于每个功能的少量文档。例如,有人告诉我 DLL 中的一个函数 (Get_Data) 的形式如下:

Public Function Get_Data(ByVal Handle As String) As String

它将返回一个值介于 -16777216 和 +16777215 之间的字符串。仅此而已。

在上一个问题中,用户 Hans Passant 给出了以下有用的回复:

VB6 DLL 通常是 COM 服务器。事实上,您确实拥有相当于 .h 文件的文件,它嵌入了一个类型库。从项目+属性、公共属性、框架和参考开始。 “添加新引用”按钮,“浏览”选项卡,选择 DLL。

接下来,视图+对象浏览器。您应该在列表中看到生成的互操作库。打开节点看看里面有什么。您可以编写普通的托管代码(例如 gcnew)来创建 COM 对象并调用接口方法。您确实需要一些关于可用方法的最少文档才能猜测应该如何调用它们。

按照这个建议,我确实在对象浏览器中看到了 DLL 中的函数列表,包括 Get_Data(System::String) 但是我对使用 COM 对象感到困惑,这是超出我的经验。

我尝试简单地调用 Get_Data(Handle); ,其中 Handle 是 BSTR,现在我已将引用添加到我的项目中,但它未被识别。我还尝试先调用 CoInitialize,但这没有帮助。

谁能具体告诉我现在需要写什么来调用该函数?谢谢!

This question is a follow up to a previous question (here).

I have acquired a DLL that was created in Visual Basic from a third party vendor(Sensor DLL.dll). This DLL contains functions for talking to a sensor, and I need to call these functions from a Visual C++ program I am writing. The vendor will not provide a header file, and I do not know Visual Basic.

I have a small amount of documentation on each function. For example, I am told one function (Get_Data) in the DLL is of the form:

Public Function Get_Data(ByVal Handle As String) As String

which will return a string with a value between -16777216 and +16777215. That is all.

In the previous question user Hans Passant gave this helpful reply:

A VB6 DLL is normally a COM server. You do in fact have the equivalent of a .h file, it has a type library embedded in it. Start this off with Project + Properties, Common Properties, Framework and References. Add New Reference button, Browse tab, select the DLL.

Next, View + Object Browser. You should see the generated Interop library in the list. Open the node to see what is there. You write normal managed code, like gcnew, to create the COM object and call the interface methods. You do need some minimum documentation on the available methods to have a guess at how they should be called.

Following this advice I did indeed see the list of functions in the DLL in the Object Browser, including Get_Data(System::String) however I am confused about working with COM objects, which is beyond my experience.

I have tried simply calling Get_Data(Handle); where Handle is a BSTR now that I have added the reference to my project, but it was not recognized. I also tried to call CoInitialize first, but that did not help.

Can anyone tell me specifically what I need to write now to call the function? Thanks!

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

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

发布评论

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

评论(1

云裳 2024-09-25 20:05:31

如果我正确理解您的情况,您至少有一个在 VB6 .dll 中实现的 COM 对象。要使用此对象,必须在调用 GetData 方法之前创建 COM 对象的实例。 CoInitialize只会初始化COM系统。您还必须使用 CoCreateInstance 等 API。

If I correctly understand your situation, you have at least one COM object implemented in a VB6 .dll. To use this object, you have to create an instance of the COM object before calling the method GetData. CoInitialize will only initialize the COM system. You also have to use an API like CoCreateInstance.

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