加载 DLL 导致错误 193,即使它可以在 ctypes 和 JNA 中加载

Loading DLL results in error 193, even though it can load in both ctypes and JNA

我正在将 Python 程序移植到 C++(在 Windows 上)。由于上述代码依赖于 DLL,因此我需要加载 DLL。但是,当我调用 LoadLibraryA 指定我的 DLL 的完整路径(带反斜杠)时,它无法加载并且 GetLastError returns 193.

这里有一个示例程序来演示:

#include <iostream>
#include <windows.h>

using std::cout, std::endl;

int main(int argc, char* argv[]) {
  if (argc != 2) {
    cout << "You only need to specify one argument, path to DLL." << endl;
    exit(2);
  }

  HMODULE dll = LoadLibraryA(argv[1]);
  if (dll == NULL) {
    cout << "DLL at " << argv[1] << " could not be loaded." << endl;
  }
  FreeLibrary(dll);
  return 0;
}

我指定了我的 DLL 的完整路径(如“C:\path\to\somewhere\test.dll”)作为第一个命令行参数,但它失败了。为什么它不会加载,即使它在 JNA 和 ctypes 等外部函数接口中加载?

编辑 1:DLL 是 64 位的。

检查您编译的是 64 位还是 32 位代码的一种简单方法是使用以下代码 (C++17):

#include <iostream>

using std::cout, std::endl;

int main(int argc, char* argv[]) {
  cout << "Your compiler compiled for " << sizeof(void*) * 8 << "-bit systems." << endl;
}

上面的代码通过检查指针的大小来工作。 32位应用程序使用4字节指针,64位应用程序使用8字节指针。

编辑: 您可以使用 dumpbin 检查(可用于 Visual Studio)。参见 this answer。您还可以使用预处理器宏:

#include <iostream>

using std::cout, std::endl;

int main(int argc, char* argv[]) {
  #ifdef __MINGW64__
  cout << "MinGW-w64" << endl;
  #endif
}

如果它是由 MinGW-w64 编译的,这只会打印 "MinGW-w64"。这些方法比我原来用的指针法靠谱多了。

就我而言,我使用的是 MinGW 而不是 MinGW-w64。因此,生成的 .exe 是一个 32 位应用程序,它与 64 位应用程序发生冲突 .dll,进而导致错误 193。