[Mono-list] Interop Utility
Jonathan Pryor
jonpryor at vt.edu
Wed Nov 30 21:28:01 EST 2005
On Wed, 2005-11-30 at 18:10 -0500, Abe Gillespie wrote:
> I seem to remember there was something lightweight out there where you
> might choose just one function in a C library and the utility would
> write the function declaration for you. Am I making this up?
I haven't seen such a utility, largely because such a utility is
non-trivial. Consider a C `char*' parameter -- there are no less than
*six* potential C# bindings for it: string (if you know it won't be
modified), StringBuilder (if it's an output buffer), sbyte[] (for raw
binary data or "strings" in an unknown encoding), and IntPtr or void* or
sbyte* for raw pointer behavior.
How's a generator supposed to know which is appropriate? It can't. It
requires human interpretation of documentation (or the function's source
code) to determine which C# binding is appropriate. See also:
http://www.mono-project.com/dllimport
Strings are just the beginning. Anything dealing with structures is a
world of hurt. At best you can do it entirely in C#, at worst you'll
need a C glue layer (see MonoPosixHelper, which provides glue code for a
number of C structures, and *must* do so because the size/ordering of
POSIX structures is implementation dependent).
Show me a program that handles the above, and I'll show you something
that isn't fully covering all the potential corner cases. :-)
An easier program would be a C# -> C glue code generator, since C#->C
doesn't have as many contextual problems. Add in some custom
attributes, and glue code becomes somewhat easier to deal with.
(Mono.Posix does this to some extent with the make-map.exe program,
which is used to generate enumeration translation functions and function
prototypes for the P/Invoke functions. It's not a general-purpose
program either, though.)
- Jon
More information about the Mono-list
mailing list