3

I'm trying to load the shared object file using CDLL function in Python like below:

from ctypes import *

my_functions = CDLL("./my_functions.dll")

print(type(my_functions))
print(my_functions.square(10))

print("Done")

but got the following error:

Traceback (most recent call last):

  File "C:\Users\me\My reserach - spyder\210903_\calling_c_functions.py", line 9, in <module>
    my_functions = CDLL("./my_functions.dll")

  File "C:\Users\me\anaconda3\lib\ctypes\__init__.py", line 381, in __init__
    self._handle = _dlopen(self._name, mode)

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

Does anyone know what is causing the problem? I'm simply following the procedures in https://www.journaldev.com/31907/calling-c-functions-from-python but with .dll file instead of .so file. For a note, I'm using Windows10 and Python3.8.


[Update] OSError has been solved but another problem has occured.

I followed @Yaroslav's advice and I've now created the 64bit.dll file like below on the Command Prompt.

C:\Users\Me>call "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\Tools\VsDevCmd.bat" -arch=x64
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.10.1
** Copyright (c) 2021 Microsoft Corporation
**********************************************************************
C:\Users\Me\FileLocation>cl.exe /D_USRDL /D_WINDLL my_functions.c /MT /link /DLL /OUT:my_functions.dll /MACHINE:X64

Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30037 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

my_functions.c
Microsoft (R) Incremental Linker Version 14.29.30037.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:my_functions.exe
/DLL
/OUT:my_functions.dll
/MACHINE:X64
my_functions.obj

Now I tried calling this new .dll file from Python. This time I did not get the OSError anymore but I still got an strange error:

<class 'ctypes.CDLL'>
Traceback (most recent call last):

  File "C:\Users\Me\My reserach - spyder\210903_\calling_c_functions.py", line 12, in <module>
    print(my_functions.square(10))

  File "C:\Users\Me\anaconda3\lib\ctypes\__init__.py", line 394, in __getattr__
    func = self.__getitem__(name)

  File "C:\Users\Me\anaconda3\lib\ctypes\__init__.py", line 399, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))

AttributeError: function 'square' not found

Function 'square' in the code is somehow not recognized. Does anyone have any idea what is causing this probelm? My original C code is like below:

//Filename: my_functions.c
#include <stdio.h>

int square(int i){
    return i * i; 
}
Ogiad
  • 139
  • 1
  • 11
  • I think there is a separate class `WinDLL` for Windows libraries … – TheEagle Sep 03 '21 at 17:18
  • @Programmer, Thank you for your comment but I am not sure what you are implying. Do you mean, I need to load the shared files in the format of `WinDLL` instead of `DLL`? – Ogiad Sep 05 '21 at 15:13
  • This corresponds to a previous post: https://stackoverflow.com/questions/57187566/python-ctypes-loading-dll-throws-oserror-winerror-193-1-is-not-a-valid-win – Ogiad Sep 05 '21 at 15:51

1 Answers1

1

You have to build 64-bit dll (see -arch=x64 and /MACHINE:X64):

call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\Tools\VsDevCmd.bat" -arch=x64
     
cl.exe /D_USRDLL /D_WINDLL api.cpp /MT /link /DLL  /OUT:api.dll /MACHINE:X64