在C ++中调用Visual Basic DLL [英] Calling a Visual Basic DLL in C++

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

问题描述

我已经从第三方供应商( Sensor DLL.dll )获取了在Visual Basic中创建的DLL。这个DLL包含与传感器通信的函数,我需要从我正在编写的Visual C ++程序中调用这些函数。供应商不会提供头文件,我不知道Visual Basic。如果我有一个头文件,这将是一个15分钟的项目...而是我仍然在一个星期后挣扎。请帮助!



我被告知DLL中的一个函数( Get_Data )是以下形式:

 公共函数Get_Data(ByVal Handle As String)As String 

我试过几种方法调用这个Get_Data函数没有成功:



方法1) DllImport属性

  #using< mscorlib.dll> 
using namespace System :: Runtime :: InteropServices;

namespace Sensor
{

[DllImport .dll,EntryPoint =Get_Data,CharSet = System :: Runtime :: InteropServices :: CharSet :: Unicode)]
BSTR Get_Data(BSTR Handle);
}

  //然后我调用函数
Sensor :: Get_Data(Handle);

这种方法似乎是我最接近sloution的方法。它编译,但运行时会出现以下错误:



发生类型为System.EntryPointNotFoundException的未处理异常



其他信息:无法在DLL'Sensor DLL.dll'中找到名为Get_Data的入口点。



除了BSTR之外的各种数据类型组合/排列,包括BSTR *,wchar_t,int等。可能我错过了一个,但每个数据类型返回相同的错误。



方法2) dllimport storage-class attribute

  __ declspec(dllimport)BSTR Get_Data(BSTR Handle); 

//然后我调用函数
Get_Data(Handle);

这种方法让我很困惑,因为我没有指定我想要导入的DLL。我已经将DLL复制到项目文件夹,我已手动添加到项目,所以希望这意味着它可以找到。当编译链接器时返回以下错误:




错误LNK2028:未解析的标记(0A00034F)wchar_t * __cdecl Get_Data(wchar_t *)在主函数int __cdecl main(void)(?main @@ $$ HYAHXZ)



中引用的函数(?Get_Data @@ $$ FYAPA_WPA_W @ Z)LNK2019:unresolved在函数int __cdecl main(void)(?main @@ $$ HYAHXZ)中引用的外部符号wchar_t * __cdecl Get_Data(wchar_t *)(?Get_Data @@ $$ FYAPA_WPA_W @ Z)



我怀疑也许这意味着我应该使用wchar_t或wchar_t *而不是BSTR,但改为任何一种数据类型都会导致相同的错误。



方法3) GetProcAddress

  typedef BSTR(* Get_Data_Ptr) (BSTR手柄); 
HINSTANCE LoadMe;
LoadMe = LoadLibraryA(Sensor DLL.dll);

if(!LoadMe)
std :: cout<< \\\
DLL加载失败!\\\
;

Get_Data_Ptr LibMainGet_Data;
LibMainGet_Data =(Get_Data_Ptr)GetProcAddress(LoadMe,Get_Data);

//然后我调用函数
LibMainGet_Data(Handle);

这将编译,但运行时会出现以下错误:




发生类型为System.AccessViolationException的未处理异常



其他信息:尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

当我在调试模式下将鼠标悬停在此代码的各个部分时,似乎与第一种方法一样,在DLL中找到Get_Data入口点。



有没有人使用C ++从VB DLL调用函数,当你没有自己制作DLL,有.idl文件等?有没有人有这样的工作示例,你可以分享?

谢谢!

解决方案

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



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


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. If I had a header file this would be a 15 minute project... instead I am still struggling with it a week later. Please Help!

I am told one function (Get_Data) in the DLL is of the form:

Public Function Get_Data(ByVal Handle As String) As String

I have tried several methods for calling this Get_Data function with no success:

Method 1) the DllImport attribute

#using <mscorlib.dll>
using namespace System::Runtime::InteropServices; 

namespace Sensor
{

[DllImport("Sensor DLL.dll", EntryPoint = "Get_Data", CharSet = System::Runtime::InteropServices::CharSet::Unicode)] BSTR Get_Data(BSTR Handle); }

//then I call the function
Sensor::Get_Data(Handle);

This method seems to be the closest I have gotten to a sloution. It compiles, but gives the following error when it runs:

An unhandled exception of type 'System.EntryPointNotFoundException' occurred

Additional information: Unable to find an entry point named 'Get_Data' in DLL 'Sensor DLL.dll'.

I have tried various datatype combinations/permutations besides BSTR including BSTR*, wchar_t, int, etc. It is possible that I missed one, but each datatype returns the same error.

Method 2) dllimport storage-class attribute

__declspec(dllimport) BSTR Get_Data(BSTR Handle);

//then I call the function
Get_Data(Handle);

This method is confusing to me because I don't specify the DLL I want to import from. I have copied the DLL to the project folder and I have added it to the project manually, so hopefully that means it can be found. When I compile the linker returns the following errors:

error LNK2028: unresolved token (0A00034F) "wchar_t * __cdecl Get_Data(wchar_t *)" (?Get_Data@@$$FYAPA_WPA_W@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)

error LNK2019: unresolved external symbol "wchar_t * __cdecl Get_Data(wchar_t *)" (?Get_Data@@$$FYAPA_WPA_W@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)

I suspected maybe this meant I should be using wchar_t or wchar_t* instead of BSTR, but changing to either datatype results in the same error.

Method 3) GetProcAddress

typedef BSTR (*Get_Data_Ptr)(BSTR Handle);  
HINSTANCE LoadMe;
LoadMe = LoadLibraryA("Sensor DLL.dll");

if (!LoadMe)
    std::cout << "\nDLL failed to load!\n";

Get_Data_Ptr LibMainGet_Data;  
LibMainGet_Data = (Get_Data_Ptr)GetProcAddress(LoadMe,"Get_Data");

//then I call the function
LibMainGet_Data(Handle);

This will compile, but gives the following error when run:

An unhandled exception of type 'System.AccessViolationException' occurred

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

When I mouse over the various parts of this code in debug mode it seems that, like the first method, it was also unable to find the 'Get_Data' entry point in the DLL.

Has anyone called functions from a VB DLL using C++ when you haven't made the DLL yourself and you don't have .idl files, etc? Does anyone have a working example like this you could share?
Thanks!

解决方案

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.

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

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