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

T

Tim Stanka

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
 
M

Miki Tebeka

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/extending-with-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.
 
G

Giles Brown

Tim Stanka said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top