26

When I use CDLL to call 32bit dll in 32bit python, it works well. But unfortunatelly in my 64bit win7 os only installs 64bit python, when calling it turns: it is not a effective win32 app!

Can I use 32bit dll or exe in 64bit python? Or I have to install 32bit python instead?

User97693321
  • 3,336
  • 7
  • 45
  • 69
hjhnju
  • 265
  • 1
  • 3
  • 6

1 Answers1

38

64-bit EXEs cannot load 32-bit DLLs. (And vice versa: 32-bit EXEs cannot load 64-bit DLLs.) After all, they can't agree on the size of a pointer -- what would happen if the EXE allocated memory above the 4GB boundary and wanted to pass that pointer to the 32-bit DLL?

You'll have to either:

  1. Make a 64-bit version of your DLL;
  2. Use a 32-bit version of Python; or
  3. Host the DLL in a separate (32-bit) EXE, and use some form of inter-process communication to talk to it from your 64-bit Python process. Here's an example of using IPC to let a 64-bit Python process talk to a 32-bit DLL.
Joe White
  • 94,807
  • 60
  • 220
  • 330
  • 1
    so, 64-bit version of Python cannot choose to run in 32-bit mode? just like 64bit win7 can execute 32bit app – hjhnju Jul 25 '12 at 08:23
  • 8
    64-bit Windows can run 32-bit processes, but each individual process must be either 64-bit or 32-bit; you can't mix the two within a single address space. If you want to run an app as 32-bit, it has to be compiled as a 32-bit app. (Those pointer sizes again. If an app is compiled to expect pointers to be 64 bits in size, it won't work with pointers that are 32 bits, and vice versa.) If the app is compiled as 32-bit, it runs as a 32-bit app; if it's compiled as 64-bit, it runs as a 64-bit app (on a 64-bit OS, and doesn't run at all on a 32-bit OS). – Joe White Jul 25 '12 at 12:09
  • 1
    This is wrong, technically 64-bit DLLs/EXEs can load 32-bit DLLs and vice versa. There is a library out there on GitHub to do exactly this on Windows. Some assembly level stuff – demberto Feb 28 '22 at 10:01
  • @demberto That's an interesting claim. Do you have a link with more information? – Joe White Apr 15 '22 at 17:13
  • 1
    @JoeWhite https://github.com/ez8-co/yapi – demberto Apr 16 '22 at 18:36