title: Program works in IDLE, but fails at the command line
body: <p>I'm using Python's ctypes library to talk to a Windows DLL. When I run my code from IDLE, Ipython, or typed into the interactive python interpreter, it works fine. When I run the same code from the Windows command prompt, it crashes. <strong>Why does one way crash, and one way succeed?</strong></p>
  
  <p>Here's a simplified version of the code I'm running:</p>
  
  <pre><code>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]
  </code></pre>
  
  <p>When I run this code from IDLE or Ipython, I get the following result:</p>
  
  <pre><code>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
  &gt;&gt;&gt; 
  </code></pre>
  
  <p>When I run this code from the Windows command prompt, I get the following results:</p>
  
  <pre><code>Microsoft Windows [Version 6.1.7601]
  Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
  
  C:\Users\Admin&gt;cd Desktop\code
  
  C:\Users\Admin\Desktop\code&gt;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 &lt;module&gt;
      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&gt;
  </code></pre>
  
  <p>Notice that a few of the DLL calls work, it isn't until I get to setting the sensor format that we go off the rails.</p>
  
  <p>By inspecting the documentation that came with the DLL I'm calling, I see the Windows Error decodes to "The wSize of a buffer is to small." (sic). I'm not sure that's relevant. Just in case it matters, <a href="http://www.pco.de/fileadmin/user_upload/db/download/MA_DCSDKWINE_114.pdf" rel="nofollow">here's the API documentation</a>.</p>
  
  <p>When I see "works in IDLE, fails at prompt", I assume there must be some environment variable set differently. What should I check?</p>
  
  <p>EDIT:</p>
  
  <p>I added sys.path and os.getcwd() to the test code.</p>
  
  <p>EDIT:</p>
  
  <p>Not sure if this matters, but the DLL I load (SC2_Cam.dll) is in the current working directory. Also in this directory is another DLL (sc2_cl_me4.dll), which I believe is loaded by SC2_Cam.dll. If I remove sc2_cl_me4.dll from this directory, none of the calls to SC2_Cam.dll work, including PCO_OpenCamera.</p>
  
  <p>EDIT:</p>
  
  <p>The code above also works if I type it into the 'vanilla' interactive python interpreter. I don't need IDLE or ipython to make it work. Only calling 'python.exe test.py' fails.</p>