[Tutor] c++ on python
Stefan Behnel
stefan_ml at behnel.de
Thu Mar 13 18:15:50 CET 2014
James Chapman, 13.03.2014 17:35:
> 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)
In Cython, that would essentially be
cdef extern from "main.h":
int add(int a, int b)
print(add(1, 2))
It compiles down to C(++), i.e. it interfaces at the API level, not the ABI
level, as ctypes would.
Stefan
More information about the Tutor
mailing list