From: William S F. <ws...@fu...> - 2006-03-23 23:24:42
|
Thomas Dudziak wrote: > On 3/23/06, William S Fulton <ws...@fu...> wrote: > >>SafeHandle is in .NET 2.0 and overcomes some problems with HandleRef. >>SWIG originally used IntPtr instead of HandleRef for marshalling. When >>it turned out IntPtr was no good for marshalling and I changed it to use >>HandleRef, there was some documentation on SafeHandle at msdn, but as >>.NET 2.0 was not released, SafeHandle couldn't be used. It might be >>possible to replace a SafeHandle derived class with HandleRef or even >>construct a new HandleRef class derived from SafeHandle. If so, you need >>to look for all the typemaps that have HandleRef and replace with >>SafeHandle (they are all in csharp.swg). Simply copy all the relevant >>typemaps and put them in your own interface file and modify. Let me know >>how you get on so I can look at adding doing this for a future version >>of SWIG. I might be able to create a #define so that a SafeHandle gets >>used instead of HandleRef given commandline option. Alternatively, if >>the OpenNETCF library's version of SafeHandle equates to the .NET 2.0 >>version, we could possibly use it instead for .NET 1 users as HandleRef >>is meant to have various issues, although I haven't come across any. > > I'm not really an expert when it comes to Interop, so I looked at Mono > to see what their versions of the HandleRef and also the > ThreadStaticAttribute (I think that's what's called) are, and they had > quite simple implementations (HandleRef only being a container for the > two properties, and the attribute only just an attribute class with no > functionality). > With similar implementations, I could get my code to compile, but then > faced a much graver problem. It turns out that NetCF2 does not support > several features that SWIG uses in the implementation of the PInvoke > handling: > > * string mapping does not work at all. Especially the callback for > safely creating strings always fails with a NotSupportedException > because of the string return type. NetCF2 does support delegate > callbacks (as opposed to NetCF1), but only with limited type support > (namely primitives and pointers). > Btw. MSDN suggests to use StringBuilder for mapping wchar_t * (and not > to use char * as NetCF is always unicode), and this actually works. > However I have not yet been able to create a size-independent string > mapping yet - StringBuilder needs a size to initialize - perhaps this > is possible via byte[] or IntPtr ? > > * Types such as double and float can only be mapped via pointer, not by val. > Sounds like NETCF is a poor cousin of the .NET framework. Hopefully you've worked out how to change use the typemaps to customise. There are alternative string handling typemaps at the bottom of csharp.swg which doesn't invoke a delegate/callback from C++ to C#. The capacity of a StringBuilder can be changed, perhaps a call back into the managed code to do this can be done after a C strlen has determined the size? Let us know how you get on as the StringBuilder typemaps are on my todo list. William |