Calling customized C++ dll from Python - c++

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. :)

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.

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)

Build your C code to X64 version
I tested it and actually works well

Related

Compiled dll not working on intel Atom 32bit

I am compiling a dll in visual studio 2017 c++.
SDK: 10.0.17134.0
this project uses a template, that automatically creates 2 dll, one for 32 bit and one for 64 bit. I do have two machines that run the same software but have different hardware and OS.
First machine has a intel i7 and runs windows embedded standard 64 bit
the second machine has a intel atom and runs windows embedded standard 32 bit
On the 64 bit machine, both dll work. (32 bit and 64 bit), on the atom the 32 bit does not work tough. I do not have any error messages, the only thing i get from the software is that it is not compatible without any additional clues. The software is the same on both systems so I assume that the problem is related to the OS or the processor.
the software I am developing for is a vision system by omron so it is nothing that is available online or that can be shared here.
What could be the cause for this? If you need additional information just ask.
Generally, in order for an executable file (either an .EXE program or a .DLL support module) built using the MSVC C/C++ compiler in Visual Studio 2015 or later, to work on a target PC, you need to have the latest VC++ Redistributable run-time libraries installed on that PC.
See also this discussion on Stack Overflow.

Visual Studio C++ Application wont run on 32bit computer

I have a program that I made in Visual Studio 2010. I built the program in release mode and Win32 solution platform. I then made an executable by following this guide step by step. I then copied the setup.exe that was created onto a new 32-bit computer. I then get this error message when I try to run the setup on the new computer:
Why is the setup not working? I built the program in Win32, so it should work on a 32-bit computer? Am i missing something? Any help would be appreciated.
There are 3 major reasons for this to happen. The Windows executables contain 3 fields that must be matched by the OS: Minimal OS version number, correct CPU type and correct CPU bit-ness. Now you're probably not running into a Windows version issue (I think the error message is different), you're quite unlikely to have the wron CPU type (ARM builds are pretty hard to make by accident) so that leaves as the most likely scenario that you actually made a 64 bits build.
"Win32" is a rather deceiving term here, it doesn't always exclude 64 bits builds. E.g. the macro WIN32 is defined for 64 bits builds as well.
#Mailerdaimon and #MSalters you were correct. Even though I was building the program in win32, the target machine was x64. After changing it to x86 the program ran. Thanks for everyones help!

Which windows subsystem is supported on a recovery disk

When running a standard Windows 7 Installation Disk in recovery mode, if you open up the command line and run a custom-built application you will receive the error 'subsystem not supported'. I have tried linking with /SUBSYSTEM:CONSOLE, WINDOWS and NATIVE, none of these work.
I had a little difficulty with a partition table (and may have found a bug, or at least 'stupid' behaviour from the partition manager included in windows) and so wrote a utility to fix it. My program uses 'Windows.h' to import CreateFile, however if need be I can use only standard C++ (Or even standard C) with no windows specific headers.
What do I need to do to get an application running?
The Windows Recovery Environment is a superset of the Windows Preinstallation Environment.
Windows PE is a stripped down version of windows, lacking many subsystems including WoW (Windows on Windows).
This means that 32bit executables (or anything with a 32bit component) WILL NOT RUN on a 64-bit Windows PE disk. (Note that WinPE 32 cannot install/repair 64 bit systems and vice-versa).
The solution to my problem was to compile to 64 bit code -- a descriptive error message would have been nice Microsoft :|
Found after much searching:
http://technet.microsoft.com/en-us/library/cc766093(v=ws.10).aspx
Are you using the C++ CRT in any way? I don't think that's supported. I'd even doubt that CreateFile is appropriate; and look into NtCreateFile instead.

Weird compiling problem - DLL not compiled the same on some machines

I'm not asking for the SO to tell me what the problem is, I'm just asking what sanity checks should I run in a case like this.
Using Visual Studio 2005, plain c++ project. Actual code is:
int Sum(int a, int b)
{
return a+b;
}
No pre-compiled headers. Exported with a DEF file:
LIBRARY testdll
EXPORTS
Sum
Compiled on a test machine (VS2005 again), the DLL works on other machines (64 bit and 32 bit tested, always compiled with a Win32 target platform). Compiled on my machine (64 bit, same project, same properties), the DLL works only on my machine, on others it starts the Just-In-Time Debugger (or crashes horribly if JIT isn't installed):
Unhandled exception at 0x00000000 in Caller.exe: 0xC0000005: Access violation reading location 0x00000000.
At first I was calling it with j on my test machines, and that would fail too, giving me (with cder) a "file not found" error.
Other symptom: File size is different, my machine gives the DLL an extra 512 bytes.
My system configuration:
Windows Vista - 64 bit
VS2005 Version 8.050727.867 (vsvista.050727-8600)
.NET Framework Version 2.0.50727 SP2
Tested environments:
Windows XP - 32 bit (virtual machine)
VS2005 Version 8.0.50727.42 (RTM.050727-4200)
.NET Framework Version 2.0.50727 SP2
Windows XP - 64 bit
VS2005 Version 8.0.50727.42 (RTM.050727-4200)
.NET Framework Version 2.0.50727 SP2
I suppose that "Win32 target platform" is wrong configured on your 64-bit machine. I recommend you to start "Visual Studio 2005 Command Prompt" and use
dumpbin.exe /headers YouDll.dll
to examine the "wrong" DLL which will be produced on the 64-bit machine. You can compare it with the "good" DLL. I suppose that you will immediately see the differences. WinDiff.exe can help you additionally.
One small general advice: consider to use EXTERN_C and WINAPI (or __stdcall) for all functions which you export from the DLL.
Open both DLLs in Dependency Walker and see what's different. Preferably do this on a machine where one DLL doesn't run right.

Credential manager for Vista/Windows 7

I have Credential manager implemented in VC++ which captures credentials during login process. It works well in XP/Vista/Windows 7 32 bit env. But is not working in 64 bit.
Any idea ?
Thanks in advance for any help
If you want your DLL to be loaded by a 64-bit process, your DLL has to be compiled for 64 bits.
If you want your DLL to be loaded by a 32-bit process, your DLL has to be compiled for 32 bits. This is true on both 64-bit Windows systems and 32-bit Windows systems.
John gave you a useful link, even though John's wording is wrong. An application (exe) which is built for 32 bits will run in 64 bit Windows, but it can only load 32-bit DLLs.
Did you build for a 64-bit platform in Visual Studio?
A Visual C++ application which is build for a 32-bit environment won't work directly in a 64 bit environment. And although applications will work using the WOW64 compatibility layer, DLLs must be 64-bit if they are to be loaded by a 64-bit operating system.
Since that is indeed the case here, you need to build your project for a 64-bit platform (Visual Studio 2005 and later have a 64-bit compiler).
See the link http://msdn.microsoft.com/en-us/library/ms185328.aspx for more details.