[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Sat, 18 Jan 2003 20:04:55 +0100
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
* 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
* 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:
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)
break; /* Never reached but always useful :-) */
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.
- [DotGNU]P/Invoking C++,
Peter Minten <=