简体   繁体   English

如何从Visual Basic 2010调用C ++ dll文件

[英]How to call a C++ dll file from Visual Basic 2010

I'm using Microsoft Visual Studio 2010 Express: the C++ and VB versions. 我正在使用Microsoft Visual Studio 2010 Express:C ++和VB版本。

  • There is some VB code that forms the body of a program, and all the GUI. 有一些VB代码构成了程序的主体以及所有GUI。
  • There is also some C++ code that does some fast processing (lots of loops). 还有一些C ++代码可以进行一些快速处理(很多循环)。

I am trying to call the C++ code, compiled as a dll, using: 我正在尝试使用以下方式调用编译为dll的C ++代码:

Private Declare Sub CalcGraph Lib "Model.dll" ()


And at the moment keep getting the error: 目前,不断出现错误:

Unhandled exception has occurred in your application. 您的应用程序中发生了未处理的异常。 Unable to find an entry point named 'CalcGraph' in DLL 'Model.dll' 在DLL'Model.dll'中找不到名为'CalcGraph'的入口点

Could someone explain how to correctly call the DLL, please? 有人可以解释如何正确调用DLL吗?
Do you need any other information to better understand the problem? 您是否需要其他信息以更好地理解问题?

I'm fairly new to programming, so please be patient with me! 我刚开始编程,所以请耐心等待!
That said, I'm prepared to do the leg-work, and have already spent quite a while reading around on this and other sites. 就是说,我已经准备好进行腿部工作了,并且已经花了很多时间在此站点和其他站点上阅读。 Nothing seems to match quite well enough to help me understand what's going wrong. 似乎没有什么能很好地匹配我,以帮助我了解问题所在。

Okay, with your help and a fair bit of Google, this finally works! 好的,在您的帮助和相当多的Google的帮助下,这终于可以了!

Here's a run-down, in case it helps anyone else in the future: 这是一个破败,以防将来对其他人有帮助:

  • Use the Ultimate Header File for a blueprint of how to create the header file. 使用终极头文件获取如何创建头文件的蓝图。
  • It is important to understand how compiling as C will not name mangle, whereas compiling as C++ will. 重要的是要了解以C进行编译不会如何命名,而以C ++进行编译会如何命名。
  • It also appears that DevC++ has a neat BUILDING_DLL flag but Visual Studio requires you to create a definition in your main.c file. 还似乎DevC ++具有一个整洁的BUILDING_DLL标志,但是Visual Studio要求您在main.c文件中创建一个定义。
  • __stdcall does something called 'name decoration' that is different from name mangling but will still change your function name. __stdcall “名称修饰”操作与名称修饰不同,但仍会更改函数名称。 Thanks to @slugonamission for giving me a pointer about this. 感谢@slugonamission为我提供了有关此方面的指导。 It finally clicked when using dumpbin.exe, as suggested by @HansPassant. 如@HansPassant所建议的,当使用dumpbin.exe时,它最终单击。
  • so, switching to __cdecl manages to avoid name decoration, and compiling in C (or using extern and compiling in C++) avoids name mangling. 因此,切换到__cdecl可以避免名称修饰,而在C中进行编译(或在C ++中使用extern和compile)可以避免名称混乱。
  • and the dll will finally give me CalcGraph as a valid entry point! dll最终将给我CalcGraph作为有效的入口点!

The Implicit / Explicit dll linking is a very important distinction. 隐式/显式dll链接是一个非常重要的区别。 Implicit linking requires a .lib file, the .dll and perhaps also a .h file. 隐式链接需要一个.lib文件,.dll文件以及一个.h文件。 Explicit linking is what I was after - you can get away with the .dll by itsself. 显式链接是我要做的事情-您可以自己摆脱.dll的困扰。 Thanks to @squelos for the link explaining this. 感谢@squelos提供了解释此问题的链接。

And last but not least: 最后但并非最不重要:

In the dll: 在dll中:

extern _COMPILING_ void __cdecl CalcGraph(PanelParameters *, Calculations *);

And in the VB code: 在VB代码中:

Imports System.Runtime.InteropServices

Private Declare Sub CalcGraph Lib "myDLL.dll" (ByRef params As Parameters, _
ByRef calcs As Calculations)

And it finally worked! 终于成功了!

I'm going to assume here the C++ DLL is written in pure C++ (not C++/CLI or anything like that). 我要假设这里的C ++ DLL是用纯C ++编写的(不是C ++ / CLI或类似的东西)。 It seems that the VB Lib keyword will only be importing a .NET procedure, rather than a native procedure. 似乎VB Lib关键字将仅导入.NET过程,而不是本机过程。 Instead, you need to use P/Invoke . 相反,您需要使用P / Invoke

Something like this may work 这样的事情可能会起作用

<DllImport("Model.dll")>
Public Shared Function CalcGraph
End Function

But of course, fill in your parameters and return types too. 但是,当然,也要填写参数和返回类型。

In some cases, depending on how the DLL was generated (ordinal or by name) you may have to use the DLL with GetProcAddress 在某些情况下,取决于DLL的生成方式(常规或按名称),您可能必须将DLL与GetProcAddress一起使用

This documentation here can give you a quick overview of how to use DLL's the oldschool way MSDN Linking overview 这里的文档可以为您提供有关如何使用DLL的老式方法的快速概述MSDN链接概述

And finally, DumpBin can you help you out a lot, by allowing you to inspect a DLL (pretty useful if you dont have the sources or documentation ) 最后, DumpBin可以通过允许您检查DLL来为您提供很多帮助(如果没有源代码或文档,这很有用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM