Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Loading dll using Python Ctypes

I've looked at the example given here ctypes - Beginner and followed the same steps with a different bit of C code. I've built a .dll and a .lib using C code given here: http://wolfprojects.altervista.org/articles/dll-in-c-for-python/

 //test.c
__declspec(dllexport) int sum(int a, int b) {
 return a + b;
}

In my wrapper.py I have this:

import ctypes
testlib = ctypes.CDLL("C:\\Users\\xyz\\Documents\\Python\\test.dll")

When I run the script I get this error:

self._handle = _dlopen(self._name, mode)

OSError: [WinError 193] %1 is not a valid Win32 application

If I use

testlib = ctypes.LibraryLoader("C:\\Users\\xyz\\Documents\\Python\\test.dll")

then I don't get any error on running the script. But If I try to do this:

testlib.sum(3,4)

I get the error:

dll = self._dlltype(name)

TypeError: 'str' object is not callable

The dll and the .py are in the same folder. Can anyone help me understand what's going on here. I've spent hours trying to figure this out, but have hit a wall. Thanks.

Answer*

Draft saved
Draft discarded
Cancel
3
  • Thanks for the answers. I'm on 64-bit Windows and have installed 64-bit version of Anaconda so my Python is 64-bit. I didn't compile the dll myself as I didn't have the tools for it. So perhaps the problem was because of the compiling. Commented Apr 10, 2017 at 5:50
  • 1
    You might could mention that it is possible to have a 32 bit and 64 bit version of python installed... since I had the 64 bit version installed and couldn't recompile the dll (as far as I know), I just installed a 32 bit of python 3.8 and it worked like a charm. Thanks! Commented May 19, 2020 at 23:33
  • @ShanerM13 You did just mention it 🙂. If you install multiple Python versions and don't add them to the path, you can use the Python Launcher option and easily switch between any version or even call out the version for a script to run in a shebang(#!) comment. Commented May 21, 2020 at 22:47

lang-py

AltStyle によって変換されたページ (->オリジナル) /