unsigned long to c# Int64

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

unsigned long to c# Int64

Eric Sanford
Hi all
I'm Having problems with how I typemap unsigned long to Int64. It does
not seem to convert correctly for a value of 12 in c++ i get
587409482934386700 in c#.  We want to use Int64 because UInt64,32 and
all are not CLS-compliant.

the function I'm calling:
static size_t getTypeSize(VertexElementType etype);

here is the typemap:
%typemap(ctype) unsigned long, ulong, size_t "unsigned long";
%typemap(imtype) unsigned long, ulong, size_t "Int64";
%typemap(cstype) unsigned long, ulong, size_t "Int64";
%typemap(csin) unsigned long, ulong, size_t "$csinput";
%typemap(csout) unsigned long, ulong, size_t
{
        return (Int64)($imcall);
}
%typemap(csvarout) unsigned long, ulong, size_t
%{
get
{
        return (Int64)($imcall);
}
%}


In the _wrap.cxx (I added the logMessage lines):
SWIGEXPORT unsigned long SWIGSTDCALL
CSharp_VertexElement_getTypeSize(int jarg1) {
    unsigned long jresult ;
    Ogre::VertexElementType arg1 ;
    size_t result;
   
    arg1 = (Ogre::VertexElementType)jarg1;
    result = Ogre::VertexElement::getTypeSize(arg1);

Ogre::LogManager::getSingleton().logMessage( Ogre::String("CSharp_VertexElement_getTypeSize result = ") + Ogre::StringConverter::toString(result) );
    jresult = result;

Ogre::LogManager::getSingleton().logMessage( Ogre::String("CSharp_VertexElement_getTypeSize jresult = ") + Ogre::StringConverter::toString(jresult) );
    return jresult;
}

In VertexElement.cs (I added the logMessage line):
  public static Int64 getTypeSize(VertexElementType etype) {
        LogManager.Instance.logMessage(string.Format("getTypeSize ret={0}" ,
(OgreBindingsPINVOKE.VertexElement_getTypeSize((int)etype)) ));
        return
(Int64)(OgreBindingsPINVOKE.VertexElement_getTypeSize((int)etype));
}

and this is what was written to the log:
CSharp_VertexElement_getTypeSize result = 12
CSharp_VertexElement_getTypeSize jresult = 12
getTypeSize ret=587409482934386700





-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.  Get Certified Today
Register for a JBoss Training Course.  Free Certification Exam
for All Training Attendees Through End of 2005. For more info visit:
http://ads.osdn.com/?ad_id=7628&alloc_id=16845&op=click
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: unsigned long to c# Int64

William S Fulton
Eric Sanford wrote:

> Hi all
> I'm Having problems with how I typemap unsigned long to Int64. It does
> not seem to convert correctly for a value of 12 in c++ i get
> 587409482934386700 in c#.  We want to use Int64 because UInt64,32 and
> all are not CLS-compliant.
>
> the function I'm calling:
> static size_t getTypeSize(VertexElementType etype);
>
> here is the typemap:
> %typemap(ctype) unsigned long, ulong, size_t "unsigned long";
> %typemap(imtype) unsigned long, ulong, size_t "Int64";
> %typemap(cstype) unsigned long, ulong, size_t "Int64";
> %typemap(csin) unsigned long, ulong, size_t "$csinput";
> %typemap(csout) unsigned long, ulong, size_t
> {
> return (Int64)($imcall);
> }
> %typemap(csvarout) unsigned long, ulong, size_t
> %{
> get
> {
> return (Int64)($imcall);
> }
> %}
>

You are marshalling a 32 bit unsigned number (unsigned long in the ctype
typemap) on the unmanaged side to a 64 bit signed number on the managed
side (Int64 in the imtype typemap). If you match this up correctly it
will work, eg change the ctype typemap to use 'long long' or '__int64'.

What do you mean by UInt64 etc are not CLS compliant? They are defined
in the ISO C# spec.

William


-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.  Get Certified Today
Register for a JBoss Training Course.  Free Certification Exam
for All Training Attendees Through End of 2005. For more info visit:
http://ads.osdn.com/?ad_id=7628&alloc_id=16845&op=click
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: unsigned long to c# Int64

Eric Sanford
On Sun, 2005-11-20 at 21:42 +0000, William S Fulton wrote:

> Eric Sanford wrote:
> > Hi all
> > I'm Having problems with how I typemap unsigned long to Int64. It does
> > not seem to convert correctly for a value of 12 in c++ i get
> > 587409482934386700 in c#.  We want to use Int64 because UInt64,32 and
> > all are not CLS-compliant.
> >
> > the function I'm calling:
> > static size_t getTypeSize(VertexElementType etype);
> >
> > here is the typemap:
> > %typemap(ctype) unsigned long, ulong, size_t "unsigned long";
> > %typemap(imtype) unsigned long, ulong, size_t "Int64";
> > %typemap(cstype) unsigned long, ulong, size_t "Int64";
> > %typemap(csin) unsigned long, ulong, size_t "$csinput";
> > %typemap(csout) unsigned long, ulong, size_t
> > {
> > return (Int64)($imcall);
> > }
> > %typemap(csvarout) unsigned long, ulong, size_t
> > %{
> > get
> > {
> > return (Int64)($imcall);
> > }
> > %}
> >
>
> You are marshalling a 32 bit unsigned number (unsigned long in the ctype
> typemap) on the unmanaged side to a 64 bit signed number on the managed
> side (Int64 in the imtype typemap). If you match this up correctly it
> will work, eg change the ctype typemap to use 'long long' or '__int64'.
>
> What do you mean by UInt64 etc are not CLS compliant? They are defined
> in the ISO C# spec.
>
> William
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by the JBoss Inc.  Get Certified Today
> Register for a JBoss Training Course.  Free Certification Exam
> for All Training Attendees Through End of 2005. For more info visit:
> http://ads.osdn.com/?ad_id=7628&alloc_id=16845&op=click
> _______________________________________________
> Swig-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/swig-user


MS says that Uints are not CLS-compliant.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemUInt32ClassTopic.asp
But I'd just as soon use them. Others in the project are using vb.net
maybe they are having problems, Ill talk to them again.

I was able to get it to return cpp unsigned long as c# Int64 with:
%typemap(csout) unsigned long, ulong, size_t
{
        return (Int64)((UInt32)($imcall));
}
%typemap(csvarout) unsigned long, ulong, size_t
%{
get
{
        return (Int64)((UInt32)($imcall));
}
%}

now I need to correct in. I tried This line below but its not working.
%typemap(csin) unsigned long, ulong, size_t "(UInt32)$csinput";

in the converting from c# to cpp there's something weird. the first argument is ok but the second is zero and the third is what the second should be.

the cpp function i call is defined.
virtual HardwareVertexBufferSharedPtr createVertexBuffer(size_t vertexSize, size_t numVerts, HardwareBuffer::Usage usage, bool useShadowBuffer = false);


my call to the function (vertexsize is an Int64 ):
 HardwareVertexBufferSharedPtr vbuf = HardwareBufferManager.getSingleton().createVertexBuffer( vertexsize, 12, HardwareBuffer.Usage.HBU_STATIC_WRITE_ONLY);


in the HardwareBufferManager.cs (i added the logMessage and it outputs correctly).
  public virtual HardwareVertexBufferSharedPtr createVertexBuffer(Int64 vertexSize, Int64 numVerts, HardwareBuffer.Usage usage) {
        LogManager.Instance.logMessage(string.Format("HardwareBufferManager.createVertexBuffer arg1={0}, arg2={1}, arg3={2}",
                 vertexSize, numVerts, (int)usage ));
    HardwareVertexBufferSharedPtr ret = new HardwareVertexBufferSharedPtr(OgreBindingsPINVOKE.HardwareBufferManager_createVertexBuffer__SWIG_1(swigCPtr, (UInt32)vertexSize, (UInt32)numVerts, (int)usage), true);
    return ret;
  }

in _wrap.cxx:
SWIGEXPORT void * SWIGSTDCALL CSharp_HardwareBufferManager_createVertexBuffer__SWIG_1(void * jarg1, unsigned long jarg2, unsigned long jarg3, int jarg4) {
    void * jresult ;
    Ogre::HardwareBufferManager *arg1 = (Ogre::HardwareBufferManager *) 0 ;
    size_t arg2 ;
    size_t arg3 ;
    Ogre::HardwareBuffer::Usage arg4 ;
    HardwareVertexBufferSharedPtr result;
   
    arg1 = (Ogre::HardwareBufferManager *)jarg1;
    arg2 = (size_t)jarg2;
    arg3 = (size_t)jarg3;
    arg4 = (Ogre::HardwareBuffer::Usage)jarg4;

        Ogre::LogManager::getSingleton().logMessage( Ogre::String("CSharp_HardwareBufferManager_createVertexBuffer__SWIG_1")
         + Ogre::String(" arg1=") + Ogre::StringConverter::toString(jarg2)
         + Ogre::String(" arg2=") + Ogre::StringConverter::toString(jarg3)
         + Ogre::String(" arg3=") + Ogre::StringConverter::toString(jarg4)
         );

    result = (arg1)->createVertexBuffer(arg2,arg3,arg4);
   
    jresult = new HardwareVertexBufferSharedPtr((HardwareVertexBufferSharedPtr &)result);
    return jresult;
}




but here in the _wrap.cxx the arguments get mestup.
where jarg2 is vertexsize value 32 i pass as an Int64,
 jarg3 should be 12 but comes across as 0,
 and jarg4 should be 5 the HBU_STATIC_WRITE_ONLY but is 12.
its as if too much was pushed on the stack. unsigned long is a 32bit value? I changed the .cs file to pass as Int64 but still get the same results.






-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.  Get Certified Today
Register for a JBoss Training Course.  Free Certification Exam
for All Training Attendees Through End of 2005. For more info visit:
http://ads.osdn.com/?ad_id=7628&alloc_id=16845&op=click
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: unsigned long to c# Int64

William S Fulton
Eric Sanford wrote:

> On Sun, 2005-11-20 at 21:42 +0000, William S Fulton wrote:
>>Eric Sanford wrote:
>>>Hi all
>>>I'm Having problems with how I typemap unsigned long to Int64. It does
>>>not seem to convert correctly for a value of 12 in c++ i get
>>>587409482934386700 in c#.  We want to use Int64 because UInt64,32 and
>>>all are not CLS-compliant.
>>>
>>>the function I'm calling:
>>>static size_t getTypeSize(VertexElementType etype);
>>>
>>>here is the typemap:
>>>%typemap(ctype) unsigned long, ulong, size_t "unsigned long";
>>>%typemap(imtype) unsigned long, ulong, size_t "Int64";
>>>%typemap(cstype) unsigned long, ulong, size_t "Int64";
>>>%typemap(csin) unsigned long, ulong, size_t "$csinput";
>>>%typemap(csout) unsigned long, ulong, size_t
>>>{
>>> return (Int64)($imcall);
>>>}
>>>%typemap(csvarout) unsigned long, ulong, size_t
>>>%{
>>>get
>>>{
>>> return (Int64)($imcall);
>>>}
>>>%}
>>>
>>You are marshalling a 32 bit unsigned number (unsigned long in the ctype
>>typemap) on the unmanaged side to a 64 bit signed number on the managed
>>side (Int64 in the imtype typemap). If you match this up correctly it
>>will work, eg change the ctype typemap to use 'long long' or '__int64'.
>>
>>What do you mean by UInt64 etc are not CLS compliant? They are defined
>>in the ISO C# spec.
>>
>>William
>>
>>
>
> MS says that Uints are not CLS-compliant.
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemUInt32ClassTopic.asp
> But I'd just as soon use them. Others in the project are using vb.net
> maybe they are having problems, Ill talk to them again.
>
You can use UInt32 from VB, it just is not a native VB type.

> I was able to get it to return cpp unsigned long as c# Int64 with:
> %typemap(csout) unsigned long, ulong, size_t
> {
> return (Int64)((UInt32)($imcall));
> }
> %typemap(csvarout) unsigned long, ulong, size_t
> %{
> get
> {
> return (Int64)((UInt32)($imcall));
> }
> %}
>
> now I need to correct in. I tried This line below but its not working.
> %typemap(csin) unsigned long, ulong, size_t "(UInt32)$csinput";
>
I gave you the answer before and you are still marshalling a 64 bit
number to a 32 bit number across the PInvoke layer, then trying to hack
around it with casts. Don't do it!

William



-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.  Get Certified Today
Register for a JBoss Training Course.  Free Certification Exam
for All Training Attendees Through End of 2005. For more info visit:
http://ads.osdn.com/?ad_id=7628&alloc_id=16845&op=click
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user