如何从 Visual Basic 2010 调用 C++ dll 文件 [英] How to call a C++ dll file from Visual Basic 2010

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

问题描述

我使用的是 Microsoft Visual Studio 2010 Express:C++ 和 VB 版本.

I'm using Microsoft Visual Studio 2010 Express: the C++ and VB versions.

  • 有一些 VB 代码构成了程序主体和所有 GUI.
  • 还有一些 C++ 代码可以进行一些快速处理(大量循环).

我正在尝试调用编译为 dll 的 C++ 代码,使用:

I am trying to call the C++ code, compiled as a dll, using:

私有声明子CalcGraph LibModel.dll"()


目前不断出现错误:

您的应用程序中发生了未处理的异常.无法在 DLL 'Model.dll' 中找到名为 'CalcGraph' 的入口点

Unhandled exception has occurred in your application. Unable to find an entry point named 'CalcGraph' in DLL 'Model.dll'

有人能解释一下如何正确调用DLL吗?
您是否需要任何其他信息来更好地了解问题?

Could someone explain how to correctly call the DLL, please?
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.

推荐答案

好的,在你的帮助和 Google 的帮助下,这终于奏效了!

Okay, with your help and a fair bit of Google, this finally works!

这是一个破败,以防它在未来帮助其他人:

Here's a run-down, in case it helps anyone else in the future:

  • 使用 Ultimate Header File 获取如何创建头文件的蓝图.
  • 了解如何编译为 C 不会命名 mangle,而编译为 C++ 是很重要的.
  • DevC++ 似乎也有一个简洁的 BUILDING_DLL 标志,但 Visual Studio 要求您在 main.c 文件中创建一个定义.
  • __stdcall 做了一些称为名称修饰"的事情,它不同于名称修饰,但仍会更改您的函数名称.感谢@slugonamission 为我提供了有关此的提示.正如@HansPassant 所建议的那样,它最终在使用 dumpbin.exe 时点击了.
  • 因此,切换到 __cdecl 可以避免名称修饰,并且在 C 中编译(或使用 extern 并在 C++ 中编译)避免了名称修饰.
  • dll 最终会给我 CalcGraph 作为一个有效的入口点!
  • 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.
  • 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.
  • __stdcall does something called 'name decoration' that is different from name mangling but will still change your function name. Thanks to @slugonamission for giving me a pointer about this. It finally clicked when using dumpbin.exe, as suggested by @HansPassant.
  • so, switching to __cdecl manages to avoid name decoration, and compiling in C (or using extern and compiling in C++) avoids name mangling.
  • and the dll will finally give me CalcGraph as a valid entry point!

隐式/显式 dll 链接是一个非常重要的区别.隐式链接需要一个 .lib 文件、.dll 和一个 .h 文件.显式链接是我所追求的 - 您可以自己摆脱 .dll.感谢@squelos 提供解释这一点的链接.

The Implicit / Explicit dll linking is a very important distinction. Implicit linking requires a .lib file, the .dll and perhaps also a .h file. Explicit linking is what I was after - you can get away with the .dll by itsself. Thanks to @squelos for the link explaining this.

最后但并非最不重要的:

And last but not least:

在dll中:

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

在 VB 代码中:

Imports System.Runtime.InteropServices

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

终于成功了!

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

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