我正在尝试构建一个可以使用 ctypes.windll.loadlibrary(...) 在 python 中加载的 C DLL
我可以在 C 中创建一个 DLL 和一个客户端程序,它们按照http://www.mingw.org/wiki/MSVC_and_MinGW_DLLs上的 MinGW 教程工作。
当我尝试在 python 中加载相同的 DLL 时,出现错误:
OSError: [WinErrror 193] %1 is not a valid Win32 application
有人可以告诉我我做错了什么吗?
以下是文件:
噪音dll.h
#ifndef NOISE_DLL_H
#define NOISE_DLL_H
// declspec will identify which functions are to be exported when
// building the dll and imported when 'including' this header for a client
#ifdef BUILDING_NOISE_DLL
#define NOISE_DLL __declspec(dllexport)
#else
#define NOISE_DLL __declspec(dllimport)
#endif
//this is a test function to see if the dll is working
// __stdcall => use ctypes.windll ...
int __stdcall NOISE_DLL hello(const char *s);
#endif // NOISE_DLL_H
噪音dll.c
#include <stdio.h>
#include "noise_dll.h"
__stdcall int hello(const char *s)
{
printf("Hello %s\n", s);
return 0;
}
我使用以下方法构建 DLL:
gcc -c -D BUILDING_NOISE_DLL noise_dll.c
gcc -shared -o noise_dll.dll noise_dll.o -Wl,--out-implib,libnoise_dll.a
python代码很简单:
import ctypes
my_dll = ctypes.windll.LoadLibrary("noise_dll")
我收到上面的错误:'%1 不是有效的 Win32 应用程序'
我知道 DLL 并非完全错误,因为如果我创建一个客户端文件:
噪音客户端.c
#include <stdio.h>
#include "noise_dll.h"
int main(void)
{
hello("DLL");
return 0;
}
并建立:
gcc -c noise_client.c
gcc -o noise_client.exe noise_client.o -L. -lnoise_dll
我得到一个工作可执行文件。我对上面的代码、选项和预处理器指令中发生的所有事情都有一些了解,但是对于如何使用 .dll 文件和 .a 文件仍然有些模糊。我知道如果我删除 .a 文件,我仍然可以构建客户端,所以我什至不确定它的目的是什么。我所知道的是,它是多个目标文件的某种归档格式
我可以 ctypes.windll.loadlibrary(...) 一个在 windows/system32 中找到的普通 windows DLL 没有问题。
最后一点:我使用的是 64 位 python 3.3。我使用的是minGW tat的版本,自带推荐的安装程序(mingw-get-inst-20120426.exe)。我不确定它是否是 32 位的,或者这是否重要。
谢谢!