Reputation: 7408
My C++ IDE is Visual Studio 2012 Express Version, and my Python IDE is Aptana3 (64-bit). My computer is Windows 7 64-bits.
I've write a .dll with C++ (Win32 console application), which basically follows the instruction at MSDN. It works well when I call it with a C++ application.
Then I try to call it from Python by following codes:
import ctypes
d = ctypes.WinDLL("C:\\DynamicLibrary\\Debug\\MathFuncsDll.dll")
However, I've got following error:
File "`<pyshell#8>`", line 1, in <module>
d = ctypes.WinDLL("C:\\DynamicLibrary\\Debug\\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
I've googled about this error message, and some posts say it because the compatibility between 32- and 64-bits. But I doubt it, since my IDE's and system are all 64-bit.
May I know what am I wrong?
Many thanks in advance. :)
Upvotes: 2
Views: 2713
Reputation: 63707
I've googled about this error message, and some posts say it because the compatibility between 32- and 64-bits. But I doubt it, since my IDE's and system are all 64-bit.
Yes, your research is correct.
My C++ IDE is Visual Studio 2012 Express Version, My computer is Windows 7 64-bits.
That doesn't guarantee that you would build a 64 bit binary. Infact, VS 2012 IDE is a 32 bit application. Its the compiler and the CRT which is responsible to generate a 64 bit binary. And moreover the default settings for Visual Studio is to generate a 32 bit binary
You can easily google and determine how to build a 64 bit binary using Visual Studio. Alternatively, refer the link How to: Configure Visual C++ Projects to Target 64-Bit Platforms
and my Python IDE is Aptana3 (64-bit). My computer is Windows 7 64-bits.
That still doesn't say anything about your Bitness of your Python Installation .
When in doubt, check for the bitness of your dll
and your python.exe
. You can easily determine the bitness using dumpbin
C:\Python27>dumpbin /headers python.exe|grep "machine"
14C machine (x86)
Upvotes: 0
Reputation: 612794
The most common explanation for that error is that the system is attempting to load a 32 bit DLL into a 64 bit process, or vice versa. The fact that your system is 64 bit just makes that diagnosis more likely. Perhaps your Python is 64 bit, but the C++ project outputs a 32 bit DLL. Or vice versa.
In the question you state that your Python installation is 64 bits. In which case you need to look at your C++ project. What platform are you targetting? Win32 or x64? My money is on the answer to that question being that you target Win32.
That's the most likely explanation. Beyond that the next most likely cause is the exact same problem, but for one of the dependencies. The Python process and the DLL match, but when resolving the dependencies of the DLL the loader finds a DLL of the wrong bitness.
Upvotes: 3