使用C编写的dll扩展python [英] extending python with a C-written dll

查看:55
本文介绍了使用C编写的dll扩展python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正试图让一个可以通过python脚本访问的Windows DLL ...和

我在遇到麻烦期间链接步骤..


我使用lcc编译和链接

我在win XP下使用python 2.3


这里的编译命令似乎运行良好:

------------------------------------------------ -----

lcclnk.exe -entry inithello -dll -o hello.dll hello.obj -L

" C:\ python23 \ libs" python23.lib

这里的链接命令失败:

--------------------- ---------------

lcclnk.exe -entry inithello -dll -o hello.dll hello.obj -L

" ; C:\python23\libs" python23.lib

以下是错误:

---------------------

错误c:\ ... \plugins \ hello.c 14对__imp__Py_BuildValue的未定义引用

错误c:\ ... \plugins \ hello.c 46 undefined引用__imp__Py_InitModule4


这里是编译但不链接的C代码:

------------- -------------------------------------


# include< Python.h>


PyObject * helloHello(PyObject * self,PyObject * args)

{

printf( Hello World!;;

返回Py_BuildValue(" i",1);

}


PyObject * helloHelloString(PyObject * self,PyObject * args)

{

返回Py_BuildValue(" s",Hello World!);

}


static char MHello__doc __ [] ="这是一个简单的Python C扩展示例" ;;

static char Hello__doc __ [] ="打印在console" ;;

static char HelloString__doc __ [] ="返回消息为字符串" ;;


PyMethodDef helloMethods [] = {

{" Hello",helloHello,METH_VARARGS,Hello__doc__},

{" HelloString",helloHelloString,METH_VARARGS,HelloString__doc__},

{NULL,NULL}

};


void __declspec(dllexport)inithello()

{

(void)Py_InitModule3( hello,helloMethods,MHello__doc__);

}


任何人都可以帮助我吗?

解决方案

2004-12-20,Jean-Baptiste PERIN< jb ****** @ yahoo.fr>写道:

我正在尝试从python脚本中获取一个windows dll ..




FWIW,你可以使用ctypes模块调用dll',而不会在C中乱码。可能有性能原因构建

a" real" python模块,但我还没碰到它们。


-

Grant Edwards grante哇!我KAISER ROLL?!什么

好​​的是凯撒卷

visi.com没有一点COLE SLAW

在SIDE上?


给Edwards一个écrit:

2004-12-20,Jean-Baptiste PERIN< jb ****** @ yahoo.fr>写道:

我正在尝试从python脚本中获取一个windows dll ..



FWIW,你可以使用ctypes模块调用dll',而不会在C中乱码。可能有性能原因需要构建一个真实的模块。 python模块,但我还没碰到它们。




不是真正的性能原因..但主要是因为我有一个巨大的代码

用C生成,没有时间在python中移植它。

此外..我需要python程序与C代码交互

...( perfom调用dll定义的函数)


我不知道ctypes模块..你真的认为它可以帮助吗?

你有吗?链接以快速了解它?


我发现的唯一的事情是:
http://www.python.org/workshops/1994...Classes_5.html


2004-12-20,Jean-Baptiste PERIN< jb ****** @ yahoo.fr>写道:

我正在尝试从python脚本访问一个Windows DLL ..
FWIW,您可以使用ctypes模块调用dll',而不会在C中乱码。可能有性能原因需要构建真实模块。 python模块,但我还没碰到它们。



不是真正的性能原因..但主要是因为我有一个用C生成的巨大代码而没有是时候把它移植到python中了......
而且..我需要python程序与C
代码交互..(perfom调用dll定义的函数)

我不知道ctypes模块..你真的认为它有用吗?




它可以让你轻松调用DLL中的函数。

你有快速了解它的链接吗?

http://starship.python.net / crew / theller / ctypes /
http://starship.python.net/crew/thel.../tutorial.html

我发现的唯一一件事是:
http://www.python.org/workshops/1994...Classes_5。 html




我认为这是无关的。


-

格兰特爱德华兹格兰特哇!我有一条毛巾。

at

visi.com


Hi,

I''m trying to make a windows dll reachable from a python script .. and
I''m encountering troubles during link step ..

I use "lcc" to compile and link
I use python 2.3 under win XP

Here''s the compile command which seems to work well :
-----------------------------------------------------
lcclnk.exe -entry inithello -dll -o hello.dll hello.obj -L
"C:\python23\libs" python23.lib
Here''s the link command which fails:
------------------------------------
lcclnk.exe -entry inithello -dll -o hello.dll hello.obj -L
"C:\python23\libs" python23.lib
Here are the errors :
---------------------
Error c:\...\plugins\hello.c 14 undefined reference to __imp__Py_BuildValue
Error c:\...\plugins\hello.c 46 undefined reference to __imp__Py_InitModule4

Here''s the C code which compile but does not link :
--------------------------------------------------

#include <Python.h>

PyObject *helloHello(PyObject *self, PyObject *args)
{
printf("Hello World!");
return Py_BuildValue("i", 1);
}

PyObject *helloHelloString(PyObject *self, PyObject *args)
{
return Py_BuildValue("s", "Hello World!");
}

static char MHello__doc__[] = "This is a simple Python C extension example";
static char Hello__doc__[] = "Prints in the console";
static char HelloString__doc__[] = "Returns message as string";

PyMethodDef helloMethods[] = {
{"Hello", helloHello, METH_VARARGS, Hello__doc__},
{"HelloString", helloHelloString, METH_VARARGS, HelloString__doc__},
{NULL, NULL}
};

void __declspec(dllexport) inithello()
{
(void)Py_InitModule3("hello", helloMethods, MHello__doc__);
}

Can anyone help me?

解决方案

On 2004-12-20, Jean-Baptiste PERIN <jb******@yahoo.fr> wrote:

I''m trying to make a windows dll reachable from a python script..



FWIW, you can call dll''s using the ctypes modules without
mucking around in C. There may be performance reasons to build
a "real" python module, but I haven''t run across them yet.

--
Grant Edwards grante Yow! I KAISER ROLL?! What
at good is a Kaiser Roll
visi.com without a little COLE SLAW
on the SIDE?


Grant Edwards a écrit :

On 2004-12-20, Jean-Baptiste PERIN <jb******@yahoo.fr> wrote:

I''m trying to make a windows dll reachable from a python script..


FWIW, you can call dll''s using the ctypes modules without
mucking around in C. There may be performance reasons to build
a "real" python module, but I haven''t run across them yet.



Not really a performance reason .. but mainly because I have a huge code
produced in C and no time to port it in python ..
Moreover .. I need the python program to interact with the C code
...(perfom calls to dll-defined functions)

I don''t know ctypes module .. do you really think it can help ?
Do you have a link to quickly learn about it ?

The only thing I found is :
http://www.python.org/workshops/1994...Classes_5.html


On 2004-12-20, Jean-Baptiste PERIN <jb******@yahoo.fr> wrote:

I''m trying to make a windows dll reachable from a python script..
FWIW, you can call dll''s using the ctypes modules without
mucking around in C. There may be performance reasons to build
a "real" python module, but I haven''t run across them yet.



Not really a performance reason .. but mainly because I have a
huge code produced in C and no time to port it in python ..
Moreover .. I need the python program to interact with the C
code ..(perfom calls to dll-defined functions)

I don''t know ctypes module .. do you really think it can help?



It lets you easily call functions in DLLs.
Do you have a link to quickly learn about it ?
http://starship.python.net/crew/theller/ctypes/
http://starship.python.net/crew/thel.../tutorial.html
The only thing I found is :
http://www.python.org/workshops/1994...Classes_5.html



I think that is unrelated.

--
Grant Edwards grante Yow! I HAVE a towel.
at
visi.com


这篇关于使用C编写的dll扩展python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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