Perhaps I should look into Cython as I'm currently working on a project that utilises a C API.
I've been finding that getting the data types to be exactly what the C API is expecting to be the hardest part. With the original question in mind, here's an example calling into a C++ external C API: (Works if compiled is VisualStudio. The DLL produced by MinGW-G++ didn't work). ------------------------------- // main.h #ifndef __MAIN_H__ #define __MAIN_H__ #include <windows.h> #define DLL_EXPORT __declspec(dllexport) #ifdef __cplusplus extern "C" { #endif int DLL_EXPORT add(int a, int b); #ifdef __cplusplus } #endif #endif // __MAIN_H__ ------------------------------- ------------------------------- //main.cpp #include "main.h" // a sample exported function int DLL_EXPORT add(int a, int b) { return(a + b); } extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { switch (fdwReason) { case DLL_PROCESS_ATTACH: // attach to process // return FALSE to fail DLL load break; case DLL_PROCESS_DETACH: // detach from process break; case DLL_THREAD_ATTACH: // attach to thread break; case DLL_THREAD_DETACH: // detach from thread break; } return TRUE; // succesful } ------------------------------- ------------------------------- # -*- coding: utf-8 -*- # dll.py import ctypes class DllInterface(object): dll_handle = None def __init__(self, dll_file): self.dll_handle = ctypes.WinDLL(dll_file) def add_a_and_b(self, a=0, b=0): return self.dll_handle.add(a, b) if __name__ == '__main__': dll_file = 'PythonDLL.dll' external_lib = DllInterface(dll_file) int_a = ctypes.c_int(1) int_b = ctypes.c_int(2) result = external_lib.add_a_and_b(int_a, int_b) print(result) ------------------------------- -- James -- James On 13 March 2014 15:57, Stefan Behnel <stefan...@behnel.de> wrote: > Alan Gauld, 12.03.2014 23:05: >> On 12/03/14 16:49, Stefan Behnel wrote: >>> Alan Gauld, 12.03.2014 10:11: >>>> If it were a library then you would have to call >>>> the individual C++ functions directly using >>>> something like ctypes, which is usually more >>>> complex. >>> >>> ctypes won't talk to C++, but Cython can do it quite easily. >> >> I thought it would work provided the interface functions >> were declared as C functions? That might involve >> writing a wrapper around it but that is usually >> trivial if you have to compile the source anyway. > > The thing is: if you have to write your own wrapper anyway (trivial or > not), then why not write it in Cython right away and avoid the intermediate > plain C level? > > It's usually much nicer to work with object oriented code on both sides > (assuming you understand the languages on both sides), than to try to > squeeze them through a C-ish API bottleneck in the middle. > > Stefan > > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor