Need help with C# generated code

View: New views
6 Messages — Rating Filter:   Alert me  

Need help with C# generated code

by Ryan Koehnen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello list members,

I have some questions on how to use swig C# generated code. Specifically I have questions about the generated SWIGTYPE_p classes and how to use them.  If the answer is obvious, I apologize for my naivety on this subject.   Here's the example:


Source C Header snippet used as reference in .i file:

DECLSPECIFIER int    mCreateDataFile(char* FileName,char* OrgName,int ClobberFlag,int* DFID);


Generated C wrapper function:

SWIGEXPORT int SWIGSTDCALL CSharp_mCreateDataSet(char * jarg1, char * jarg2, int jarg3, void * jarg4) {
 int jresult ;
 char *arg1 = (char *) 0 ;
 char *arg2 = (char *) 0 ;
 int arg3 ;
 int *arg4 = (int *) 0 ;
 int result;

 arg1 = (char *)jarg1;
 arg2 = (char *)jarg2;
 arg3 = (int)jarg3;
 arg4 = (int *)jarg4;
 result = (int)mCreateDataSet(arg1,arg2,arg3,arg4);
 jresult = result;
 return jresult;
}


Generated pinvoke call:

 [DllImport("meraf_swig_wrapper", EntryPoint="CSharp_mCreateDataFile")]
 public static extern int mCreateDataFile(string jarg1, string jarg2, int jarg3, HandleRef jarg4);


Generated final C# call meant for consumption:

 public static int mCreateDataSet(string DSName, string Organization, int ClobberFlag, SWIGTYPE_p_int DSIndex) {
   int ret = Meraf_Swig_WrapperPINVOKE.mCreateDataSet(DSName, Organization, ClobberFlag, SWIGTYPE_p_int.getCPtr(DSIndex));
   return ret;
 }

I am having trouble using the generated custom type SWIGTYPE_p_int class:

public class SWIGTYPE_p_int {
 private HandleRef swigCPtr;

 internal SWIGTYPE_p_int(IntPtr cPtr, bool futureUse) {
   swigCPtr = new HandleRef(this, cPtr);
 }

 protected SWIGTYPE_p_int() {
   swigCPtr = new HandleRef(null, IntPtr.Zero);
 }

 internal static HandleRef getCPtr(SWIGTYPE_p_int obj) {
   return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
 }
}


Here's a sample implmentation:

   static void Main(string[] args)
   {
             SWIGTYPE_p_int p;  //what am I supposed to do with this here?  set it to null? new doesn't work.
             Meraf_Swig_Wrapper.mCreateDataFile(@"c:\temp\test12.mrf", "ryan", 1, p);
         }

How do I properly initialize this type? I get a runtime error of unhandled exception of type 'System.TypeInitializationException' when I instantiate SWIGTYPE_p_int and set it to null or set it to new SWIGTYPE_p_int().

Also if I need to set it to new somehow then why are all of the constructors and method set to internal or protected?  I don't wish to make this part of the same assembly as my final code.  Am I supposed to inherit to use the protected constructor  and then use it somehow?


Thanks for any help.

Ryan

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: Need help with C# generated code

by Ryan Koehnen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Can anybody point me in the right direction on the proper use of the generated SWIGTYPE_p classes?  Could really use a clue at the moment.

Thanks,

Ryan

On Wed, Sep 17, 2008 at 5:17 PM, Ryan Koehnen <ryan.koehnen@...> wrote:
Hello list members,

I have some questions on how to use swig C# generated code. Specifically I have questions about the generated SWIGTYPE_p classes and how to use them.  If the answer is obvious, I apologize for my naivety on this subject.   Here's the example:


Source C Header snippet used as reference in .i file:

DECLSPECIFIER int    mCreateDataFile(char* FileName,char* OrgName,int ClobberFlag,int* DFID);


Generated C wrapper function:

SWIGEXPORT int SWIGSTDCALL CSharp_mCreateDataSet(char * jarg1, char * jarg2, int jarg3, void * jarg4) {
 int jresult ;
 char *arg1 = (char *) 0 ;
 char *arg2 = (char *) 0 ;
 int arg3 ;
 int *arg4 = (int *) 0 ;
 int result;

 arg1 = (char *)jarg1;
 arg2 = (char *)jarg2;
 arg3 = (int)jarg3;
 arg4 = (int *)jarg4;
 result = (int)mCreateDataSet(arg1,arg2,arg3,arg4);
 jresult = result;
 return jresult;
}


Generated pinvoke call:

 [DllImport("meraf_swig_wrapper", EntryPoint="CSharp_mCreateDataFile")]
 public static extern int mCreateDataFile(string jarg1, string jarg2, int jarg3, HandleRef jarg4);


Generated final C# call meant for consumption:

 public static int mCreateDataSet(string DSName, string Organization, int ClobberFlag, SWIGTYPE_p_int DSIndex) {
   int ret = Meraf_Swig_WrapperPINVOKE.mCreateDataSet(DSName, Organization, ClobberFlag, SWIGTYPE_p_int.getCPtr(DSIndex));
   return ret;
 }

I am having trouble using the generated custom type SWIGTYPE_p_int class:

public class SWIGTYPE_p_int {
 private HandleRef swigCPtr;

 internal SWIGTYPE_p_int(IntPtr cPtr, bool futureUse) {
   swigCPtr = new HandleRef(this, cPtr);
 }

 protected SWIGTYPE_p_int() {
   swigCPtr = new HandleRef(null, IntPtr.Zero);
 }

 internal static HandleRef getCPtr(SWIGTYPE_p_int obj) {
   return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
 }
}


Here's a sample implmentation:

   static void Main(string[] args)
   {
             SWIGTYPE_p_int p;  //what am I supposed to do with this here?  set it to null? new doesn't work.
             Meraf_Swig_Wrapper.mCreateDataFile(@"c:\temp\test12.mrf", "ryan", 1, p);
         }

How do I properly initialize this type? I get a runtime error of unhandled exception of type 'System.TypeInitializationException' when I instantiate SWIGTYPE_p_int and set it to null or set it to new SWIGTYPE_p_int().

Also if I need to set it to new somehow then why are all of the constructors and method set to internal or protected?  I don't wish to make this part of the same assembly as my final code.  Am I supposed to inherit to use the protected constructor  and then use it somehow?


Thanks for any help.

Ryan


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: Need help with C# generated code

by David Piepgrass :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

SWIGTYPE_p_* represents an opaque handle. It is generated when SWIG doesn't know how to represent a type in the target language. In this case SWIG doesn’t have typemaps for int*, so it uses SWIGTYPE_p_int.

 

The first step to dealing with this problem is to think about what the int* argument means. Is it an array? In that case you’ll need a typemap for mapping a C# array to a C++ array. Is it a reference or output parameter? In that case you’ll need a typemap for mapping a C# “ref int” or “out int” to a C++ int*. You can see that SWIG can’t deal with int* automatically, because it is ambiguous.

 

You can use SWIGTYPE_p_int directly if some other function returns an int*. It will come out as SWIGTYPE_p_int in C#. In that case you can pass the SWIGTYPE_p_int object to your “mCreateDataFile” function.

 


From: swig-user-bounces@... [mailto:swig-user-bounces@...] On Behalf Of Ryan Koehnen
Sent: Thursday, September 18, 2008 8:34 AM
To: swig-user@...
Subject: Re: [Swig-user] Need help with C# generated code

 

Can anybody point me in the right direction on the proper use of the generated SWIGTYPE_p classes?  Could really use a clue at the moment.

Thanks,

Ryan

On Wed, Sep 17, 2008 at 5:17 PM, Ryan Koehnen <ryan.koehnen@...> wrote:

Hello list members,

I have some questions on how to use swig C# generated code. Specifically I have questions about the generated SWIGTYPE_p classes and how to use them.  If the answer is obvious, I apologize for my naivety on this subject.   Here's the example:


Source C Header snippet used as reference in .i file:

DECLSPECIFIER int    mCreateDataFile(char* FileName,char* OrgName,int ClobberFlag,int* DFID);


Generated C wrapper function:

SWIGEXPORT int SWIGSTDCALL CSharp_mCreateDataSet(char * jarg1, char * jarg2, int jarg3, void * jarg4) {
 int jresult ;
 char *arg1 = (char *) 0 ;
 char *arg2 = (char *) 0 ;
 int arg3 ;
 int *arg4 = (int *) 0 ;
 int result;

 arg1 = (char *)jarg1;
 arg2 = (char *)jarg2;
 arg3 = (int)jarg3;
 arg4 = (int *)jarg4;
 result = (int)mCreateDataSet(arg1,arg2,arg3,arg4);
 jresult = result;
 return jresult;
}


Generated pinvoke call:

 [DllImport("meraf_swig_wrapper", EntryPoint="CSharp_mCreateDataFile")]
 public static extern int mCreateDataFile(string jarg1, string jarg2, int jarg3, HandleRef jarg4);


Generated final C# call meant for consumption:

 public static int mCreateDataSet(string DSName, string Organization, int ClobberFlag, SWIGTYPE_p_int DSIndex) {
   int ret = Meraf_Swig_WrapperPINVOKE.mCreateDataSet(DSName, Organization, ClobberFlag, SWIGTYPE_p_int.getCPtr(DSIndex));
   return ret;
 }

I am having trouble using the generated custom type SWIGTYPE_p_int class:

public class SWIGTYPE_p_int {
 private HandleRef swigCPtr;

 internal SWIGTYPE_p_int(IntPtr cPtr, bool futureUse) {
   swigCPtr = new HandleRef(this, cPtr);
 }

 protected SWIGTYPE_p_int() {
   swigCPtr = new HandleRef(null, IntPtr.Zero);
 }

 internal static HandleRef getCPtr(SWIGTYPE_p_int obj) {
   return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
 }
}


Here's a sample implmentation:

   static void Main(string[] args)
   {
             SWIGTYPE_p_int p;  //what am I supposed to do with this here?  set it to null? new doesn't work.
             Meraf_Swig_Wrapper.mCreateDataFile(@"c:\temp\test12.mrf", "ryan", 1, p);
         }

How do I properly initialize this type? I get a runtime error of unhandled exception of type 'System.TypeInitializationException' when I instantiate SWIGTYPE_p_int and set it to null or set it to new SWIGTYPE_p_int().

Also if I need to set it to new somehow then why are all of the constructors and method set to internal or protected?  I don't wish to make this part of the same assembly as my final code.  Am I supposed to inherit to use the protected constructor  and then use it somehow?


Thanks for any help.

Ryan

 


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: Need help with C# generated code

by Ryan Koehnen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ok, got it, thanks ;-)

The next thing I'm stuck on is defining an array properly when the return C type is float*.

Example:

mGetScalarI(int SchemeIndex,int index,long FromX,long ToX,long FromY,long ToY,long FromZ,
        long ToZ,float* R_Values);

Where return type float* R_Values (in C#) should be float[,,] a 3 dimensional array (not an array of arrays).

I know I should probably use typemaps somehow, but the examples all show for perl,tcl and python so I'm not following them well.

A simple example would be awesome.

Thanks again,

Ryan


On Thu, Sep 18, 2008 at 10:15 AM, David Piepgrass <dpiepgrass@...> wrote:

SWIGTYPE_p_* represents an opaque handle. It is generated when SWIG doesn't know how to represent a type in the target language. In this case SWIG doesn't have typemaps for int*, so it uses SWIGTYPE_p_int.

 

The first step to dealing with this problem is to think about what the int* argument means. Is it an array? In that case you'll need a typemap for mapping a C# array to a C++ array. Is it a reference or output parameter? In that case you'll need a typemap for mapping a C# "ref int" or "out int" to a C++ int*. You can see that SWIG can't deal with int* automatically, because it is ambiguous.

 

You can use SWIGTYPE_p_int directly if some other function returns an int*. It will come out as SWIGTYPE_p_int in C#. In that case you can pass the SWIGTYPE_p_int object to your "mCreateDataFile" function.

 


From: swig-user-bounces@... [mailto:swig-user-bounces@...] On Behalf Of Ryan Koehnen
Sent: Thursday, September 18, 2008 8:34 AM
To: swig-user@...
Subject: Re: [Swig-user] Need help with C# generated code

 

Can anybody point me in the right direction on the proper use of the generated SWIGTYPE_p classes?  Could really use a clue at the moment.

Thanks,

Ryan

On Wed, Sep 17, 2008 at 5:17 PM, Ryan Koehnen <ryan.koehnen@...> wrote:

Hello list members,

I have some questions on how to use swig C# generated code. Specifically I have questions about the generated SWIGTYPE_p classes and how to use them.  If the answer is obvious, I apologize for my naivety on this subject.   Here's the example:


Source C Header snippet used as reference in .i file:

DECLSPECIFIER int    mCreateDataFile(char* FileName,char* OrgName,int ClobberFlag,int* DFID);


Generated C wrapper function:

SWIGEXPORT int SWIGSTDCALL CSharp_mCreateDataSet(char * jarg1, char * jarg2, int jarg3, void * jarg4) {
 int jresult ;
 char *arg1 = (char *) 0 ;
 char *arg2 = (char *) 0 ;
 int arg3 ;
 int *arg4 = (int *) 0 ;
 int result;

 arg1 = (char *)jarg1;
 arg2 = (char *)jarg2;
 arg3 = (int)jarg3;
 arg4 = (int *)jarg4;
 result = (int)mCreateDataSet(arg1,arg2,arg3,arg4);
 jresult = result;
 return jresult;
}


Generated pinvoke call:

 [DllImport("meraf_swig_wrapper", EntryPoint="CSharp_mCreateDataFile")]
 public static extern int mCreateDataFile(string jarg1, string jarg2, int jarg3, HandleRef jarg4);


Generated final C# call meant for consumption:

 public static int mCreateDataSet(string DSName, string Organization, int ClobberFlag, SWIGTYPE_p_int DSIndex) {
   int ret = Meraf_Swig_WrapperPINVOKE.mCreateDataSet(DSName, Organization, ClobberFlag, SWIGTYPE_p_int.getCPtr(DSIndex));
   return ret;
 }

I am having trouble using the generated custom type SWIGTYPE_p_int class:

public class SWIGTYPE_p_int {
 private HandleRef swigCPtr;

 internal SWIGTYPE_p_int(IntPtr cPtr, bool futureUse) {
   swigCPtr = new HandleRef(this, cPtr);
 }

 protected SWIGTYPE_p_int() {
   swigCPtr = new HandleRef(null, IntPtr.Zero);
 }

 internal static HandleRef getCPtr(SWIGTYPE_p_int obj) {
   return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
 }
}


Here's a sample implmentation:

   static void Main(string[] args)
   {
             SWIGTYPE_p_int p;  //what am I supposed to do with this here?  set it to null? new doesn't work.
             Meraf_Swig_Wrapper.mCreateDataFile(@"c:\temp\test12.mrf", "ryan", 1, p);
         }

How do I properly initialize this type? I get a runtime error of unhandled exception of type 'System.TypeInitializationException' when I instantiate SWIGTYPE_p_int and set it to null or set it to new SWIGTYPE_p_int().

Also if I need to set it to new somehow then why are all of the constructors and method set to internal or protected?  I don't wish to make this part of the same assembly as my final code.  Am I supposed to inherit to use the protected constructor  and then use it somehow?


Thanks for any help.

Ryan

 



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: Need help with C# generated code

by David Piepgrass :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I don't remember whether SWIG has an "official" way to pass arrays. Here
is what I use:

// Array marshaling for arrays of primitives
%define %cs_marshal_array(TYPE, CSTYPE)
        %typemap(ctype)  TYPE[] "void*"
        %typemap(imtype,
inattributes="[MarshalAs(UnmanagedType.LPArray)]") TYPE[] "CSTYPE[]"
        %typemap(cstype) TYPE[] "CSTYPE[]"
        %typemap(in)     TYPE[] %{ $1 = (TYPE*)$input; %}
        %typemap(csin)   TYPE[] "$csinput"
%enddef

// The following macro invocations allow you to pass arrays of primitive

// types. Arrays of other things such as System.Drawing.Point are also
// possible.
%cs_marshal_array(bool, bool)
%cs_marshal_array(short, short)
%cs_marshal_array(unsigned short, ushort)
%cs_marshal_array(int, int)
%cs_marshal_array(unsigned int, uint)
%cs_marshal_array(long, int)
%cs_marshal_array(unsigned long, uint)
%cs_marshal_array(long long, long)
%cs_marshal_array(unsigned long long, ulong)
%cs_marshal_array(float, float)
%cs_marshal_array(double, double)

but for this to work, the function declaration needs to declare the
parameter as TYPE[], not TYPE*.  IIRC, if you then use

%apply float[] { float* R_Values }

...the array typemap will be applied to your "float* R_Values" argument.

I don't know whether it's possible to marshal float[,,]. You should try
passing float[] first, and if you get that working, then try customizing
the imtype and cstype typemaps to use float[,,] and see if it works.

P.S. remember, typemaps (etc) must come before the function
declarations.

> mGetScalarI(int SchemeIndex,int index,long FromX,long ToX,long
FromY,long

> ToY,long FromZ,
>         long ToZ,float* R_Values);
> -----Original Message-----
> From: Ryan Koehnen [mailto:ryan.koehnen@...]
> Sent: Thursday, September 18, 2008 10:32 AM
> To: David Piepgrass
> Cc: swig-user@...
> Subject: Re: [Swig-user] Need help with C# generated code
>
> Ok, got it, thanks ;-)
>
> The next thing I'm stuck on is defining an array properly when the
return
> C type is float*.
>
> Example:
>
> mGetScalarI(int SchemeIndex,int index,long FromX,long ToX,long
FromY,long
> ToY,long FromZ,
>         long ToZ,float* R_Values);
>
> Where return type float* R_Values (in C#) should be float[,,] a 3
> dimensional array (not an array of arrays).
>
> I know I should probably use typemaps somehow, but the examples all
show

> for perl,tcl and python so I'm not following them well.
>
> A simple example would be awesome.
>
> Thanks again,
>
> Ryan
>
>
>
> On Thu, Sep 18, 2008 at 10:15 AM, David Piepgrass
> <dpiepgrass@...> wrote:
>
>
> SWIGTYPE_p_* represents an opaque handle. It is generated when
SWIG
> doesn't know how to represent a type in the target language. In this
case
> SWIG doesn't have typemaps for int*, so it uses SWIGTYPE_p_int.
>
>
>
> The first step to dealing with this problem is to think about
what
> the int* argument means. Is it an array? In that case you'll need a
> typemap for mapping a C# array to a C++ array. Is it a reference or
output
> parameter? In that case you'll need a typemap for mapping a C# "ref
int"
> or "out int" to a C++ int*. You can see that SWIG can't deal with int*
> automatically, because it is ambiguous.
>
>
>
> You can use SWIGTYPE_p_int directly if some other function
returns
> an int*. It will come out as SWIGTYPE_p_int in C#. In that case you
can

> pass the SWIGTYPE_p_int object to your "mCreateDataFile" function.
>
>
>
>
> ________________________________
>
>
> From: swig-user-bounces@... [mailto:swig-user-
> bounces@...] On Behalf Of Ryan Koehnen
> Sent: Thursday, September 18, 2008 8:34 AM
> To: swig-user@...
> Subject: Re: [Swig-user] Need help with C# generated code
>
>
>
> Can anybody point me in the right direction on the proper use of
the

> generated SWIGTYPE_p classes?  Could really use a clue at the moment.
>
> Thanks,
>
> Ryan
>
> On Wed, Sep 17, 2008 at 5:17 PM, Ryan Koehnen
> <ryan.koehnen@...> wrote:
>
> Hello list members,
>
> I have some questions on how to use swig C# generated code.
> Specifically I have questions about the generated SWIGTYPE_p classes
and
> how to use them.  If the answer is obvious, I apologize for my naivety
on

> this subject.   Here's the example:
>
>
> Source C Header snippet used as reference in .i file:
>
> DECLSPECIFIER int    mCreateDataFile(char* FileName,char*
> OrgName,int ClobberFlag,int* DFID);
>
>
> Generated C wrapper function:
>
> SWIGEXPORT int SWIGSTDCALL CSharp_mCreateDataSet(char * jarg1,
char

> * jarg2, int jarg3, void * jarg4) {
> int jresult ;
> char *arg1 = (char *) 0 ;
> char *arg2 = (char *) 0 ;
> int arg3 ;
> int *arg4 = (int *) 0 ;
> int result;
>
> arg1 = (char *)jarg1;
> arg2 = (char *)jarg2;
> arg3 = (int)jarg3;
> arg4 = (int *)jarg4;
> result = (int)mCreateDataSet(arg1,arg2,arg3,arg4);
> jresult = result;
> return jresult;
> }
>
>
> Generated pinvoke call:
>
> [DllImport("meraf_swig_wrapper",
> EntryPoint="CSharp_mCreateDataFile")]
> public static extern int mCreateDataFile(string jarg1, string
> jarg2, int jarg3, HandleRef jarg4);
>
>
> Generated final C# call meant for consumption:
>
> public static int mCreateDataSet(string DSName, string
> Organization, int ClobberFlag, SWIGTYPE_p_int DSIndex) {
>   int ret = Meraf_Swig_WrapperPINVOKE.mCreateDataSet(DSName,
> Organization, ClobberFlag, SWIGTYPE_p_int.getCPtr(DSIndex));
>   return ret;
> }
>
> I am having trouble using the generated custom type
SWIGTYPE_p_int

> class:
>
> public class SWIGTYPE_p_int {
> private HandleRef swigCPtr;
>
> internal SWIGTYPE_p_int(IntPtr cPtr, bool futureUse) {
>   swigCPtr = new HandleRef(this, cPtr);
> }
>
> protected SWIGTYPE_p_int() {
>   swigCPtr = new HandleRef(null, IntPtr.Zero);
> }
>
> internal static HandleRef getCPtr(SWIGTYPE_p_int obj) {
>   return (obj == null) ? new HandleRef(null, IntPtr.Zero) :
> obj.swigCPtr;
> }
> }
>
>
> Here's a sample implmentation:
>
>   static void Main(string[] args)
>   {
>             SWIGTYPE_p_int p;  //what am I supposed to do with
this
> here?  set it to null? new doesn't work.
>
> Meraf_Swig_Wrapper.mCreateDataFile(@"c:\temp\test12.mrf", "ryan", 1,
p);
>         }
>
> How do I properly initialize this type? I get a runtime error of
> unhandled exception of type 'System.TypeInitializationException' when
I
> instantiate SWIGTYPE_p_int and set it to null or set it to new
> SWIGTYPE_p_int().
>
> Also if I need to set it to new somehow then why are all of the
> constructors and method set to internal or protected?  I don't wish to
> make this part of the same assembly as my final code.  Am I supposed
to
> inherit to use the protected constructor  and then use it somehow?
>
>
> Thanks for any help.
>
> Ryan
>
>
>



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: Need help with C# generated code

by wsfulton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

There was no "official" support for arrays in the form of library files
to do the appropriate typemapping until just a few minutes ago. I've
just committed to svn some typemaps that Antti Karanta sent in. These
provide numerous ways to marshal arrays for C#. Take a look, you can see
the new arrays_csharp.i file online if you can't get the latest svn:

http://swig.svn.sourceforge.net/viewvc/swig/trunk/Lib/csharp/arrays_csharp.i

William

David Piepgrass wrote:

> I don't remember whether SWIG has an "official" way to pass arrays. Here
> is what I use:
>
> // Array marshaling for arrays of primitives
> %define %cs_marshal_array(TYPE, CSTYPE)
> %typemap(ctype)  TYPE[] "void*"
> %typemap(imtype,
> inattributes="[MarshalAs(UnmanagedType.LPArray)]") TYPE[] "CSTYPE[]"
> %typemap(cstype) TYPE[] "CSTYPE[]"
> %typemap(in)     TYPE[] %{ $1 = (TYPE*)$input; %}
> %typemap(csin)   TYPE[] "$csinput"
> %enddef
>
> // The following macro invocations allow you to pass arrays of primitive
>
> // types. Arrays of other things such as System.Drawing.Point are also
> // possible.
> %cs_marshal_array(bool, bool)
> %cs_marshal_array(short, short)
> %cs_marshal_array(unsigned short, ushort)
> %cs_marshal_array(int, int)
> %cs_marshal_array(unsigned int, uint)
> %cs_marshal_array(long, int)
> %cs_marshal_array(unsigned long, uint)
> %cs_marshal_array(long long, long)
> %cs_marshal_array(unsigned long long, ulong)
> %cs_marshal_array(float, float)
> %cs_marshal_array(double, double)
>
> but for this to work, the function declaration needs to declare the
> parameter as TYPE[], not TYPE*.  IIRC, if you then use
>
> %apply float[] { float* R_Values }
>
> ...the array typemap will be applied to your "float* R_Values" argument.
>
> I don't know whether it's possible to marshal float[,,]. You should try
> passing float[] first, and if you get that working, then try customizing
> the imtype and cstype typemaps to use float[,,] and see if it works.
>
> P.S. remember, typemaps (etc) must come before the function
> declarations.
>
>> mGetScalarI(int SchemeIndex,int index,long FromX,long ToX,long
> FromY,long
>> ToY,long FromZ,
>>         long ToZ,float* R_Values);
>> -----Original Message-----
>> From: Ryan Koehnen [mailto:ryan.koehnen@...]
>> Sent: Thursday, September 18, 2008 10:32 AM
>> To: David Piepgrass
>> Cc: swig-user@...
>> Subject: Re: [Swig-user] Need help with C# generated code
>>
>> Ok, got it, thanks ;-)
>>
>> The next thing I'm stuck on is defining an array properly when the
> return
>> C type is float*.
>>
>> Example:
>>
>> mGetScalarI(int SchemeIndex,int index,long FromX,long ToX,long
> FromY,long
>> ToY,long FromZ,
>>         long ToZ,float* R_Values);
>>
>> Where return type float* R_Values (in C#) should be float[,,] a 3
>> dimensional array (not an array of arrays).
>>
>> I know I should probably use typemaps somehow, but the examples all
> show
>> for perl,tcl and python so I'm not following them well.
>>
>> A simple example would be awesome.
>>
>> Thanks again,
>>
>> Ryan
>>
>>
>>
>> On Thu, Sep 18, 2008 at 10:15 AM, David Piepgrass
>> <dpiepgrass@...> wrote:
>>
>>
>> SWIGTYPE_p_* represents an opaque handle. It is generated when
> SWIG
>> doesn't know how to represent a type in the target language. In this
> case
>> SWIG doesn't have typemaps for int*, so it uses SWIGTYPE_p_int.
>>
>>
>>
>> The first step to dealing with this problem is to think about
> what
>> the int* argument means. Is it an array? In that case you'll need a
>> typemap for mapping a C# array to a C++ array. Is it a reference or
> output
>> parameter? In that case you'll need a typemap for mapping a C# "ref
> int"
>> or "out int" to a C++ int*. You can see that SWIG can't deal with int*
>> automatically, because it is ambiguous.
>>
>>
>>
>> You can use SWIGTYPE_p_int directly if some other function
> returns
>> an int*. It will come out as SWIGTYPE_p_int in C#. In that case you
> can
>> pass the SWIGTYPE_p_int object to your "mCreateDataFile" function.
>>
>>
>>
>>
>> ________________________________
>>
>>
>> From: swig-user-bounces@... [mailto:swig-user-
>> bounces@...] On Behalf Of Ryan Koehnen
>> Sent: Thursday, September 18, 2008 8:34 AM
>> To: swig-user@...
>> Subject: Re: [Swig-user] Need help with C# generated code
>>
>>
>>
>> Can anybody point me in the right direction on the proper use of
> the
>> generated SWIGTYPE_p classes?  Could really use a clue at the moment.
>>
>> Thanks,
>>
>> Ryan
>>
>> On Wed, Sep 17, 2008 at 5:17 PM, Ryan Koehnen
>> <ryan.koehnen@...> wrote:
>>
>> Hello list members,
>>
>> I have some questions on how to use swig C# generated code.
>> Specifically I have questions about the generated SWIGTYPE_p classes
> and
>> how to use them.  If the answer is obvious, I apologize for my naivety
> on
>> this subject.   Here's the example:
>>
>>
>> Source C Header snippet used as reference in .i file:
>>
>> DECLSPECIFIER int    mCreateDataFile(char* FileName,char*
>> OrgName,int ClobberFlag,int* DFID);
>>
>>
>> Generated C wrapper function:
>>
>> SWIGEXPORT int SWIGSTDCALL CSharp_mCreateDataSet(char * jarg1,
> char
>> * jarg2, int jarg3, void * jarg4) {
>> int jresult ;
>> char *arg1 = (char *) 0 ;
>> char *arg2 = (char *) 0 ;
>> int arg3 ;
>> int *arg4 = (int *) 0 ;
>> int result;
>>
>> arg1 = (char *)jarg1;
>> arg2 = (char *)jarg2;
>> arg3 = (int)jarg3;
>> arg4 = (int *)jarg4;
>> result = (int)mCreateDataSet(arg1,arg2,arg3,arg4);
>> jresult = result;
>> return jresult;
>> }
>>
>>
>> Generated pinvoke call:
>>
>> [DllImport("meraf_swig_wrapper",
>> EntryPoint="CSharp_mCreateDataFile")]
>> public static extern int mCreateDataFile(string jarg1, string
>> jarg2, int jarg3, HandleRef jarg4);
>>
>>
>> Generated final C# call meant for consumption:
>>
>> public static int mCreateDataSet(string DSName, string
>> Organization, int ClobberFlag, SWIGTYPE_p_int DSIndex) {
>>   int ret = Meraf_Swig_WrapperPINVOKE.mCreateDataSet(DSName,
>> Organization, ClobberFlag, SWIGTYPE_p_int.getCPtr(DSIndex));
>>   return ret;
>> }
>>
>> I am having trouble using the generated custom type
> SWIGTYPE_p_int
>> class:
>>
>> public class SWIGTYPE_p_int {
>> private HandleRef swigCPtr;
>>
>> internal SWIGTYPE_p_int(IntPtr cPtr, bool futureUse) {
>>   swigCPtr = new HandleRef(this, cPtr);
>> }
>>
>> protected SWIGTYPE_p_int() {
>>   swigCPtr = new HandleRef(null, IntPtr.Zero);
>> }
>>
>> internal static HandleRef getCPtr(SWIGTYPE_p_int obj) {
>>   return (obj == null) ? new HandleRef(null, IntPtr.Zero) :
>> obj.swigCPtr;
>> }
>> }
>>
>>
>> Here's a sample implmentation:
>>
>>   static void Main(string[] args)
>>   {
>>             SWIGTYPE_p_int p;  //what am I supposed to do with
> this
>> here?  set it to null? new doesn't work.
>>
>> Meraf_Swig_Wrapper.mCreateDataFile(@"c:\temp\test12.mrf", "ryan", 1,
> p);
>>         }
>>
>> How do I properly initialize this type? I get a runtime error of
>> unhandled exception of type 'System.TypeInitializationException' when
> I
>> instantiate SWIGTYPE_p_int and set it to null or set it to new
>> SWIGTYPE_p_int().
>>
>> Also if I need to set it to new somehow then why are all of the
>> constructors and method set to internal or protected?  I don't wish to
>> make this part of the same assembly as my final code.  Am I supposed
> to
>> inherit to use the protected constructor  and then use it somehow?
>>
>>
>> Thanks for any help.
>>
>> Ryan
>>
>>
>>
>
>
>
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
> Build the coolest Linux based applications with Moblin SDK & win great prizes
> Grand prize is a trip for two to an Open Source event anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> Swig-user mailing list
> Swig-user@...
> https://lists.sourceforge.net/lists/listinfo/swig-user
>


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user
LightInTheBox - Buy quality products at wholesale price!