0

I have some C code I'd like to wrap and call in python via ctypes.

//example.c
#include <stdio.h>
#include "example.h" //contains foo() declaration.

void foo(){
  printf("Hello World!\n");
}

Using the visual studio command line compiler I created a .dll file.

cl /LD example.c which created a file called example.dll.

I then attempt to call foo in python like this:

# example.py
import os
os.add_dll_directory(os.getcwd())
from ctypes import *

read_dll = WinDLL('example.dll')

foo = read_dll.foo #AttributeError: function 'foo' not found

And I get the Error Message: AttributeError: function 'foo' not found from the final line of example.py

CSStudent7782
  • 618
  • 1
  • 5
  • 21
  • 1
    You have to export the function with `dllexport`. See https://stackoverflow.com/questions/43269781/loading-dll-using-python-ctypes – Tony Tannous Aug 21 '20 at 17:32
  • @TonyTannous that worked, thank you. – CSStudent7782 Aug 21 '20 at 18:46
  • Also, WinDLL is for `__stdcall` calling convention. The default is `__cdecl` so use `CDLL`. Note this won't matter on x64 where there is only one calling convention, but x86 will fail if you use the wrong one. – Mark Tolonen Aug 21 '20 at 18:54

0 Answers0