dotgnu-general
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[DotGNU]P/Invoking C++


From: Peter Minten
Subject: [DotGNU]P/Invoking C++
Date: Sat, 18 Jan 2003 20:04:55 +0100

Hi folks,

I did some thinking about P/Invoking C++ libs without first having to port to C.
First of all these are my assumptions (I know some of them are right, some may
not be):

* A normal (non-OO) function in C++ is no more name-mangled than the same
function in C.
* The C++ lib does perfect data-hiding. Thus you only need to invoke methods.
* All objects are dynamic. In other words every object is a pointer.

Now to my approach (I know I may be reinventing the wheel here (or inventing a
square wheel :-)). The basic ideas:

* You store pointers to C++ objects in C# objects.
* There is only one P/Invoked method, this method calls functions in C++.
* The 'invoker' method gets an object pointer (null if class method is called)
and the name of a class as a string), the name of a method (as a string) and the
args.
* The invoker has a really big decision structure (autogen of course :-) which
based on the class name and the method name finds an entry, executes it and
returns it's value.
* The entry is a simply a call to the object (or class).

For example if you have a C# object FXMainWindow it could look like this:

class FXMainWindow
{

        private int handle;
        private const string classname = "FXMainWindow";

        public void Show()
        {
                CPPInvoke(handle, classname, "Show", null);
        }
}

then CPPInvoke in C++ would look like this

void * CPPInvoke(void * obj, char * classname, char * method, void ** args)
{
        select(classname)
        {
                case "FXMainWindow":
                select(method)
                {
                        case "Show":
                        return (FXMainWindow*)obj->show();
                        break; /* Never reached but always useful :-) */
                }
                break;
        }
}

Working with constructors and destructors isn't a problem in this design, simply
do 'return new FXMainWindow(...)' as the entry. It's still a bit of hand holding
of course, but the major part of the stuff can be generated from the C++ headers
by a little regex magic.

Would this actually work? If so we could use it to create a general solution to
the C++ problem.

Greetings,

Peter




reply via email to

[Prev in Thread] Current Thread [Next in Thread]