无法将我的Visual-C ++ DLL添加到我的VB.NET Windows窗体GUI应用程序 [英] Trouble adding my Visual-C++ DLL to my VB.NET windows forms GUI app

查看:75
本文介绍了无法将我的Visual-C ++ DLL添加到我的VB.NET Windows窗体GUI应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows 10和Visual Studio 2017。

Windows 10 and Visual Studio 2017.

要引用VB.NET项目中的DLL:
I DID vb.net项目属性>添加>参考>浏览>我的DLL在此vb.net项目的项目目录中>添加>选中DLL>确定

To reference the DLL in the VB.NET project: I DID vb.net project properties > Add > Reference > Browse > my DLL in project directory of this vb.net project > Add > checkmark DLL > OK

...,我得到此错误:参考[ .dll]。

...and I get this error: "A reference [.dll] could not be added."

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

Here is Visual-C++ 2017pro DLL project properties ...

此处是VB.NET调用的DLL中的函数(添加extern C和__stdcall之后):

Here is function in DLL called by VB.NET (after addition of 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中。...

Here are VB.NET declarations for Fns. within 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()方法的命令按钮时,出现以下错误:

After adding the DLL and running the program, I got below error when I clicked the command button that called the Connect() method in the DLL:

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()

在向DLL connect添加extern C和__stdcall'decorations'后,我仍然收到此错误

I still get this error after addition of extern "C" and __stdcall 'decorations' to DLL connect() function.

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

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

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

DLL config & platform are: Active(Debug) & Active(Win32)

VB.NET配置&平台是:Active(Debug)&活动(任何CPU)

VB.NET config & platform are: Active(Debug) & Active(Any CPU)

笔记本电脑Windows 10是64位

Laptop Windows 10 is 64-bit

将DLL平台更改为x64吗?

Change DLL platform to x64 ?

推荐答案

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

There's a difference between adding a reference to the DLL and P/Invoking it. You can only add a reference to .NET DLLs because it is a way of directly accessing the DLL's members in a .NET-friendly way. Since both assemblies are of the same language (IL) the DLL can easily be compiled and referenced by the regular application.

P /调用与之完全不同,因为您可以编组调用到本机DLL,该DLL已被编译为纯机器代码。不能将其添加为引用,因为编译器无法将其链接到.NET代码,因为它仅理解.NET语言和IL。

P/Invoking however is completely different because there you are marshalling calls to a native DLL, which is already compiled into pure machine code. It cannot be added as a reference because the compiler cannot link it to the .NET code since it only understands .NET languages and IL.

将DLL添加到项目中您必须改为将其添加为宽松文件:

To add the DLL to your project you have to add it as a loose file instead:


  1. 中右键单击项目解决方案资源管理器并转到添加>现有项目...



  • 找到您的本机DLL并将其选中(但不要添加)。然后按添加按钮上的小箭头,然后选择添加为链接

  • Locate your native DLL and select it (but do not add it). Then press the little arrow on the Add button and select Add As Link.


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

    • By adding the DLL as a link (shortcut) you always refer to the original file. Thus if you ever update/recompile the DLL you won't have to re-add it to your VB.NET project.


  • 解决方案资源管理器中选择您的DLL。



  • 进入属性窗口并将复制到输出目录更改为始终复制


    • 这将确保您的DLL始终复制到输出目录( bin\Debug bin\发布)。

    • This will make sure that your DLL is always copied to the output directory (bin\Debug or bin\Release) every time you compile the project.


  • 完成!

  • Done!


    li>

  • 这篇关于无法将我的Visual-C ++ DLL添加到我的VB.NET Windows窗体GUI应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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