在C ++中调用Visual Basic DLL,第2部分 [英] Calling a Visual Basic DLL in C++, Part 2

查看:113
本文介绍了在C ++中调用Visual Basic DLL,第2部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是对上一个问题的跟进(此处)。

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

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

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.

每个函数都有少量的文档。例如,我被告知DLL中的一个函数( Get_Data )是以下形式:

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

这将返回一个字符串, 16777216和+16777215。

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

在上一个问题用户 Hans Passant < a>给了这个有帮助的答复:

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


VB6 DLL通常是COM服务器。你确实有一个.h文件的等价物,它有一个类型库嵌入其中。使用Project +属性,公共属性,框架和引用开始此操作。添加新参考按钮,浏览选项卡,选择DLL。

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.

接下来,查看+对象浏览器。您应该在列表中看到生成的Interop库。打开节点看看有什么。您编写正常的托管代码(如gcnew)来创建COM对象并调用接口方法。你需要一些关于可用方法的最低限度的文档来猜测它们应该如何被调用。

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.

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

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.

我试过简单调用 Get_Data(Handle); 其中句柄是一个BSTR现在我已经添加了引用到我的项目,但它不被认可。我也试图调用 CoInitialize ,但是没有帮助。

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!

推荐答案

如果我正确理解你的情况,你至少有一个COM对象在VB6。要使用此对象,必须在调用方法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.

这篇关于在C ++中调用Visual Basic DLL,第2部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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