使用Python Ctypes加载dll [英] Loading dll using Python Ctypes

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

问题描述

我已经看过此处给出的示例 ctypes-Beginner ,并以相同的步骤执行了相同的步骤C代码.我已经使用此处给出的C代码构建了一个.dll和.lib: http://wolfprojects.altervista.org/articles/dll-in-c-for-python/

I've looked at the example given here ctypes - Beginner and followed the same steps with a different bit of C code. I've built a .dll and a .lib using C code given here: http://wolfprojects.altervista.org/articles/dll-in-c-for-python/

  //test.c
__declspec(dllexport) int sum(int a, int b) {
    return a + b;
}

在我的wrapper.py中,我有这个:

In my wrapper.py I have this:

import ctypes

testlib = ctypes.CDLL("C:\\Users\\xyz\\Documents\\Python\\test.dll")

运行脚本时出现此错误:

When I run the script I get this error:

self._handle = _dlopen(self._name,mode)

self._handle = _dlopen(self._name, mode)

OSError:[WinError 193]%1不是有效的Win32应用程序

OSError: [WinError 193] %1 is not a valid Win32 application

如果我使用

testlib = ctypes.LibraryLoader("C:\\Users\\xyz\\Documents\\Python\\test.dll")

那么我在运行脚本时没有任何错误.但是,如果我尝试这样做:

then I don't get any error on running the script. But If I try to do this:

testlib.sum(3,4)

我得到了错误:

dll = self._dlltype(name)

dll = self._dlltype(name)

TypeError:"str"对象不可调用

TypeError: 'str' object is not callable

dll和.py位于同一文件夹中.谁能帮助我了解这里的情况.我花了数小时试图弄清楚这一点,但是碰壁了.谢谢.

The dll and the .py are in the same folder. Can anyone help me understand what's going on here. I've spent hours trying to figure this out, but have hit a wall. Thanks.

推荐答案

确保您的编译器和Python版本均为32位或64位.您无法混合,这是导致 OSError:[WinError 193]%1不是有效的Win32应用程序的原因.

Make sure your compiler and version of Python are both 32-bit or both 64-bit. You can't mix, which is the cause of OSError: [WinError 193] %1 is not a valid Win32 application.

接下来,确保编译为C程序而不是C ++.这就是答案中提到名字修改的原因.

Next, make sure to compile as a C program and not C++. That's the cause of the name mangling mention in your answer.

示例(注释编译器用于 x86 ,而不是 x64 :

Example (note compiler is for x86 not x64:

C:\>cl /LD /W4 test.c
Microsoft (R) C/C++ Optimizing Compiler Version 17.00.61030 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

test.c
Microsoft (R) Incremental Linker Version 11.00.61030.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.dll
/dll
/implib:test.lib
test.obj
   Creating library test.lib and object test.exp

现在使用 32位 Python:

Now use a 32-bit Python:

C:\>py -2
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> lib = CDLL('test')
>>> lib.sum(2,3)
5

如果您使用C ++进行编译,则仍然可以通过将函数导出为C来调用函数,这可以防止C ++名称混乱:

If you compile as C++, you can still call functions by exporting them as C, which prevents the C++ name mangling:

//test.cpp
extern "C" __declspec(dllexport) int sum(int a, int b) {
    return a + b;
}

这篇关于使用Python Ctypes加载dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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