Using Cython to provide functionality to another application

I have this C ++ code that shows how to extend the software by compiling it into a DLL and putting it in the application folder:

#include <windows.h> #include <DemoPlugin.h> /** A helper function to convert a char array into a LPBYTE array. */ LPBYTE message(const char* message, long* pLen) { size_t length = strlen(message); LPBYTE mem = (LPBYTE) GlobalAlloc(GPTR, length + 1); for (unsigned int i = 0; i < length; i++) { mem[i] = message[i]; } *pLen = length + 1; return mem; } long __stdcall Execute(char* pMethodName, char* pParams, char** ppBuffer, long* pBuffSize, long* pBuffType) { *pBuffType = 1; if (strcmp(pMethodName, "") == 0) { *ppBuffer = (char*) message("Hello, World!", pBuffSize); } else if (strcmp(pMethodName, "Count") == 0) { char buffer[1024]; int length = strlen(pParams); *ppBuffer = (char*) message(itoa(length, buffer, 10), pBuffSize); } else { *ppBuffer = (char*) message("Incorrect usage.", pBuffSize); } return 0; } 

Is it possible to make a plugin this way using Cython? Or even py2exe? A DLL just needs to have an entry point, right?

Or do you just need to compile it natively and paste Python with elmer ?

+3
source share
1 answer

I think the solution is to use both. Let me explain.

Cython makes it easy to create a quick plugin using python, but it’s inconvenient (if at all possible) to make the DLL look right. You will probably have to use offline mode to enable the python runtime, and then run the generated c code so that the corresponding DLL compiles.

Conversely, elmer simplifies the creation of DLLs, but runs "clean" python code, which may not be fast enough. I assume speed is a problem because you are considering cython, not a simple attachment.

My suggestion is this: pure python code that elmer executes should import the standard python extension cython and execute code from it. That way, you don't have to hack anything ugly, and you have the best of both worlds.


Another solution to consider is to use shedskin , because in this way you can get C ++ code from your Python code, which does not depend on python runtime.

+3
source

Source: https://habr.com/ru/post/1499649/


All Articles