The program works in IDLE, but does not work on the command line

I am using the ctypes Python library to communicate with the Windows DLL. When I run my code from IDLE, Ipython, or type into the python interactive interpreter, it works fine. When I run the same code from the windows command prompt, it crashes. Why is one way to fail, and one way to success?

Here is a simplified version of the code I'm running:

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] 

When I run this code from IDLE or Ipython, I get the following result:

 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 >>> 

When I run this code from the Windows command prompt, I get the following results:

 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> 

Note that some of the DLL calls work, but only until I set the sensor format that we remove from the rails.

By checking the documentation associated with the DLL that I am invoking, I see that Windows Error decrypts "WSize buffers to small." (So โ€‹โ€‹in the original). I am not sure about that. Just in case, this is important, here is the API documentation .

When I see that โ€œit works in IDLE, the call failedโ€, I assume that some kind of environment variable should be set differently. What should I check?

EDIT:

I added sys.path and os.getcwd () to the test code.

EDIT:

Not sure if this matters, but the loadable DLL (SC2_Cam.dll) is in the current working directory. Also in this directory is another DLL (sc2_cl_me4.dll), which, it seems to me, is loaded using SC2_Cam.dll. If I remove sc2_cl_me4.dll from this directory, none of the calls to SC2_Cam.dll will work, including PCO_OpenCamera.

EDIT:

The above code also works if I inject it into the pwon 'vanilla' interactive interpreter. I don't need IDLE or ipython for it to work. Only the call to "python.exe test.py" is performed.

+6
source share
3 answers

When you interact with program C, you get all the difficulties of C. Any mistake you make can cause buffer overflows, stack overflows, segmentation violations, etc. If a program writes a location to random memory due to an error, its behavior will not be the same in all circumstances. On your computer, it works interactively, but it fails to start from a command prompt window. But in another operating system, or on another machine, or even on the same machine on another day, it can behave differently. His behavior is not deterministic.

Given this, consider the following line:

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

According to the API documentation in the above call, the PCO_OpenCamera function returns not only the value in camera_handle ; it also uses camera_handle as input value. However, you leave camera_value uninitialized. I understand that before calling you must set it to zero. Another problem is that PCO_OpenCamera returns a value that needs to be checked. If there is a problem, but the program continues, as if it were not, it will continue to use a random value for camera_handle . Thus, one error in the program, apparently, is that the previous line (print preservation) should be

 camera_handle = ctypes.c_ulong(0) 

and another is that the return value of PCO_OpenCamera not checked. (I do not know if everything is in order, I did not carefully study this.)

Also, c_ulong correct type for the Windows type HANDLE ? I donโ€™t know, and everything can be in order. Even if c_ulong larger than HANDLE , it is probably OK anyway. But probably this is not enough; You must be sure that you know what you are doing.

+4
source

Do you have several versions of python installed on your system? Perhaps you are using a different version when you are working online and when you run it from a file.

+5
source

The error you get leads me to think that the 16-bit integer that you use to store the wSensor variable is too small. I looked at their API, which simply indicates it as a WORD type, which by historical standards of Microsoft standards should be 16 bits, but since there is a lot of ambiguity around how big the words are, try increasing the value to 32 or 64 bits.

As for why this can lead to behavior in different environments, are you using a 64-bit OS? Do you have a different version of python installed?

+1
source

Source: https://habr.com/ru/post/915338/


All Articles