|
From: William S F. <ws...@fu...> - 2008-03-20 22:14:55
|
Marcell Balla wrote:
> Hello SWIG users, I need your help.
>
> I managed to wrap many C++ classes to C#. SWIG is something very useful!
> However I noticed that SWIG created many C# files that start with
> "SWIGTYPE_p_" and contain wrapper classes for C++ pointers.
>
> Example: "SWIGTYPE_p_CString.cs"
> --------------------------------------------------------
> public class SWIGTYPE_p_CString {
> private HandleRef swigCPtr;
>
> internal SWIGTYPE_p_CString(IntPtr cPtr, bool futureUse) {
> swigCPtr = new HandleRef(this, cPtr);
> }
>
> protected SWIGTYPE_p_CString() {
> swigCPtr = new HandleRef(null, IntPtr.Zero);
> }
>
> internal static HandleRef getCPtr(SWIGTYPE_p_CString obj) {
> return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
> }
> }
>
> I doesn't seem to be able to use these classes. How do I create an
> object of this class?
> I need one, because one of the wrapper classes needs a pointer to a
> CString in its constructor.
>
> Please tell me: how can I make use of these "SWIGTYPE_p_" classes?
>
These type wrapper classes as they are called get generated when SWIG
isn't sure how to handle them. For example in your case, CString is
perhaps the MFC CString class and you havn't given the definition of
CString to SWIG. You could let SWIG parse this class or probably a
better solution is to write some typemaps to marshal CString to a C#
string type --- I suggest you copy and modify the std::string wrappers
in std_string.i for CString.
Each type wrapper class should be looked at individually and might have
different solutions. Often they point to types that SWIG should have
parsed, but you havn't given the definition before the type gets used.
Some pointers to primitive types, eg int * can be fixed by applying the
typemaps in typemaps.i. Sometimes these pointers are arrays, in which
you can consider using the arrays library in carrays.i.
Any of the type wrapper classes can be passed around if you can create
an object of the underlying type. Sometimes this is the only solution
for opaque pointers. Add in a factory method, eg:
// in your .h file:
struct XYZ*;
void useXYZ(XYZ* x);
Add this in:
%inline %{
XYZ* createXYZ() {... } // create an XYZ and return by pointer
%}
Then in C# you can use it like this:
SWIGTYPE_p_XYZ = xyz = createXYZ();
useXYZ(xyz);
William
|