加载DLL时Python解释器退出 [英] Python interpreter exits when loading DLL

查看:69
本文介绍了加载DLL时Python解释器退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试用 ctypes 包装一个dll.我已经编写了以下测试代码,并使用Code :: Blocks和Cygwin将其编译为dll.

I want to test wrapping a dll with ctypes. I've written the following test code and compiled it using Code::Blocks and Cygwin to a dll.

#define DLL_EXPORT extern "C" __declspec(dllexport)

DLL_EXPORT int sum(int a, int b) {
    return a + b;
}

注意:这是完整的代码.也许缺少什么?

现在,我将 TestDll.dll 复制到我的桌面并启动Python解释器.但是当我要加载它时,解释器就退出了!

Now, I copy the TestDll.dll to my Desktop and start the Python interpreter. But when I want to load it, the interpreter just exits !

C:\Users\niklas\Desktop>python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win 32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> dll = ctypes.WinDLL('TestDll')

C:\Users\niklas\Desktop>

但是,加载任何其他库都可以,如果找不到该库,则会给出错误消息.
您能告诉我我在做什么错吗?

However, loading any other library works, or giving an error if the library could not be found.
Can you tell me what I'm doing wrong here ?

使用Cygwin g ++ 3.4.4

Using Cygwin g++ 3.4.4

推荐答案

检查 objdump -p TestDll.dll |grep dll 来查看是否已链接"cygwin1.dll"和 nm TestDll.dll |grep Dll 来查看您是否有 DllMain .以下命令应正确构建DLL:

Check objdump -p TestDll.dll | grep dll to see if you've linked in "cygwin1.dll" and nm TestDll.dll | grep Dll to see if you have a DllMain. The following command should build the DLL correctly:

g++ testdll.c -mno-cygwin -shared -o TestDll.dll

此外,您需要对cdecl调用约定使用CDLL,而不是WinDLL:

Also, you need to use CDLL for the cdecl calling convention, not WinDLL:

>>> import ctypes
>>> dll = ctypes.CDLL('TestDll')
>>> dll.sum(4, 5)
9

我使用Cygwin信息库中的 i686-w64-mingw32-g ++.exe (4.5.3)进行了编译,但是在没有问题的情况下,我曾经使用默认的Cygwin gcc,原因是-mno-cygwin选项.

I compiled with i686-w64-mingw32-g++.exe (4.5.3) from the Cygwin repository, but I used to use the default Cygwin gcc without a problem, given the -mno-cygwin option.

这篇关于加载DLL时Python解释器退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆