2

I am using ctypes to load a 64-bit Windows DLL into a 64-bit Python 3.6.2 environment, but Python complains that the DLL is not a 32-bit DLL. This is all 64-bit, so I don't know why it expects a 32-bit DLL. This same error message appears in Visual Studio 2017 and in PyCharm 2017.1.5.

Here is the problem code:

ThisDLL = ctypes.WinDLL(r"C:\Temp2\Test_Project\Std_Math_Formulas.dll") 

Here are the error messages:

File "C:\Project Backups\Python Projects - PyCharm\Test_DLL\Main_Entry.py", line 73, in CTypes_Test_02 ThisDLL = ctypes.WinDLL(r"C:\Temp2\Test_Project\Std_Math_Formulas.dll") File "C:\Program Files\Python36\lib\ctypes\__init__.py", line 348, in __init__ self._handle = _dlopen(self._name, mode) OSError: [WinError 193] %1 is not a valid Win32 application 

I have researched this at length, and the answers all apply to a mixed 32-bit to 64-bit situation -- 32-bit DLL in 64-bit environment or 64-bit DLL in 32-bit environment. Here they both environment and DLL are 64-bit.

I know that Windows 32-bit DLLs use the stdcall calling convention and 64-bit uses the fastcall convention, but ctypes should be able to load a 64-bit DLL.

2
  • How do you know that your Python environment is 64-bit? Can you print the output of import platform; print(platform.architecture())? Commented Dec 28, 2017 at 0:33
  • I'm not completely sure, but I wouldn't be surprised that the Windows error 193 is not "bit-localized" - i.e., it's a generic loader error, and will spit "it is not a valid Win32 application" in reply to an invalid file, regardless of it expecting a 32 or 64 bit executable. If you open that dll in e.g. Dependency Walker does it see it as a valid dll? Commented Dec 28, 2017 at 0:36

2 Answers 2

1

You are mixing up win32 and 32-bit. 64-bit applications are also win32 applications. So your problem has nothing to do with 32 and 64-bit systems.

Sign up to request clarification or add additional context in comments.

1 Comment

Dietrich: import platform; print(platform.architecture()) and it confirms that it is a 64-bit environment. Matteo: Dependency Walker will open the file, so it is a valid DLL, but x64dbg disassembler says that the DLL is 32-bit, although it is compiled for 64-bit (NASM), and all the registers in the disassembly are 32-bit although they're written for 64-bit registers. Mike: I assume that you are right -- the error message is more generic than specific to 32-bit, so I will investigate the DLL and post when I have an answer. Thanks to all three of you for your input.
1

I solved the problem. I assembled this DLL with the NASM assembler before linking. Originally, I used -f COFF for the output format. However, when you assemble with NASM for 64-bit, you must use -f Win64 as the output format, not -f COFF. So the proper output format (with no other switches) is:

nasm -f Win64 FileName.asm -o FileName.obj

And that solved this problem.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.