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

查看:324
本文介绍了如何从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代码

  • 还有一些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.

推荐答案

好的,在你的帮助下,一个公平的位

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标题文件获取如何创建的蓝图头文件。

  • 重要的是要了解如何编译为C不会命名mangle,而编译为C ++将。

  • 看起来DevC ++有一个简洁的 BUILDING_DLL 标志,但Visual Studio要求您在main.c文件中创建一个定义。

  • __ stdcall 执行与名称调整不同的名为名称装饰的操作,但仍会更改函数名称。感谢@slugonamission给我一个指针。它最终点击时使用dumpbin.exe,如@HansPassant建议。

  • 所以,切换到 __ cdecl 设法避免名称装饰,和编译C(或使用 extern 和编译在C + +)避免名称mangling。

  • ,并且dll最终会给我CalcGraph as有效的入口点!

  • 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:

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天全站免登陆