c# - 加载 MITab dll 在 c# 中给出 System.BadImageFormatException

标签 c# c++ dll

这个错误让我非常沮丧,我在网上找不到任何东西。

我有一个 MITab V1.7.0 dll 和一个包含以下内容的 MiApi.cs 文件:

// $Id: MiApi.cs,v 1.2 2005/03/24 17:02:06 dmorissette Exp $
//

using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections;

namespace EBop.MapObjects.MapInfo {
  /// <summary>
  /// Wrapper functions for the version 1.3.0 of the MapInfo Tab API.
  /// </summary>
  /// <remarks>
  /// Requires MITab.dll (www.maptools.org)
  /// See http://mitab.maptools.org/
  /// 
  /// Graham Sims
  /// Environment Bay of Plenty, Whakatane, New Zealand
  /// http://www.envbop.govt.nz
  /// </remarks>
  public class MiApi {

    // -- enums left out for brevity //

    private MiApi() {
    }

    /// <summary>
    /// Returns the version of the library. 
    /// </summary>
    /// <returns>An integer representing the current version of the MITAB library in the 
    /// format xxxyyyzzz, e.g. returns 1002004 for v1.2.4.  
    /// </returns>
    [DllImport("mitab.dll")]
    public static extern int mitab_c_getlibversion();

    // -- Other entry points -- //

  }
}

当我尝试访问它时;

int version = MiApi.mitab_c_getlibversion();

我明白了;

A first chance exception of type 'System.BadImageFormatException' occurred in BBX.IAP.MapInfoProcessor.exe

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

标准响应是我在 64 位应用程序中加载 32 位库或在 32 位应用程序中加载 64 位库,但我在这两种应用程序中都重新编译了,并且也重新编译了库。我还检查了符号并使用 cdb 来检查访问库的方式,但我无法从输出中获得太多意义。

214c:2f54 @ -2131284890 - LdrLoadDll - ENTER: DLL name: C:\Users\Owen\X\deps\Debug\MITab.dll
214c:2f54 @ -2131284890 - LdrpLoadDll - ENTER: DLL name: C:\Users\Owen\X\deps\Debug\MITab.dll
214c:2f54 @ -2131284890 - LdrpLoadDll - INFO: Loading DLL C:\Users\Owen\X\deps\Debug\MITab.dll
214c:2f54 @ -2131284890 - LdrpFindOrMapDll - ENTER: DLL name: C:\Users\Owen\X\deps\Debug\MITab.dll
214c:2f54 @ -2131284890 - LdrpResolveDllName - ENTER: DLL name: C:\Users\Owen\X\deps\Debug\MITab.dll
214c:2f54 @ -2131284890 - LdrpResolveDllName - RETURN: Status: 0x00000000
214c:2f54 @ -2131284890 - LdrpMapViewOfSection - ENTER: DLL name: C:\Users\Owen\X\deps\Debug\MITab.dll
ModLoad: 00000000`1b680000 00000000`1b813000   C:\Users\Owen\X\deps\Debug\MITab.dll
214c:2f54 @ -2131284890 - LdrpMapViewOfSection - RETURN: Status: 0x4000000e
214c:2f54 @ -2131284890 - LdrpFindOrMapDll - RETURN: Status: 0xc000007b
214c:2f54 @ -2131284890 - LdrpLoadDll - RETURN: Status: 0xc000007b
214c:2f54 @ -2131284890 - LdrLoadDll - RETURN: Status: 0xc000007b
214c:2f54 @ -2131284890 - LdrLoadDll - ENTER: DLL name: MITab.dll
214c:2f54 @ -2131284890 - LdrpLoadDll - ENTER: DLL name: MITab.dll
214c:2f54 @ -2131284890 - LdrpLoadDll - INFO: Loading DLL MITab.dll
214c:2f54 @ -2131284890 - LdrpFindOrMapDll - ENTER: DLL name: MITab.dll
214c:2f54 @ -2131284890 - LdrpFindKnownDll - ENTER: DLL name: MITab.dll
214c:2f54 @ -2131284890 - LdrpFindKnownDll - RETURN: Status: 0xc0000135
214c:2f54 @ -2131284890 - LdrpSearchPath - ENTER: DLL name: MITab.dll

我什至已经使用 dumpbin 确认该符号存在于 dll 中

1610  649 000015E1 _mitab_c_getlibversion@0 = @ILT+1500(_mitab_c_getlibversion@0)

最糟糕的是,我的程序输出表明它过去曾成功运行。我只剩下沮丧和困惑。

谁能告诉我我做错了什么?

最佳答案

好的...

尽管我发誓我已经确定它们都是 x64 的......但我错了。

MiTab.dll 是一个旧的库,它被设置为 Win32。我不得不深入了解编译说明,找到它设置为 Win32 的位置并进行更改。错误立即消失。

教训:即使您的所有设置都表明代码生成为 Win32 或 x64,也要检查它。

关于c# - 加载 MITab dll 在 c# 中给出 System.BadImageFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37200443/

相关文章:

c# - 多行文本框打断字符

c++ - Powerbuilder 实现 SFTP 功能

c++ - Visual Studio C++ : When should I be using __declspec(dllimport)?

c++ - 在 Visual Studio 2015 中创建一个 dll 库

c# - 向 XNA 游戏添加类似输入框的控件

C# main 函数无效

c# - 将 DataTable 插入存储过程中的表中

c++ - 使用我的配置类 (C++) 无法解析的外部符号

模板内部类中的 C++ decltype

c++ - c++中数组的动态内存分配