URGENT: Unix/Windows Struct cast problem

  • Thread starter Thread starter cody
  • Start date Start date
C

cody

Would be great if you could provide the code how you are receiving the
packets in C# so we can see how to help you.
 
Follwing the struct:
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

public struct TpSomeMsgRep

{

public uint SomeId;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]

public Byte[] notinuse1;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=674)]

public Byte[] theMainPart;

}

This used to cast from buffer, recieved by TCP from C++ Program, like this:

oMsgRes= (mm.TpSomeMsgRep )SomeBuffer;

This work fine with Unix server. once transfer to Windows I can not cast to
the structure.

PLEASE HELP HELP HELP!
 
The code is looking like regular Async TCP Client

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


cody said:
Would be great if you could provide the code how you are receiving the
packets in C# so we can see how to help you.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Tamir Khason said:
Follwing the struct:
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

public struct TpSomeMsgRep

{

public uint SomeId;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]

public Byte[] notinuse1;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=674)]

public Byte[] theMainPart;

}

This used to cast from buffer, recieved by TCP from C++ Program, like this:

oMsgRes= (mm.TpSomeMsgRep )SomeBuffer;

This work fine with Unix server. once transfer to Windows I can not cast to
the structure.

PLEASE HELP HELP HELP!
 
Hi,

Tamir Khason said:
Follwing the struct:
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

public struct TpSomeMsgRep

{

public uint SomeId;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]

public Byte[] notinuse1;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=674)]

public Byte[] theMainPart;

}

This used to cast from buffer, recieved by TCP from C++ Program, like this:

oMsgRes= (mm.TpSomeMsgRep )SomeBuffer;

This work fine with Unix server. once transfer to Windows I can not cast to
the structure.

What and how are you casting, if you want to cast a byte array to your
struct, you must use marshalling functionality:

byte [] buf = * received * ;
GCHandle h = GCHandle.Alloc( buf, GCHandleType.Pinned );
TpSomeMsgRep rep = (TpSomeMsgRep)Marshal.PtrToStruct(h.AddrOfPinnedObject(),
typeof(TpSomeMsgRep));
h.Free();
// use rep


If you need something else, be more specific.

HTH,
greetings
 
I'm doing this.
The question is what can be different between the packets between Unix and
Windows packets...

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


BMermuys said:
Hi,

Tamir Khason said:
Follwing the struct:
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

public struct TpSomeMsgRep

{

public uint SomeId;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]

public Byte[] notinuse1;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=674)]

public Byte[] theMainPart;

}

This used to cast from buffer, recieved by TCP from C++ Program, like this:

oMsgRes= (mm.TpSomeMsgRep )SomeBuffer;

This work fine with Unix server. once transfer to Windows I can not cast to
the structure.

What and how are you casting, if you want to cast a byte array to your
struct, you must use marshalling functionality:

byte [] buf = * received * ;
GCHandle h = GCHandle.Alloc( buf, GCHandleType.Pinned );
TpSomeMsgRep rep = (TpSomeMsgRep)Marshal.PtrToStruct(h.AddrOfPinnedObject(),
typeof(TpSomeMsgRep));
h.Free();
// use rep


If you need something else, be more specific.

HTH,
greetings
PLEASE HELP HELP HELP!
 
Tamir Khason said:
I'm doing this.
The question is what can be different between the packets between Unix and
Windows packets...

You're are still not very clear. Is this server written in c++ compiled for
both linux/windows?

I don't think struct differ from one platform to another. Struct alignment
may cause different struct layout's but that is compiler dependent not
platform.

What's exactly is the problem you have ? Does PtrToStruct throw ?
Unexpected data ??

greetings

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


BMermuys said:
Hi,

Tamir Khason said:
Follwing the struct:
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

public struct TpSomeMsgRep

{

public uint SomeId;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]

public Byte[] notinuse1;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=674)]

public Byte[] theMainPart;

}

This used to cast from buffer, recieved by TCP from C++ Program, like this:

oMsgRes= (mm.TpSomeMsgRep )SomeBuffer;

This work fine with Unix server. once transfer to Windows I can not
cast
to
the structure.

What and how are you casting, if you want to cast a byte array to your
struct, you must use marshalling functionality:

byte [] buf = * received * ;
GCHandle h = GCHandle.Alloc( buf, GCHandleType.Pinned );
TpSomeMsgRep rep = (TpSomeMsgRep)Marshal.PtrToStruct(h.AddrOfPinnedObject(),
typeof(TpSomeMsgRep));
h.Free();
// use rep


If you need something else, be more specific.

HTH,
greetings
PLEASE HELP HELP HELP!
 
Struct alignment
may cause different struct layout's but that is compiler dependent not
platform.
Yes, both servers sources were compiled with alignment 4 both for unix (sun)
and windows
What's exactly is the problem you have ? Does PtrToStruct throw ?
Unexpected data ??
No, PtrToStruct works well, but in cast with
(MyStruct)ObjectSerializedFromBinaryStream failed.




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


BMermuys said:
Tamir Khason said:
I'm doing this.
The question is what can be different between the packets between Unix and
Windows packets...

You're are still not very clear. Is this server written in c++ compiled for
both linux/windows?

I don't think struct differ from one platform to another. Struct alignment
may cause different struct layout's but that is compiler dependent not
platform.

What's exactly is the problem you have ? Does PtrToStruct throw ?
Unexpected data ??

greetings

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


BMermuys said:
Hi,

Follwing the struct:
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

public struct TpSomeMsgRep

{

public uint SomeId;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]

public Byte[] notinuse1;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=674)]

public Byte[] theMainPart;

}

This used to cast from buffer, recieved by TCP from C++ Program, like
this:

oMsgRes= (mm.TpSomeMsgRep )SomeBuffer;

This work fine with Unix server. once transfer to Windows I can not cast
to
the structure.

What and how are you casting, if you want to cast a byte array to your
struct, you must use marshalling functionality:

byte [] buf = * received * ;
GCHandle h = GCHandle.Alloc( buf, GCHandleType.Pinned );
TpSomeMsgRep rep = (TpSomeMsgRep)Marshal.PtrToStruct(h.AddrOfPinnedObject(),
typeof(TpSomeMsgRep));
h.Free();
// use rep


If you need something else, be more specific.

HTH,
greetings


PLEASE HELP HELP HELP!
 
What is ObjectSerializedFromBinaryStream? It seems not to be part of the
Framework.
If you want that we help you you have to provive more information for
example show your code that receives the packets.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Tamir Khason said:
Struct alignment
may cause different struct layout's but that is compiler dependent not
platform.
Yes, both servers sources were compiled with alignment 4 both for unix (sun)
and windows
What's exactly is the problem you have ? Does PtrToStruct throw ?
Unexpected data ??
No, PtrToStruct works well, but in cast with
(MyStruct)ObjectSerializedFromBinaryStream failed.




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


BMermuys said:
Tamir Khason said:
I'm doing this.
The question is what can be different between the packets between Unix and
Windows packets...

You're are still not very clear. Is this server written in c++ compiled for
both linux/windows?

I don't think struct differ from one platform to another. Struct alignment
may cause different struct layout's but that is compiler dependent not
platform.

What's exactly is the problem you have ? Does PtrToStruct throw ?
Unexpected data ??

greetings

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


Hi,

Follwing the struct:
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

public struct TpSomeMsgRep

{

public uint SomeId;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]

public Byte[] notinuse1;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=674)]

public Byte[] theMainPart;

}

This used to cast from buffer, recieved by TCP from C++ Program, like
this:

oMsgRes= (mm.TpSomeMsgRep )SomeBuffer;

This work fine with Unix server. once transfer to Windows I can
not
cast
to
the structure.

What and how are you casting, if you want to cast a byte array to your
struct, you must use marshalling functionality:

byte [] buf = * received * ;
GCHandle h = GCHandle.Alloc( buf, GCHandleType.Pinned );
TpSomeMsgRep rep =
(TpSomeMsgRep)Marshal.PtrToStruct(h.AddrOfPinnedObject(),
typeof(TpSomeMsgRep));
h.Free();
// use rep


If you need something else, be more specific.

HTH,
greetings


PLEASE HELP HELP HELP!
 
This just name of variable to explain where ti tooked from :)

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....
advice

cody said:
What is ObjectSerializedFromBinaryStream? It seems not to be part of the
Framework.
If you want that we help you you have to provive more information for
example show your code that receives the packets.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Tamir Khason said:
Struct alignment
may cause different struct layout's but that is compiler dependent not
platform.
Yes, both servers sources were compiled with alignment 4 both for unix (sun)
and windows
What's exactly is the problem you have ? Does PtrToStruct throw ?
Unexpected data ??
No, PtrToStruct works well, but in cast with
(MyStruct)ObjectSerializedFromBinaryStream failed.




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


BMermuys said:
I'm doing this.
The question is what can be different between the packets between
Unix
and
Windows packets...

You're are still not very clear. Is this server written in c++
compiled
for
both linux/windows?

I don't think struct differ from one platform to another. Struct alignment
may cause different struct layout's but that is compiler dependent not
platform.

What's exactly is the problem you have ? Does PtrToStruct throw ?
Unexpected data ??

greetings



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


Hi,

Follwing the struct:
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

public struct TpSomeMsgRep

{

public uint SomeId;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]

public Byte[] notinuse1;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=674)]

public Byte[] theMainPart;

}

This used to cast from buffer, recieved by TCP from C++ Program, like
this:

oMsgRes= (mm.TpSomeMsgRep )SomeBuffer;

This work fine with Unix server. once transfer to Windows I can not
cast
to
the structure.

What and how are you casting, if you want to cast a byte array to your
struct, you must use marshalling functionality:

byte [] buf = * received * ;
GCHandle h = GCHandle.Alloc( buf, GCHandleType.Pinned );
TpSomeMsgRep rep =
(TpSomeMsgRep)Marshal.PtrToStruct(h.AddrOfPinnedObject(),
typeof(TpSomeMsgRep));
h.Free();
// use rep


If you need something else, be more specific.

HTH,
greetings


PLEASE HELP HELP HELP!
 
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.
 
Hi,

Tamir Khason said:
Struct alignment
Yes, both servers sources were compiled with alignment 4 both for unix (sun)
and windows

Then why are you using Pack=1 on the struct ?

Greetings

What's exactly is the problem you have ? Does PtrToStruct throw ?
Unexpected data ??
No, PtrToStruct works well, but in cast with
(MyStruct)ObjectSerializedFromBinaryStream failed.




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


BMermuys said:
Tamir Khason said:
I'm doing this.
The question is what can be different between the packets between Unix and
Windows packets...

You're are still not very clear. Is this server written in c++ compiled for
both linux/windows?

I don't think struct differ from one platform to another. Struct alignment
may cause different struct layout's but that is compiler dependent not
platform.

What's exactly is the problem you have ? Does PtrToStruct throw ?
Unexpected data ??

greetings

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


Hi,

Follwing the struct:
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

public struct TpSomeMsgRep

{

public uint SomeId;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]

public Byte[] notinuse1;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=674)]

public Byte[] theMainPart;

}

This used to cast from buffer, recieved by TCP from C++ Program, like
this:

oMsgRes= (mm.TpSomeMsgRep )SomeBuffer;

This work fine with Unix server. once transfer to Windows I can
not
cast
to
the structure.

What and how are you casting, if you want to cast a byte array to your
struct, you must use marshalling functionality:

byte [] buf = * received * ;
GCHandle h = GCHandle.Alloc( buf, GCHandleType.Pinned );
TpSomeMsgRep rep =
(TpSomeMsgRep)Marshal.PtrToStruct(h.AddrOfPinnedObject(),
typeof(TpSomeMsgRep));
h.Free();
// use rep


If you need something else, be more specific.

HTH,
greetings


PLEASE HELP HELP HELP!
 
You should use the same pack that was used to generate the original struct.
If you are not sure, compare their sizes (your struct and the receiced
packet).
If it is the same your pack size is correct.

if the cast from ObjectSerializedFromBinaryStream failed try to display the
type of the generated object:

Console.WriteLine(ObjectSerializedFromBinaryStream().GetType());

maybe that can give you the idea what got wrong.

However if you would post the piece of code you are using for receiving the
packets you have much bigger chances somebody here can solve your problem.
 
Hi,

Tamir Khason said:
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
 
So what I have to use?

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

BMermuys said:
Hi,

Tamir Khason said:
Struct alignment
Yes, both servers sources were compiled with alignment 4 both for unix (sun)
and windows

Then why are you using Pack=1 on the struct ?

Greetings

What's exactly is the problem you have ? Does PtrToStruct throw ?
Unexpected data ??
No, PtrToStruct works well, but in cast with
(MyStruct)ObjectSerializedFromBinaryStream failed.




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


BMermuys said:
I'm doing this.
The question is what can be different between the packets between
Unix
and
Windows packets...

You're are still not very clear. Is this server written in c++
compiled
for
both linux/windows?

I don't think struct differ from one platform to another. Struct alignment
may cause different struct layout's but that is compiler dependent not
platform.

What's exactly is the problem you have ? Does PtrToStruct throw ?
Unexpected data ??

greetings



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


Hi,

Follwing the struct:
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

public struct TpSomeMsgRep

{

public uint SomeId;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]

public Byte[] notinuse1;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=674)]

public Byte[] theMainPart;

}

This used to cast from buffer, recieved by TCP from C++ Program, like
this:

oMsgRes= (mm.TpSomeMsgRep )SomeBuffer;

This work fine with Unix server. once transfer to Windows I can not
cast
to
the structure.

What and how are you casting, if you want to cast a byte array to your
struct, you must use marshalling functionality:

byte [] buf = * received * ;
GCHandle h = GCHandle.Alloc( buf, GCHandleType.Pinned );
TpSomeMsgRep rep =
(TpSomeMsgRep)Marshal.PtrToStruct(h.AddrOfPinnedObject(),
typeof(TpSomeMsgRep));
h.Free();
// use rep


If you need something else, be more specific.

HTH,
greetings


PLEASE HELP HELP HELP!
 
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
 
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...
 
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
 
I have the same code, the only thing changed is enviroment of server (from
Sun to Windows)
I compared the packets - they are the same
the type I recieved is Object
I can post the code of reciever, but it just async C# TCP server. (bit long
for newsgroup)
 
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

--
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
but
explanation
I classes
with TCP
 
Back
Top