Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Embedding Python In a WINDOWS App - Help!!!!

Reply
Thread Tools

Embedding Python In a WINDOWS App - Help!!!!

 
 
Tim Stanka
Guest
Posts: n/a
 
      07-18-2004
Hello,

I'm brand new to Python and WINDOWS programming. I was given the task of
integrating a scripting language into our WINDOWS MFC application. After
some
investigation I chose Python. I have played around with Python and have
written
a little test app that embeds Python into it and I have also created python
extensions using VC++ and setup.py.

Unfortunately I have a situation where extensions in DLLs need to call
functions
which are native to the WINDOWS application. I tried to talk my coworker
into
moving this functionality into DLL's or static libraries which my extension
DLLs
could link against but he was not superly receptive because we are
leveraging
a very complex existing design as our new application.

We talked about linking in the Python static library instead of the DLL but
documentation implies that Python will not be able to load DLL extensions if
the
static library is linked into an application. Does anyone have any
experience in
this area?

The WINDOWS FAQ area describes a method of using SWIG to create extensions
which are not DLLs. This would be a way to expose an application's functions
to Python and still be able to load other extensions in DLLs. Has anyone
done this?

My last question: Will Python work with any of the 3 DLL types ?(WIN32, MFC
extension DLL and MFC regular DLL)

I'm very new to all this so maybe I'm missing the big picture. If there are
other ways
to do this please feel free to indicate so.

Thanks in advance,

Tim Stanka


 
Reply With Quote
 
 
 
 
Miki Tebeka
Guest
Posts: n/a
 
      07-19-2004
Hello Tim,

> Unfortunately I have a situation where extensions in DLLs need to call
> functions which are native to the WINDOWS application.

It's somewhere in the docs
(http://www.python.org/doc/2.3.4/ext/...embedding.html) but the
general scheme is:
1. Write a wrapper functions to the ones Python need to access.
2. Initialize the interperter.
3. Initialize the module containing the warpper functions.
4. In the Python script import the wrapping module and then use these
functions.

Exmaple:
I have a simulator which is written in C++. I've decided to add a module
using Python. Below is a shortened version of what I did.

--- sim.cpp ---
/* read_memory wrapper */
static PyObject *
py_read_memory(PyObject *self, PyObject *args)
{
unsigned addr;
unsigned long value;

if (!PyArg_ParseTuple(args, "I", &addr)) {
return NULL;
}

value = read_memory(addr); /* Call simulator function */

return PyLong_FromUnsignedLong(value);
}

static PyMethodDef SimMethods[] = {
{"read_memory", py_read_memory, METH_VARARGS, "Read internal memory"},
{NULL, NULL, 0, NULL}
};

static int
py_initialize()
{
PyObject *module, *modname, *dict;

Py_Initialize();
Py_InitModule("sim", SimMethods); /* Initialize simulator module */
}

--- sim.cpp ---

This way "read_memory" is accesible by any script run from the embedded
interpreter.
Just do:
--- module.py ---
import sim
val = ream_memory(0x1000)
--- module.py ---

HTH.
Bye.
--
------------------------------------------------------------------------
Miki Tebeka <(E-Mail Removed)>
http://tebeka.spymac.net
The only difference between children and adults is the price of the toys
 
Reply With Quote
 
 
 
 
Giles Brown
Guest
Posts: n/a
 
      07-19-2004
"Tim Stanka" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> I'm brand new to Python and WINDOWS programming. I was given the task of
> integrating a scripting language into our WINDOWS MFC application.

....
> Unfortunately I have a situation where extensions in DLLs need to call
> functions which are native to the WINDOWS application.
> I tried to talk my coworker into
> moving this functionality into DLL's or static libraries which my extension
> DLLs
> could link against but he was not superly receptive because we are
> leveraging
> a very complex existing design as our new application.


Just to clarify, you've have some existing functions that are built
into
your .exe file, but you want the C functions in the extension .dll
files
to be able to call the same functions *as C functions* that are in the
..exe file.

Here are some options:
1) Share the functions as source code.
2) Persuade your co-workers to put the functions into a .lib file and
share
using as a static library.
3) Provide Python wrapping functions for the functions in the .exe so
that
they available as built-in functions to from your applications Python
interpreter instance. Then your extension .dll files will call then
functions using *Python* function call mechanics.

Hope these suggestions are useful to you,
Giles Brown
 
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
Embedding Python in Multi-threading App. Any Guidelines, Hints, Recipes ?? adsheehan@eircom.net Python 0 06-07-2005 03:12 PM
[Q] Embedding C++/MFC app into java application Jonathan Belson Java 3 04-19-2005 01:45 PM
newbie question: embedding Python into a C++ app and calling app functions davout Python 0 08-04-2004 03:26 PM
Embedding Python in a threaded app Jesper Nilsson Python 0 11-03-2003 04:56 PM
Re: Embedding Python in multi-threaded app Harri Pesonen Python 0 09-04-2003 01:11 PM



Advertisments