Python Ctypes加载库 [英] Python Ctypes Load Library

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

问题描述

我正在使用Windows 7 64位机器。我安装了Visual studio 2010,并开发了一个简单的win32 dll来添加2个数字。dll是创建的,我使用测试应用程序来测试dll,它的工作正常。



现在我编写python脚本(如下所示)来使用这个库。但是我收到以下错误消息。

 追溯(最近的最后一次呼叫):
文件C:\ Users &\\sbritto\Documents\Visual Studio 2008\Projects\MathFuncsDll\Debug\MathFuncs.py,第5行,< module>
lib = ctypes.WinDLL('MathFuncsDll.dll',use_last_error = True)
文件C:\Python27\lib\ctypes\__init __。py,第365行,__init__
self._handle = _dlopen(self._name,mode)
WindowsError:[错误193]%1不是有效的Win32应用程序

Python脚本

  import ctypes 
从ctypes import *

#lib = cdll.LoadLibrary(MathFuncsDll.dll)
lib = ctypes.WinDLL('MathFuncsDll.dll',use_last_error = True)
打印lib

请尽快让我知道。



提前感谢

解决方案

如果您尝试使用Python解释器打开64位DLL,您将收到此错误为32位机器编译,反之亦然。所以,如果这是一个64位的DLL,你需要确保你运行一个64位的Python。


I am using windows 7 64 bit machine. I installed Visual studio 2010 and developed a simple win32 dll to add 2 numbers.. The dll is created and i used a test application to test the dll and it works fine..

Now i write python script(shown below) to use this library. But i get the following error message.

Traceback (most recent call last):
  File "C:\Users\sbritto\Documents\Visual Studio 2008\Projects\MathFuncsDll\Debug\MathFuncs.py", line 5, in <module>
    lib = ctypes.WinDLL('MathFuncsDll.dll',use_last_error=True)
  File "C:\Python27\lib\ctypes\__init__.py", line 365, in __init__
    self._handle = _dlopen(self._name, mode)
WindowsError: [Error 193] %1 is not a valid Win32 application

Python Script

import ctypes
from ctypes import *

#lib = cdll.LoadLibrary("MathFuncsDll.dll")
lib = ctypes.WinDLL('MathFuncsDll.dll',use_last_error=True)
print lib

Kindly let me know ASAP.

Thanks in advance

解决方案

You'll get this error if you try to open a 64-bit DLL using a Python interpreter compiled for a 32-bit machine, or vice versa. So, if this is a 64-bit DLL you need to make sure you are running a 64-bit Python.

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

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