我正在使用
Python的ctypes库与Windows DLL进行通信.当我从IDLE,Ipython运行我的代码,或者输入交互式python解释器时,它工作正常.当我从Windows命令提示符运行相同的代码时,它崩溃了.为什么单向崩溃,一种方式成功?
这是我正在运行的代码的简化版本:
import ctypes,os,sys
print "Current directory:",os.getcwd()
print "sys.path:"
for i in sys.path:
print i
PCO_api = ctypes.oledll.LoadLibrary("SC2_Cam")
camera_handle = ctypes.c_ulong()
print "Opening camera..."
PCO_api.PCO_OpenCamera(ctypes.byref(camera_handle),0)
print " Camera handle:",camera_handle.value
wSensor = ctypes.c_uint16(0)
print "Setting sensor format..."
PCO_api.PCO_SetSensorFormat(camera_handle,wSensor)
PCO_api.PCO_GetSensorFormat(camera_handle,ctypes.byref(wSensor))
mode_names = {0: "standard",1:"extended"}
print " Sensor format is",mode_names[wSensor.value]
当我从IDLE或Ipython运行此代码时,我得到以下结果:
Current directory: C:\Users\Admin\Desktop\code
sys.path:
C:\Users\Admin\Desktop\code
C:\Python27\Lib\idlelib
C:\Windows\system32\python27.zip
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\plat-win
C:\Python27\lib\lib-tk
C:\Python27
C:\Python27\lib\site-packages
Opening camera...
Camera handle: 39354336
Setting sensor format...
Sensor format is standard
>>>
当我从Windows命令提示符运行此代码时,我得到以下结果:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Admin>cd Desktop\code
C:\Users\Admin\Desktop\code>C:\Python27\python.exe test.py
Current directory: C:\Users\Admin\Desktop\code
sys.path:
C:\Users\Admin\Desktop\code
C:\Windows\system32\python27.zip
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\plat-win
C:\Python27\lib\lib-tk
C:\Python27
C:\Python27\lib\site-packages
Opening camera...
Camera handle: 43742176
Setting sensor format...
Traceback (most recent call last):
File "test.py",line 18,in
PCO_api.PCO_GetSensorFormat(camera_handle,ctypes.byref(wSensor))
File "_ctypes/callproc.c",line 936,in GetResult
WindowsError: [Error -1609945086] Windows Error 0xA00A3002
C:\Users\Admin\Desktop\code>
请注意,一些DLL调用工作,直到我设置传感器格式,我们离开轨道.
通过检查我正在调用的DLL附带的文档,我看到Windows错误解码为“缓冲区的wSize是小的”. (原文如此).我不确定这是否相关.万一重要,here’s the API documentation.
当我看到“在IDLE中工作,在提示时失败”时,我假设必须设置一些不同的环境变量.我该怎么检查?
编辑:
我将sys.path和os.getcwd()添加到测试代码中.
编辑:
不确定这是否重要,但我加载的DLL(SC2_Cam.dll)位于当前工作目录中.此目录中还有另一个DLL(sc2_cl_me4.dll),我相信它是由SC2_Cam.dll加载的.如果我从此目录中删除sc2_cl_me4.dll,则无法调用SC2_Cam.dll,包括PCO_OpenCamera.
编辑:
如果我将它输入’vanilla’交互式python解释器,上面的代码也可以工作.我不需要IDLE或ipython来使它工作.只调用’python.exe test.py’失败.