Skip to content
Advertisement

OSError: [WinError 193] %1 is not a valid Win32 application while reading custom DLL in python with CTypes

I am trying to write code that wraps a C library in python. I am planning on using CTypes to do it and I used visual studio to compile my DLL. I started with a simple function and I added the following in a header within Visual Studio that was then built to a DLL

JavaScript

My python wrapper is the following

JavaScript

When I execute the python code I get the following error

JavaScript

What causes this error and how do I fix it? I am using a 64 bit machine with windows 10 and my python build is 64 bit. I don’t know much C, and the main goal is to get it working so that I can code everything in python.

Advertisement

Answer

This is a typical CPU architecture (032bit (your .dll) vs. 064bit (Python process that tries to load it)) mismatch. Check [SO]: Python Ctypes – loading dll throws OSError: [WinError 193] %1 is not a valid Win32 application (@CristiFati’s answer) for more details.

Build the 064bit (pc064) version of your your .dll.
You can use the command line tools from the aforementioned URL, or you can set the VStudio IDE to do it, as explained in [MS.Docs]: How to: Configure Visual Studio C++ projects to Target 64-Bit, x64 Platforms:

  1. Open the C++ project that you want to configure.
  2. Open the property pages for that project. For more information, see Set C++ compiler and build properties in Visual Studio.
  3. Choose the Configuration Manager button to open the Configuration Manager dialog box.
  4. In the Active Solution Platform drop-down list, select the <New…> option to open the New Solution Platform dialog box.
  5. In the Type or select the new platform drop-down list, select a 64-bit target platform.
  6. Choose the OK button. The platform that you selected in the preceding step appears under Active Solution Platform in the Configuration Manager dialog box.
  7. Choose the Close button in the Configuration Manager dialog box, and then choose the OK button in the <Projectname> Property Pages dialog box.
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement