【问题标题】:Trouble adding my Visual-C++ DLL to my VB.NET windows forms GUI app将我的 Visual-C++ DLL 添加到我的 VB.NET Windows 窗体 GUI 应用程序时遇到问题
【发布时间】:2017-12-26 21:48:33
【问题描述】:

Windows 10 和 Visual Studio 2017。

在 VB.NET 项目中引用 DLL: 我做了 vb.net 项目属性 > 添加 > 参考 > 浏览 > 我在这个 vb.net 项目的项目目录中的 DLL > 添加 > 选中 DLL > OK

...我收到此错误:“无法添加引用 [.dll]。”

这里是 Visual-C++ 2017pro DLL 项目属性...

这是由 VB.NET 调用的 DLL 中的函数(在添加 extern "C" 和 __stdcall 之后):

 extern "C" BASICDLL_API  int __stdcall Connect()
{
    char manufacturer[] = "Acme Inc.  ";
    char product[] = "System          ";
    return BDLL_Connect(manufacturer, product);
}

这里是 Fns 的 VB.NET 声明。在 DLL 中......

Imports System.Runtime.InteropServices

Module main_board_interface

    Public Class NativeMethods
        <DllImport("myDLL.dll")>
        Public Shared Function Connect() As Integer
        End Function

        <DllImport("myDLL.dll")>
        Public Shared Function Read_Parameters(ByVal board As Byte, ByRef params As UInt16()) As Integer
        End Function

        <DllImport("myDLL.dll")>
        Public Shared Function Write_Parameter(ByVal board As Byte, ByRef param_ID As Byte, value As Int32) As Integer
        End Function

        <DllImport("myDLL.dll")>
        Public Shared Function Save_Parameter(ByVal board As Byte, ByRef param_ID As Byte) As Integer
        End Function

        <DllImport("myDLL.dll")>
        Public Shared Function Disconnect() As Integer
        End Function
    End Class

End Module

..................................

添加 DLL 并运行程序后,单击 DLL 中调用 Connect() 方法的命令按钮时出现以下错误:

Private Sub Button_test_main_board_Click(sender As Object, e As EventArgs) Handles Button_test_main_board.Click
    Dim return_status = main_board_interface.NativeMethods.Connect()  <<<<<<<<<<<<<<  BELOW ERROR HERE.
    If return_status = 0 Then
        TextBox_main_board_comm.Text = "Connection with Main Board V1" & vbCrLf
    Else
        TextBox_main_board_comm.Text = "No connection with Main Board V1" & vbCrLf
        Return
    End If

错误详情...

System.BadImageFormatException occurred
  HResult=0x8007000B
  Message=An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
  Source=v1
  StackTrace:
   at NationalInstruments.Examples.ContAcqVoltageSamples_IntClk.main_board_interface.NativeMethods.Connect()
   at NationalInstruments.Examples.ContAcqVoltageSamples_IntClk.MainForm.Button_test_main_board_Click(Object sender, EventArgs e) in C:\PRIMARY\...\WORK\SYSTEM GUI V1\MainForm.vb:line 1579
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at NationalInstruments.Examples.ContAcqVoltageSamples_IntClk.MainForm.Main()

将 extern "C" 和 __stdcall 'decorations' 添加到 DLL connect() 函数后,我仍然收到此错误。

......

DLL 配置和平台是:Active(Debug) & Active(Win32)

VB.NET 配置和平台是:Active(Debug) & Active(Any CPU)

笔记本电脑 Windows 10 是 64 位

将 DLL 平台更改为 x64 ?

【问题讨论】:

  • 它在屏幕截图中大声喊叫:“没有公共语言运行时支持”。所以添加参考是行不通的。假设您使用extern "C" 并声明它们__stdcall,pinvoke 声明应该足够好。您所要做的就是确保可以在运行时找到 myDll.dll 文件。通过使用 Project > Add Existing Item > 选择 DLL 来执行此操作。将其“复制到输出目录”属性设置为“如果较新则复制”。
  • 汉斯,我可以将 Visual-C++ DLL 项目属性 > 公共语言运行时支持从“否...”设置为“公共语言运行时支持”吗? (另外,extern "C" __stdcall )
  • 这并不容易,你必须编写 C++/CLI 代码。 public ref class 是到达那里的必要关键字。
  • 唉。然后我继续下面的答案......
  • 构建,但调用 DLL 时出现错误:System.BadImageFormatException:'试图加载格式不正确的程序。 (来自 HRESULT 的异常:0x8007000B)'

标签: .net vb.net visual-studio visual-c++ dll


【解决方案1】:

添加对 DLL 的引用和 P/Invoking 它是有区别的。您只能添加对 .NET DLL 的引用,因为它是一种以 .NET 友好的方式直接访问 DLL 成员的方法。由于两个程序集使用相同的语言 (IL),因此 DLL 可以很容易地被常规应用程序编译和引用。

然而,

P/Invoking 则完全不同,因为您在 编组 调用已编译为纯机器代码的本机 DLL。它不能作为参考添加,因为编译器无法将其链接到 .NET 代码,因为它只理解 .NET 语言和 IL。

要将 DLL 添加到您的项目中,您必须将其添加为松散文件:

  1. Solution Explorer 中右键单击您的项目,然后转到Add &gt; Existing Item...

  2. 找到您的本地 DLL 并选择它(但不要添加它)。然后按Add按钮上的小箭头并选择Add As Link

    • 通过将 DLL 添加为链接(快捷方式),您始终可以引用原始文件。因此,如果您更新/重新编译 DLL,您将不必将其重新添加到您的 VB.NET 项目中。

  3. Solution Explorer 中选择您的 DLL。

  4. 转到Properties Window 并将Copy to Output Directory 更改为Copy always

    • 这将确保每次编译项目时始终将 DLL 复制到输出目录(bin\Debugbin\Release)。

  5. 完成!

【讨论】:

  • 那么,出于好奇,我是否也可以通过配置项目属性将我的 Visual-C++ DLL 编译为 .NET DLL?
  • @DougNull :不是真的没有。你必须为此使用 C++/CLR,而不是 C++/Win32。创建一个名为Class Library 的新项目。注意:语法不同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
  • 2017-12-26
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多