Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to import a c code from a python script.

I get an error as below:
Quote:
Traceback (most recent call last):
File "function.py", line 3, in <module>
hllDll = ctypes.WinDLL ("someso.dll")
File "C:\Python35\lib\ctypes\__init__.py", line 347, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application


My aim is to use the function "myFunc" into my python script.

What I have tried:

I created a file named : "simple.c" as below:
int myFunc(int num)
{
  if (num == 0)
    return 0;
  else
    return num;
}


I compiled it to create a shared object named "someso.so"

Below is my python code:
import ctypes
hllDll = ctypes.WinDLL ("someso.dll")


I have also tried other modules from ctypes.
I have a 64 bit windows and 64 bit python application.
Posted
Updated 27-Mar-19 4:11am
Comments
CPallini 27-Mar-19 8:25am    
As Richard pointed out, you should create a Windows Dynamic Library (DLL).
The .so extension is typical of the Linux ones.

You have create a shared object someso.so (which will not run in Windows) but you are trying to load someso.dll.
 
Share this answer
 
Comments
CPallini 27-Mar-19 8:24am    
Indeed. 5.
Richard MacCutchan 27-Mar-19 8:30am    
Thank you.
Rahul VB 27-Mar-19 9:55am    
Thanks for the solution, you right. I installed Ming-w compiler for 64 bit applications. Created a c code containing the dll export attribute indicating the compiler that the application is meant to export the selected function.
I too will post a solution please feel free to review and comment on it.
Thanks again for help
I would like to thank Richard MacCutchan for his helpful solution. Using that i figured out a way to call dlls from python code.
1. Firstly if your python application is 64 bit, the dll which you create using MingW should also be 64 bit. so make sure to download a compatible version of the dll file.
2. Please do not create shared objects and try to import them, it wont work.
Shared objects are used in POSIX systems. If you do so you will get the below error:

Quote:
Traceback (most recent call last):
File "function.py", line 3, in <module>
hllDll = ctypes.WinDLL ("someso.dll")
File "C:\Python35\lib\ctypes\__init__.py", line 347, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application


Lets say we have a below c program in a file called mydll.c:
__declspec(dllexport) int __cdecl Add(int a, int b)
{
  return (a + b);
}

- ____declspec(dllexport): This basically allows us to specify a storage class specifier.
- This indicates to the compiler that a function or an objecy is being exported from a dll file.
- Similarly there is another storage class specifier called dllimport, this tells the compiler that the
exported object can be imported in an other file.

Note: You can use a Mingw compiler as per your python application i.e 32 or 64 bit.
You can compile using below steps:

gcc -c mydll.c
gcc -o basicDLL.dll -s -shared mydll.o -Wl,--subsystem,windows
python basicApplication.py
python basicApplication.py


Lets look at the flags used in the above commands for compilation:
-Wl: All which follow this flag are passed as an argument to the linker.
This eventually will become a linker call as below:
ld --subsystem windows
Basically this call will instruct the linker to set PE header subsystem type to windows.
This will indicate that a dll is to be created which is compiled using mingw and is to be run in windows.

Lets look at the python side code:
from ctypes import *
lib = CDLL('basicDLL')
print (lib.Add(1,2))


Name that file to anything lets say basicApplication.py and lets run it.
The output is 3

Please note: Making any changes in the dll, will have to be rebuilt again. Which means you first compile the .c file and then generate a dll out of it.

ctypes is a module which is very helpful to extend c and python functionalities with each other.
 
Share this answer
 
Comments
Richard MacCutchan 27-Mar-19 10:29am    
Or you could make it easier by using the Microsoft C compiler that comes free with Visual Studio Community Edition.
Rahul VB 27-Mar-19 10:35am    
Great, will try that too. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900