[C++-sig] Embedding in .NET architecture

Nick Trout nick at rockstarvancouver.com
Sat Jun 5 02:02:39 CEST 2004


Hello!

I'm trying to embed Python into Windows Forms Application I'm working
on. .NET is relatively new to me and having just got over the Managed
objects vs unmanaged objects problems I'm now having trouble getting
Boost working.

My objective is to start up an application and then use Python scripts
to customise the application and do various processing. I want to expose
various features of the application to Python to enable this. Given what
I am trying to do I think it's more appropriate to embed than to extend.

I've been through the archives and looked at the embedding.cpp test
example that is in the Boost source but I can't seem to find any
information which fixes my problem. When I run the test suite the
embedding.cpp example works when bjam compiles and runs it. However,
when I copy the example into my code and run it throws and exception
which crashes the application.

I'm trying to get my application running using multithreaded DLL
support. I've tried linking Boost against libboost statically linked
libraries and against the boost_python dynamic libraries but I keep
getting exceptions of various kinds (TEH, missing types etc). I know
Boost works with MSVC 7.1 because other people seem to be using it and
all the tests suite worked for me. What I haven't been able to do is get
Boost.Python working in a MSVC app that uses managed objects. I'm
therefore assuming that I don't have the correct settings, am missing
some configuration #define or am just too much of a novice at the .NET
framework to understand what the problem is.

One thing I haven't tried, which is repeated on the mailing list is to
build my application using bjam. I haven't tried this as I don't know
how it would work with all the "assemblies" and stuff that goes into a
WFA. I really don't want to have to do this if I can avoid it.

Current problem I am having: When I run the embedded code in my app:

    if (python::handle_exception( test ))
    {
        if (PyErr_Occurred())
            PyErr_Print();
        return 1;
    }

The exception handler fails because apparently test provides an invalid
(NULL) pointer so f() has nothing to call in the try-catch handler.

If I call test() directly in:

<<<<
    // Register the module with the interpreter
    if (PyImport_AppendInittab("embedded_hello", initembedded_hello) ==
-1)
        throw std::runtime_error("Failed to add embedded_hello to the
interpreter's "
                                 "builtin modules");

    // Initialize the interpreter
    Py_Initialize();

    // Retrieve the main module
    python::object main_module = python::extract<python::object>(
        PyImport_AddModule("__main__")
    )();
    
    // Retrieve the main module's namespace
    python::object main_namespace(main_module.attr("__dict__"));
>>>>

Execution gets as far as "python::object main_module..." and I get the
following error:

<<<<
An unhandled exception of type 'System.TypeLoadException' occurred in
WorldEditor.exe

Additional information: Could not load type
boost.python.detail.new_non_null_reference_t from assembly WorldEditor,
Version=1.0.1616.28683, Culture=neutral, PublicKeyToken=null.
>>>>

I've tried to set all of the correct project settings. I have
asynchronous exeception handling turned on (/EHa) as opposed to the
default (/EHsc) and RTTI is on.

I hope you can help please. It may be obvious to a .NET expert. If not,
I'll probably give this another day and move to PyCXX (and then Lua!)

Nick






More information about the Cplusplus-sig mailing list