使用用Cython揭露功能到另一个应用程序 [英] Using Cython to expose functionality to another application

查看:183
本文介绍了使用用Cython揭露功能到另一个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个C ++ code,显示如何通过它来编译一个DLL,并把它在应用程序文件夹来扩展软件:

I have this C++ code that shows how to extend a software by compiling it to 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;
}

时有可能使一个插件使用用Cython这种方式?甚至py2exe?该DLL只是必须有一个切入点,对吧?

Is is possible to make a plugin this way using Cython? Or even py2exe? The DLL just has to have an entry point, right?

或者我应该只是编译它本身并嵌入Python的使用埃尔默

Or should I just compile it natively and embed Python using elmer?

推荐答案

我认为,解决办法是同时使用。让我来解释一下。

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

用Cython可以方便地使用Python,但不便(如果可能的话),使DLL的权利那种做一个快速的插件。你可能会需要使用独立模式,以便必要Python运行时包括在内,然后用生成的C code一塌糊涂所以适当的DLL被编译。

Cython makes it convenient to make a fast plugin using python but inconvenient (if at all possible) to make the right "kind" of DLL. You would probably have to use the standalone mode so that the necessary python runtime is included and then mess with the generated c code so an appropriate DLL gets compiled.

相反,埃尔默可以方便地使DLL运行,但纯蟒蛇code这可能不够快。我认为速度是因为你正在考虑用Cython,而不是简单的嵌入一个问题。

Conversely, elmer makes it convenient to make the DLL but runs "pure" python code which might not be fast enough. I assume speed is an issue because you are considering cython as opposed to simple embedding.

我的建议是这样的:纯Python code,它埃尔默执行时要导入一个标准用Cython Python扩展,并从它执行code。这样,您就不必破解丑东西,你有两全其美的。

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

还有一个解决方案是使用 shedskin ,因为这样你可以考虑从你的Python code,它是独立于Python运行时获得C ++ code。

One more solution to consider is using shedskin, because that way you can get c++ code from your python code that is independent from the python runtime.

这篇关于使用用Cython揭露功能到另一个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆