【发布时间】: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