VB.NET calling a function in non-.NET Visual-C++ DLL gets 'An attempt was made to load a program with an incorrect format.'

-1

Am using: Windows 10 64-bit and Visual Studio 2017 Pro.

I built a Visual-C++ DLL. config=active(Debug) Platform=Active(Win32) Platform_Target=Windows10

I also built a VB.NET windows forms GUI. config=active(Debug) Platform=Active(Any CPU) Target_CPU=Any CPU .NET 4.5.1

DLL has this empty function:

extern "C" BASICDLL_API  void __stdcall test_empty_function(void)
{
}

VB.NET has this DLL declaration:

Imports System.Runtime.InteropServices

Module main_board_interface

    Public Class NativeMethods

        <DllImport("MyDll.dll")>
        Public Shared Sub test_empty_function()
        End Sub

VB.NET calls the empty DLL function when I click VB.NET button here....

Private Sub Button_test_main_board_Click(sender As Object, e As EventArgs) Handles Button_test_main_board.Click

    main_board_interface.NativeMethods.test_empty_function() '<<< causes error

At this last line, I get this error:

    System.BadImageFormatException: 
'An attempt was made to load a program with an incorrect format.
(Exception from HRESULT: 0x8007000B)'
.net
vb.net
visual-c++
dll
asked on Stack Overflow Jul 21, 2017 by Doug Null • edited Jul 23, 2017 by Paul Karam

1 Answer

2

Sounds like you're trying to load a 32-bit DLL in a 64-bit process.

Change your .Net project to 32-bit only.

answered on Stack Overflow Jul 21, 2017 by SLaks

User contributions licensed under CC BY-SA 3.0