Load dll with Python Ctypes


Kronost

I have looked at the ctypes example given here - Beginner and followed the same steps with different C code. I have built a .dll and .lib using the C code provided 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 the following 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 run the script without any errors. However, if I try to do this:

testlib.sum(3,4)

I get the error:

dll = self._dlltype(name)

TypeError: 'str' object is not callable

dll and .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.

Mark Tolonen

Make sure your compiler and Python version are both 32-bit or 64-bit. You can't mix and that's what causes it OSError: [WinError 193] %1 is not a valid Win32 application.

Next, make sure to compile as a C program and not C++. That's why the name mangling is mentioned in your answer.

Example (note that the compiler is for x86 not x64 :

C:\>cl /LD /W4 test.c
Microsoft (R) C/C++ Optimizing Compiler Version 17.00.61030 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

test.c
Microsoft (R) Incremental Linker Version 11.00.61030.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.dll
/dll
/implib:test.lib
test.obj
   Creating library test.lib and object test.exp

Now using 32-bit Python:

C:\>py -2
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> lib = CDLL('test')
>>> lib.sum(2,3)
5

If you compile in C++, you can still call functions by exporting them as C, which prevents C++ name clutter:

//test.cpp
extern "C" __declspec(dllexport) int sum(int a, int b) {
    return a + b;
}

Related


Load dll with Python Ctypes

Kronost I have looked at the ctypes example given here - Beginner and followed the same steps with different C code. I have built a .dll and .lib using the C code provided here : http://wolfprojects.altervista.org/articles/dll-in-c-for-python/ //test.c __dec

Load dll with Python Ctypes

Kronost I have looked at the ctypes example given here - Beginner and followed the same steps with different C code. I have built a .dll and .lib using the C code provided here : http://wolfprojects.altervista.org/articles/dll-in-c-for-python/ //test.c __dec

Load dll with Python Ctypes

Kronost I have looked at the ctypes example given here - Beginner and followed the same steps with different C code. I have built a .dll and .lib using the C code provided here : http://wolfprojects.altervista.org/articles/dll-in-c-for-python/ //test.c __dec

Load dll with Python Ctypes

Kronost I have looked at the ctypes example given here - Beginner and followed the same steps with different C code. I have built a .dll and .lib using the C code provided here : http://wolfprojects.altervista.org/articles/dll-in-c-for-python/ //test.c __dec

Python ctypes dll load error 487

Dan I'm using Python 2.7 and trying to load the dll using ctypes: lib = ctypes.cdll.LoadLibrary("mylib.dll") It sometimes throws the following error and sometimes it works fine. Also using Python 3 always throws this error: libcrypto = ctypes.cdll.LoadLibrary(

Python ctypes load DLL using winmode

Sean Zhang I have installed a C++ API application that puts several DLLs ( A.DLLand B.DLL) in my program folder. A.DLLhave dependenciesB.DLL I can successfully load them ctypes.WinDLLfrom the installation folder using IF , e.g.C:\Programs Files\XXX-API\A.DLL I

Python ctypes load DLL using winmode

Sean Zhang I have installed a C++ API application that puts several DLLs ( A.DLLand B.DLL) in my program folder. A.DLLhave dependenciesB.DLL I can successfully load them ctypes.WinDLLfrom the installation folder using IF , e.g.C:\Programs Files\XXX-API\A.DLL I

Python ctypes load DLL using winmode

Sean Zhang I have installed a C++ API application that puts several DLLs ( A.DLLand B.DLL) in my program folder. A.DLLhave dependenciesB.DLL I can successfully load them ctypes.WinDLLfrom the installation folder using IF , e.g.C:\Programs Files\XXX-API\A.DLL I

Python ctypes load DLL using winmode

Sean Zhang I have installed a C++ API application that puts several DLLs ( A.DLLand B.DLL) in my program folder. A.DLLhave dependenciesB.DLL I can successfully load them ctypes.WinDLLfrom the installation folder using IF , e.g.C:\Programs Files\XXX-API\A.DLL I

Load .dll with ctypes

悲or I have a .dll that depends on some other (Sundials ODE solver) .dll. I am using Windows 8.1 with mingw to compile and link C code for .dll. When trying to load them with Python and ctypes, I get a popup with a System error: cannot find libsundials_cvode.dl

Load .dll with ctypes

悲or I have a .dll that depends on some other (Sundials ODE solver) .dll. I am using Windows 8.1 with mingw to compile and link C code for .dll. When trying to load them with Python and ctypes, I get a popup with a System error: cannot find libsundials_cvode.dl

Load .dll with ctypes

Sad or I have a .dll that depends on some other (Sundials ODE solver) .dll. I am using Windows 8.1 with mingw to compile and link C code for .dll. When trying to load them with Python and ctypes, I get a popup with a System error: cannot find libsundials_cvode

Python and Ctypes - Amplify DLL

heal you My goal is to get the magnification.dll from Windows running in Python 3.6 with Ctypes. I can zoom the screen, but I can't do the transformation of the input. I hope someone knows how to fix this and can explain to me what I'm doing wrong. thanks. ( Z

Python and Ctypes - Amplify DLL

heal you My goal is to get the magnification.dll from Windows running in Python 3.6 with Ctypes. I can zoom the screen, but I can't do the transformation of the input. I hope someone knows how to fix this and can explain to me what I'm doing wrong. thanks. ( Z

Unable to load dll using ctypes.open()

Guy I have some dlls that cannot be loaded using the ctypes.open() method. I do not know why. It has a C extern function that correctly loads all other dlls from the same path. In FF 8 the dll loads without problems, I tried on FF 22 without any success. Thank

Unable to load dll using ctypes.open()

Guy I have some dlls that cannot be loaded using the ctypes.open() method. I do not know why. It has a C extern function that correctly loads all other dlls from the same path. In FF 8 the dll loads without problems, I tried on FF 22 without any success. Thank

Unable to load dll using ctypes.open()

Guy I have some dlls that cannot be loaded using the ctypes.open() method. I do not know why. It has a C extern function that correctly loads all other dlls from the same path. In FF 8 the dll loads without problems, I tried on FF 22 without any success. Thank

Python ctypes: dll not loaded as expected

Dias I am trying to load a library in python in windows 10 (x64). The cpp file used to generate the test library is: extern "C" int check() { return(1); } Then, use the following command to generate the dll (in x64 native tools command prompt): cl /c mylib.

C dll equivalent to python ctypes

Adam Solchenberger I'm fairly capable of coding in python but would like to start using C for some functionality. Using ctypes you can open the DLL and use the functions easily. Finding an equivalent simple C example seems impossible. I can always find 2 pages

Python ctypes: dll not loaded as expected

Dias I am trying to load a library in python in windows 10 (x64). The cpp file used to generate the test library is: extern "C" int check() { return(1); } Then, use the following command to generate the dll (in x64 native tools command prompt): cl /c mylib.

Python ctypes: dll not loaded as expected

Dias I am trying to load a library in python in windows 10 (x64). The cpp file used to generate the test library is: extern "C" int check() { return(1); } Then, use the following command to generate the dll (in x64 native tools command prompt): cl /c mylib.

C dll equivalent to python ctypes

Adam Solchenberger I'm fairly capable of coding in python but would like to start using C for some functionality. Using ctypes you can open the DLL and use the functions easily. Finding an equivalent simple C example seems impossible. I can always find 2 pages

Python ctypes: dll not loaded as expected

Dias I am trying to load a library in python in windows 10 (x64). The cpp file used to generate the test library is: extern "C" int check() { return(1); } Then, use the following command to generate the dll (in x64 native tools command prompt): cl /c mylib.

Python ctypes parameter with DLL - pointer to array of doubles

lightning I'm a novice programmer using ctypes with Python and trying to use functions from a DLL written in C. I've found a lot of similar questions on SO to solve, but none of the answers to this kind of conundrum. I have the DLL loaded just fine, but one of

ctypes dll call works in ipython but not regular python

Vince W. Note: My original question was closed for being off-topic, but I'll resubmit it with an answer for anyone who might have a similar problem My system details: Windows 10 64-bit Python 3.6 64-bit Unfortunately I cannot share the data files or dlls due

Python ctypes parameter with DLL - pointer to array of doubles

lightning I'm a novice programmer using ctypes with Python and trying to use functions from a DLL written in C. I've found a lot of similar questions on SO to solve, but none of the answers to this kind of conundrum. I have the DLL loaded just fine, but one of

ctypes dll call works in ipython but not regular python

Vince W. Note: My original question was closed for being off-topic, but I'll resubmit it with an answer for anyone who might have a similar problem My system details: Windows 10 64-bit Python 3.6 64-bit Unfortunately I cannot share the data files or dlls due

ctypes dll call works in ipython but not regular python

Vince W. Note: My original question was closed for being off-topic, but I'll resubmit it with an answer for anyone who might have a similar problem My system details: Windows 10 64-bit Python 3.6 64-bit Unfortunately I cannot share the data files or dlls due

ctypes dll call works in ipython but not regular python

Vince W. Note: My original question was closed for being off-topic, but I'll resubmit it with an answer for anyone who might have a similar problem My system details: Windows 10 64-bit Python 3.6 64-bit Unfortunately I cannot share the data files or dlls due