您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – 程序在IDLE中工作,但在命令行失败

5b51 2022/1/14 8:23:14 python 字数 5651 阅读 582 来源 www.jb51.cc/python

我正在使用 Python的ctypes库与Windows DLL进行通信.当我从IDLE,Ipython运行我的代码,或者输入交互式python解释器时,它工作正常.当我从Windows命令提示符运行相同的代码时,它崩溃了.为什么单向崩溃,一种方式成功? 这是我正在运行的代码的简化版本: import ctypes, os, sys print "Current directory:", os.

概述

这是我正在运行的代码的简化版本:

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 <module>
    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’失败.

鉴于此,让我们看看以下行:

PCO_api.PCO_OpenCamera(ctypes.byref(camera_handle),0)

根据api文档,在上面的调用中,PCO_OpenCamera函数不只是在camera_handle中返回一个值;它还使用camera_handle作为输入值.但是,您将camera_value保留为未初始化状态.我知道你应该在通话前将它设置为零.另一个问题是PCO_OpenCamera返回一个应该检查的值.如果有问题,但程序继续,就好像没有,它将继续使用camera_handle的随机值.因此,程序中的一个错误似乎是前一行(保存打印)应该是

camera_handle = ctypes.c_ulong(0)

一个是不检查PCO_OpenCamera的返回值. (我不知道其余的是否合适,我没有仔细检查过.)

另外,c_ulong是Windows HANDLE类型的正确类型吗?我不知道,也没关系.即使c_ulong大于HANDLE,它仍然可能正常.但可能还不够;你必须确定你知道自己在做什么.


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶