URGENT: Unix/Windows Struct cast problem

  • Thread starter Thread starter cody
  • Start date Start date
Hi,

Tamir Khason said:
Simple Casting. This server using its own protocol over TCP to construct
structures and I'm on other side have the same structures to recieve

If the server is run on Sun and the client can receive it with Pack=1, then
I suspect that structure alignment for the Sun compilation is 1 byte, not 4.

Dunno if it will help, but I don't think it's much work to see if Pack=4 for
the client helps when the server runs on Windows.


Greetings
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "


BMermuys said:
Hi,

Tamir Khason said:
Thank you for response.
1) The source server was Sun (not Linux)
2) no, the client is C# and I'm cast Object (created from Byte[]) to
struct

Yes, but how is the server (c++) sending this struct ?
Server-side there should be a struct to byte conversion, how is it done ?
Simple casting or is each field send seperatly ?



Greetings

3) No problem, it works with Sun server, but I think does not with Windows
server

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Hi,

The problem is WHAT I'M passing in.
I revieve the stream from C++ server.
When it was in UNIX enviroment - everything worked ok, but once they
treanfer to Windows the problem begun. There are same structures,
but
as
far
as I understand , not the same stream - thet's the single
explanation
I
can
think about...

So, if you only see this problem when the server is on Windows and
not
on
Linux, you can agree that the problem is with the server and not the
client.

Can't you post some code, how do you transmit this struct in c++, do you
cast the struct* to a byte* ?

I still don't quite understand what the problem is you're having if
PtrToStruct works.

HTH,
greetings



--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Anyway there is no way to provide full source it a lot of classes
with
relations etc.
The point is that there are diferences between unix and
windows
TCP
or
binary stream....


I do not believe that. The bytes can be read from the stream the same
way
you put them in.
The marshaller will take care that the byte order (big endian/little
endian)
in your struct's variables will be ok.

Remember that you have to pass the correct type to PtrToStruct,
otherwise
itt will output the wrong type.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
 
You have a field of type uint in your struct. Maybe it is 32bit on windows
but it is 64 on sun, that could be the problem.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Tamir Khason said:
Thank you for response.
1) The source server was Sun (not Linux)
2) no, the client is C# and I'm cast Object (created from Byte[]) to struct
3) No problem, it works with Sun server, but I think does not with Windows
server

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

BMermuys said:
Hi,

as
far I
can

So, if you only see this problem when the server is on Windows and not on
Linux, you can agree that the problem is with the server and not the client.

Can't you post some code, how do you transmit this struct in c++, do you
cast the struct* to a byte* ?

I still don't quite understand what the problem is you're having if
PtrToStruct works.

HTH,
greetings
TCP
or same
way
 
cody said:
You have a field of type uint in your struct. Maybe it is 32bit on windows
but it is 64 on sun, that could be the problem.

And don't Suns have a different endianess to x86?
--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Tamir Khason said:
Thank you for response.
1) The source server was Sun (not Linux)
2) no, the client is C# and I'm cast Object (created from Byte[]) to struct
3) No problem, it works with Sun server, but I think does not with Windows
server

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

BMermuys said:
Hi,

The problem is WHAT I'M passing in.
I revieve the stream from C++ server.
When it was in UNIX enviroment - everything worked ok, but once they
treanfer to Windows the problem begun. There are same structures,
but
explanation
I classes
with TCP
 
On Mon, 26 Jul 2004 16:28:08 +0200, "Tamir Khason"

<urgent plea deleted>

You, and everyone else, can drop the Urgent Urgent Urgent crap
from your posts.

Lack of planning on your part does not constititute an emergency on
our part.

Oz
 
You have a field of type uint in your struct. Maybe it is 32bit on
windows
And don't Suns have a different endianess to x86?

Yes, Sun has motorola byte order. Thinking about it a while I do not believe
that the marshaller converts the byteorder automatically because it doesn't
know the byteorder which the packet has. It would be great if I could tell
the marshaller to convert is automatically.
But I do not believe this has something to do which the error the OP is
getting.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Stu Smith said:
--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Tamir Khason said:
Thank you for response.
1) The source server was Sun (not Linux)
2) no, the client is C# and I'm cast Object (created from Byte[]) to struct
3) No problem, it works with Sun server, but I think does not with Windows
server

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Hi,

The problem is WHAT I'M passing in.
I revieve the stream from C++ server.
When it was in UNIX enviroment - everything worked ok, but once they
treanfer to Windows the problem begun. There are same structures,
but
as
far
as I understand , not the same stream - thet's the single
explanation
I
can
think about...

So, if you only see this problem when the server is on Windows and
not
on
Linux, you can agree that the problem is with the server and not the
client.

Can't you post some code, how do you transmit this struct in c++, do you
cast the struct* to a byte* ?

I still don't quite understand what the problem is you're having if
PtrToStruct works.

HTH,
greetings



--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Anyway there is no way to provide full source it a lot of classes
with
relations etc.
The point is that there are diferences between unix and
windows
TCP
or
binary stream....


I do not believe that. The bytes can be read from the stream the same
way
you put them in.
The marshaller will take care that the byte order (big endian/little
endian)
in your struct's variables will be ok.

Remember that you have to pass the correct type to PtrToStruct,
otherwise
itt will output the wrong type.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
 
Following the code
public static object RawDeserializeEx(ref byte[] rawdatas, Type anytype )
{
try
{
int rawsize = Marshal.SizeOf( anytype ) + 2;//Dont's Ask me why-This is
the structure

if( rawsize > rawdatas.Length )
return null;

GCHandle handle = GCHandle.Alloc( rawdatas, GCHandleType.Pinned );
IntPtr buffer = handle.AddrOfPinnedObject();
object retobj = Marshal.PtrToStructure( (IntPtr)(((int)buffer + 2)),
anytype ); //Dont's Ask me why-This is the structure
buffer = IntPtr.Zero;
handle.Free();
return retobj;
}
catch(Exception e)
{
ex.Handle(e);
return null;
}
}
 
This is realy Urgent issues. I'm use this newsgoup a lot both as "asker" and
"answerer". I'm usualy do not use URGENT tag, except REALLY URGENT ISSUES
Check me... :)

BTW, nice sentence (Lack of planning on your part does not constititute an
emergency on our part.), I'll use it
 
instead of adding always 2 to the number of bytes in your method you should
declare the
field "public uint SomeId;" as "ulong" instead because the sun machine
certainly has 64 bit integers.

just a few comments:
public static object RawDeserializeEx(ref byte[] rawdatas, Type anytype )

"ref" is unnessecary because you don't modify the reference "rawdatas", just
its content.
buffer = IntPtr.Zero;

unnessecary it will automatically be freed when it goes out of scope.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Tamir Khason said:
Following the code
public static object RawDeserializeEx(ref byte[] rawdatas, Type anytype )
{
try
{
int rawsize = Marshal.SizeOf( anytype ) + 2;//Dont's Ask me why-This is
the structure

if( rawsize > rawdatas.Length )
return null;

GCHandle handle = GCHandle.Alloc( rawdatas, GCHandleType.Pinned );
IntPtr buffer = handle.AddrOfPinnedObject();
object retobj = Marshal.PtrToStructure( (IntPtr)(((int)buffer + 2)),
anytype ); //Dont's Ask me why-This is the structure
buffer = IntPtr.Zero;
handle.Free();
return retobj;
}
catch(Exception e)
{
ex.Handle(e);
return null;
}
}


--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Willy Denoyette said:
Not sure what you mean with:

Threfore, Just the piece of code that receives the packets and re-assemble
them into the struct could be enough.

Willy.
 
TNX for reply. I'll try it

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

cody said:
instead of adding always 2 to the number of bytes in your method you should
declare the
field "public uint SomeId;" as "ulong" instead because the sun machine
certainly has 64 bit integers.

just a few comments:
public static object RawDeserializeEx(ref byte[] rawdatas, Type
anytype )

"ref" is unnessecary because you don't modify the reference "rawdatas", just
its content.
buffer = IntPtr.Zero;

unnessecary it will automatically be freed when it goes out of scope.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Tamir Khason said:
Following the code
public static object RawDeserializeEx(ref byte[] rawdatas, Type anytype )
{
try
{
int rawsize = Marshal.SizeOf( anytype ) + 2;//Dont's Ask me why-This is
the structure

if( rawsize > rawdatas.Length )
return null;

GCHandle handle = GCHandle.Alloc( rawdatas, GCHandleType.Pinned );
IntPtr buffer = handle.AddrOfPinnedObject();
object retobj = Marshal.PtrToStructure( (IntPtr)(((int)buffer + 2)),
anytype ); //Dont's Ask me why-This is the structure
buffer = IntPtr.Zero;
handle.Free();
return retobj;
}
catch(Exception e)
{
ex.Handle(e);
return null;
}
}


--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Willy Denoyette said:
Not sure what you mean with:
the type I recieved is Object

Threfore, Just the piece of code that receives the packets and re-assemble
them into the struct could be enough.

Willy.
 
Back
Top