无法将我的Visual-C ++ DLL添加到我的VB.NET窗口GUI应用程序

时间:2017-07-21 01:27:02

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

Windows 10和Visual Studio 2017。

在VB.NET项目中引用DLL: 我DID vb.net项目属性>添加>参考>浏览>我在这个vb.net项目的项目目录中的DLL>添加> checkmark DLL>行

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

这是Visual-C ++ 2017pro DLL项目属性... enter image description here

这是由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

enter image description here

错误细节......

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&#34; C&#34;之后我仍然会收到此错误和__stdcall&#39;装饰&#39;到DLL connect()函数。

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

DLL配置&amp;平台是:主动(调试)&amp;活性的(Win32)

VB.NET config&amp;平台是:主动(调试)&amp;活动(任何CPU)

笔记本电脑Windows 10是64位

将DLL平台更改为x64?

1 个答案:

答案 0 :(得分:2)

添加对DLL的引用和P /调用它之间存在差异。您只能添加对.NET DLL的引用,因为它是一种以.NET友好的方式直接访问DLL成员的方法。由于两个程序集都是相同的语言(IL),因此常规应用程序可以轻松地编译和引用DLL。

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

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

  1. Solution Explorer中右键点击您的项目,然后转到Add > Existing Item...

      

    Add an existing item

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

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

    Add you DLL as link

  3. Solution Explorer

    中选择您的DLL
      

    Select the DLL in the Solution Explorer

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

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

    Change Copy to Output Directory to Copy Always

  5. 完成!

      

    Profit!