【问题标题】:VB.NET calling a function in non-.NET Visual-C++ DLL gets 'An attempt was made to load a program with an incorrect format.'VB.NET 在非 .NET Visual-C++ DLL 中调用函数得到“试图加载格式不正确的程序。”
【发布时间】:2023-04-11 07:03:02
【问题描述】:

正在使用:Windows 10 64 位和 Visual Studio 2017 Pro。

我构建了一个 Visual-C++ DLL。 config=active(Debug) Platform=Active(Win32) Platform_Target=Windows10

我还构建了一个 VB.NET windows 窗体 GUI。 config=active(Debug) Platform=Active(Any CPU) Target_CPU=Any CPU .NET 4.5.1

DLL 有这个空函数:

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

VB.NET 有这个 DLL 声明:

Imports System.Runtime.InteropServices

Module main_board_interface

    Public Class NativeMethods

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

单击此处的 VB.NET 按钮时,VB.NET 调用空 DLL 函数....

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

在最后一行,我得到这个错误:

    System.BadImageFormatException: 
'An attempt was made to load a program with an incorrect format.
(Exception from HRESULT: 0x8007000B)'

【问题讨论】:

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


【解决方案1】:

听起来您正试图在 64 位进程中加载​​ 32 位 DLL。

将您的 .Net 项目更改为仅 32 位。

【讨论】:

    猜你喜欢
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 2012-05-01
    • 2014-02-10
    • 2015-10-13
    • 1970-01-01
    相关资源
    最近更新 更多