1

I am trying to use a C++ dll in python. I am running python 2.7 on Windows server 2012; both are 64bit.

To create a dll, I followed the directions on "Walkthrough: Creating and Using a Dynamic Link Library (C++)" on http://msdn.microsoft.com/en-us/library/vstudio/ms235636.aspx. I used their example code. I am new to dlls and python so I thought I would start with examples.

My python code:

from ctypes import *  
hw = CDLL("Y:\dll_check\MathFuncsDll.dll")  
print "HelloWorld"  

I get the following error:

 Y:\dll_check> python .\MathFuncsMain/py  
    Traceback (most recent call last):  
        File ".\MathFunsMain.py"  
            libimgr = CDLL("Y:\dll_check\MathFuncsDll.dll")  
        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  

What is causing my error?

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • 2
    Perhaps you are loading a 64bit DLL into a 32bit process? – paulm May 06 '13 at 17:47
  • What is the output when you run this: `python -c 'import struct;print( 8 * struct.calcsize("P"))'` from the commandline? – Jason Sperske May 06 '13 at 17:49
  • How would I check that? Or correct it? – user2043697 May 06 '13 at 17:49
  • import struct;print( 8 * struct.calcsize("P")) outputs 64 – user2043697 May 06 '13 at 17:50
  • Here is where that line came from: http://stackoverflow.com/questions/1405913/how-do-i-determine-if-my-python-shell-is-executing-in-32bit-or-64bit-mode – Jason Sperske May 06 '13 at 17:51
  • python -c 'import struct;print( 8 * struct.calcsize("P"))' outputs Traceback (most resent call last): file "< string>", line 1 in NameError: name 'P' is not defined – user2043697 May 06 '13 at 17:52
  • I put the struct.calcsize code in a python file and got 64 back. – user2043697 May 06 '13 at 17:55
  • Duplicate of http://stackoverflow.com/questions/2253555/calling-c-dlls-from-python It isn't possible to call C++ dll using ctypes. You have top create wrapper around it using SWIG, SIP or few other wrapper generators – Zuljin May 06 '13 at 18:05
  • If I was given the dll and lib files (I don't have access to source code), is there a wrapper I can use? SWIG and Boost sound like I need the source code. – user2043697 May 06 '13 at 18:41
  • Just a side note: shouldn't you be writing Windows paths as `r"Y:\dll_check\MathFuncsDll.dll"` in order to prevent backslash interpretation? – n. m. could be an AI May 06 '13 at 19:33
  • r"Y:\dll_check\MathFuncsDll.dll" do not change anything. – user2043697 May 07 '13 at 13:24
  • @user2043697 It certainly does change something. It doesn't fix the mismatch between 32 and 64 bits. But then nobody claimed it did. Don't just try things at random without attempting to understand them. – David Heffernan Sep 20 '13 at 08:31

1 Answers1

-1

use 32 bit python version and compile to resolve the issue. Reason(probably) : even though your machine is 64-bit; the dll you have created is a 32 bit one. bcz through visual studio you can select and create only "Win32 application".