Windows 10 and Visual Studio 2017.
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
...and I get this error: "A reference [.dll] could not be added."
Here is Visual-C++ 2017pro DLL project properties ...
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);
}
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
..........................................
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
ERROR DETAILS...
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()
I still get this error after addition of extern "C" and __stdcall 'decorations' to DLL connect() function.
.............
DLL config & platform are: Active(Debug) & Active(Win32)
VB.NET config & platform are: Active(Debug) & Active(Any CPU)
Laptop Windows 10 is 64-bit
Change DLL platform to x64 ?
extern "C"
and declared them__stdcall
. All you have to do is make sure that the myDll.dll file can be found at runtime. Do so by using Project > Add Existing Item > select the DLL. Set its "Copy to Output Directory" property to Copy if Newer. – Hans Passantpublic ref class
are essential keywords to get there. – Hans Passant