Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > extending python with a C-written dll

Reply
Thread Tools

extending python with a C-written dll

 
 
Jean-Baptiste PERIN
Guest
Posts: n/a
 
      12-20-2004
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?
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      12-20-2004
On 2004-12-20, Jean-Baptiste PERIN <> 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?
 
Reply With Quote
 
 
 
 
Jean-Baptiste PERIN
Guest
Posts: n/a
 
      12-20-2004
Grant Edwards a écrit :
> On 2004-12-20, Jean-Baptiste PERIN <> 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





 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      12-20-2004
On 2004-12-20, Jean-Baptiste PERIN <> 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
 
Reply With Quote
 
Jean-Baptiste PERIN
Guest
Posts: n/a
 
      12-20-2004
Grant Edwards a écrit :

> On 2004-12-20, Jean-Baptiste PERIN <> 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.
>


OK .. ctypes looks great .. and I plan to use it for futur purposes

The fact is that the python interpreter I use is not a standard one
It is a python interpreter delivered within a software named Blender.
I don't know whether it is possible or not to add ctypes to it ..(I
don't even have a python shell to perform the setup)

I'm sure it is possible to use dll-built with it ..
So please .. can you tell me where I can find Py_BuildValue and
Py_InitModule4 to link with ?



 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      12-20-2004
On 2004-12-20, Jean-Baptiste PERIN <> wrote:

> OK .. ctypes looks great .. and I plan to use it for futur
> purposes
>
> The fact is that the python interpreter I use is not a
> standard one It is a python interpreter delivered within a
> software named Blender. I don't know whether it is possible or
> not to add ctypes to it ..(I don't even have a python shell to
> perform the setup)
>
> I'm sure it is possible to use dll-built with it .. So please
> .. can you tell me where I can find Py_BuildValue and
> Py_InitModule4 to link with ?


Sorry, I don't know anything about doing C lanugage extensions
under Windows.

--
Grant Edwards grante Yow! Boy, am I glad it's
at only 1971...
visi.com
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      12-20-2004
> The fact is that the python interpreter I use is not a standard one
> It is a python interpreter delivered within a software named Blender.
> I don't know whether it is possible or not to add ctypes to it ..(I
> don't even have a python shell to perform the setup)
>
> I'm sure it is possible to use dll-built with it ..
> So please .. can you tell me where I can find Py_BuildValue and
> Py_InitModule4 to link with ?


I doubt that adding new modules as dlls is easier than incorporating
pure-python ones or a mixture of both - as ctypes most probably is. After
all, its pythons module loading mechanism that kicks in for all cases.

If ctypes suits your needs outside of blender, I'd go for trying to
incorporate it into the blender interpreter.

--
Regards,

Diez B. Roggisch
 
Reply With Quote
 
Thomas Heller
Guest
Posts: n/a
 
      12-20-2004
Jean-Baptiste PERIN <> writes:

> Grant Edwards a écrit :
>
>> On 2004-12-20, Jean-Baptiste PERIN <> 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.
>>

>
> OK .. ctypes looks great .. and I plan to use it for futur purposes
>
> The fact is that the python interpreter I use is not a standard one
> It is a python interpreter delivered within a software named Blender.
> I don't know whether it is possible or not to add ctypes to it ..(I
> don't even have a python shell to perform the setup)


You could try to download the ctypes windows installer, use winzip or
equivalent to unpack the contained files (you need _ctypes.pyd, and the
ctypes directory containing __init__.py), and copy them to some
directory where python can find them.

Thomas
 
Reply With Quote
 
Jean-Baptiste PERIN
Guest
Posts: n/a
 
      12-20-2004
thanks for your valuable help .. ctypes is not lost for ever ..I'm
going to explore a little bit .. it may suit my need in the end ..


I had read :

http://www.python.org/dev/doc/devel/ext/intro.html

and eveything looked so simple in it ..
I'm sad to realize that it only works for linux plateform

I've tried to build and install ctypes from sources

I ran the following command:

>> python setup.py build


and I got the following message:

>> error: Python was built with version 6 of Visual Studio, and

extensions need to
be built with the same version of the compiler, but it isn't installed.


So sad ....
 
Reply With Quote
 
Jean-Baptiste PERIN
Guest
Posts: n/a
 
      12-20-2004
Diez B. Roggisch a écrit :

>>The fact is that the python interpreter I use is not a standard one
>>It is a python interpreter delivered within a software named Blender.
>>I don't know whether it is possible or not to add ctypes to it ..(I
>>don't even have a python shell to perform the setup)
>>
>>I'm sure it is possible to use dll-built with it ..
>>So please .. can you tell me where I can find Py_BuildValue and
>>Py_InitModule4 to link with ?

>
>
> I doubt that adding new modules as dlls is easier than incorporating
> pure-python ones or a mixture of both - as ctypes most probably is. After
> all, its pythons module loading mechanism that kicks in for all cases.
>
> If ctypes suits your needs outside of blender, I'd go for trying to
> incorporate it into the blender interpreter.
>



I've tried to build and install ctypes from sources

I ran the following command:

>> python setup.py build


and I got the following message:

>> error: Python was built with version 6 of Visual Studio, and

extensions need to
be built with the same version of the compiler, but it isn't installed.


So sad ....

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
extending python: which python code calls the c iterator methods? Johannes Zellner Python 1 01-18-2006 12:22 PM
How to determine if a DLL is a COM DLL or .NET DLL Anushi ASP .Net 5 10-28-2004 01:59 PM
Why does Ruby use both tcl83.dll and tk83.dll (instead of just tk83.dll)? H. Simpson Ruby 4 08-03-2004 04:45 PM
mprapi.dll --> samlib.dll --> ntdll.dll issue. Some1 Computer Support 4 04-05-2004 02:02 AM
msvcrt.dll, msvcirt.dll, msvcrt20.dll and msvcrt40.dll, explanation please! Snoopy NZ Computing 16 08-25-2003 12:34 PM



Advertisments