From: William S F. <ws...@fu...> - 2007-06-25 21:26:55
|
Jake Janovetz wrote: > Hey folks-- > > I recently had need to expand an existing SWIG wrapper from Python/Java to > now include Ruby and C#. The only challenging part is with functions like: > long read_data(long buffer_length, unsigned char *buffer); > long write_data(long buffer_length, unsigned char *buffer); > > I have these wrapped in Python and Java, but looking for appropriate > typemaps in C# has not been easy. I know this subject has come up many many > times, so I'm wondering if there is some centralized documentation for this > particular subject that I'm missing. > > I know there is the standard SWIG docs, but this seems to be a FAQ that is > probably addressed somewhere. > > If not, can someone provide an example of how they've wrapped such functions > for C#? > For C#, ideally, you'd use a multi-argument typemap, passing just the one parameter (a uchar[]) from managed to unmanaged code. However, you'll then be missing the array size in the unmanaged code. You could make a callback into the managed side to extract this from the uchar[] (managed side) unsigned char* (unmanaged side). See the char* or std::string typemaps to get an idea how to add in a callback / delegate. Alternatively, pass the array and size across the PInvoke layer. Don't use multi-argument typemaps. Instead you can (optionally) make the proxy method private (use %csmethodmodifiers) then add in another method on the c# side (eg use the cscode typemap to add a method to a proxy class) which has just the one parameter which in turn calls the private method. William |