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

Discussion in 'Python' started by Tim Stanka, Jul 18, 2004.

  1. Tim Stanka

    Tim Stanka Guest

    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
    Tim Stanka, Jul 18, 2004
    #1
    1. Advertising

  2. Tim Stanka

    Miki Tebeka Guest

    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.
    --
    ------------------------------------------------------------------------
    Miki Tebeka <>
    http://tebeka.spymac.net
    The only difference between children and adults is the price of the toys
    Miki Tebeka, Jul 19, 2004
    #2
    1. Advertising

  3. Tim Stanka

    Giles Brown Guest

    "Tim Stanka" <> wrote in message news:<>...
    > 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
    Giles Brown, Jul 19, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TGU5NTY5?=

    Moving web app from windows 2000 to windows 2003 - Help

    =?Utf-8?B?TGU5NTY5?=, Apr 18, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    363
    =?Utf-8?B?TGU5NTY5?=
    Apr 18, 2005
  2. Harri Pesonen

    Re: Embedding Python in multi-threaded app

    Harri Pesonen, Sep 4, 2003, in forum: Python
    Replies:
    0
    Views:
    830
    Harri Pesonen
    Sep 4, 2003
  3. Jesper Nilsson

    Embedding Python in a threaded app

    Jesper Nilsson, Nov 3, 2003, in forum: Python
    Replies:
    0
    Views:
    250
    Jesper Nilsson
    Nov 3, 2003
  4. davout
    Replies:
    0
    Views:
    325
    davout
    Aug 4, 2004
  5. Replies:
    0
    Views:
    334
Loading...

Share This Page