Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 156945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:24:57+00:00 2026-05-11T10:24:57+00:00

I have a TCP Client,which puts a packet in a structure using System.Runtime.InteropServices; [StructLayoutAttribute(LayoutKind.Sequential)]

  • 0

I have a TCP Client,which puts a packet in a structure

using System.Runtime.InteropServices;  [StructLayoutAttribute(LayoutKind.Sequential)] public struct tPacket_5000_E {     public Int16 size;     public Int16 opcode;     public byte securityCount;     public byte securityCRC;     public byte flag;     [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 8, ArraySubType = UnmanagedType.I1)]     public byte[] blowfish;     public UInt32 seedCount;     public UInt32 seedCRC;     [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 5, ArraySubType = UnmanagedType.I1)]     public UInt32[] seedsecurity; } 

The code I use to put the packet in the structure is:

tPacket_5000_E packet = new tPacket_5000_E(); GCHandle pin = GCHandle.Alloc(data, GCHandleType.Pinned); packet = (tPacket_5000_E)Marshal.PtrToStructure(pin.AddrOfPinnedObject(), typeof(tPacket_5000_E)); pin.Free(); 

Now,before i continue I must tell you that I’m translating this project from C++ to C#.

This is the problem:

The last 3 members of tPacket_5000_E are Int32(i tried UInt32 too),which is DWORD in C++. The values before those three members,which are NOT Int32,are equal to those in C++.(I inject same packet in both C++ and C# project).

However,those three members have different values.

in C++ the values are(correct):

  1. seedCount:0x00000079
  2. seedCRC:0x000000d1
  3. SeedSecurity:
  4. -[0]:0x548ac099
  5. –1:0x03c4d378
  6. -[2]:0x292e9eab
  7. -[3]:0x4eee5ee3
  8. -[4]:0x1071206e

in C# the values are(incorrect):

  1. seedCount:0xd1000000
  2. seedCRC:0x99000000
  3. SeedSecurity:
  4. -[0]: 0x78548ac0
  5. –1: 0xab03c4d3
  6. -[2]: 0xe3292e9e
  7. -[3]: 0x6e4eee5e
  8. -[4]: 0x00107120

The packet in both applications is equal

byte[] data = new byte[] { 0x25, 0x00, 0x00, 0x50, 0x00, 0x00, 0x0E, 0x10,  0xCE, 0xEF, 0x47, 0xDA, 0xC3, 0xFE, 0xFF, 0x79,  0x00, 0x00, 0x00, 0xD1, 0x00, 0x00, 0x00, 0x99,  0xC0, 0x8A, 0x54, 0x78, 0xD3, 0xC4, 0x03, 0xAB,  0x9E, 0x2E, 0x29, 0xE3, 0x5E, 0xEE, 0x4E, 0x6E,  0x20, 0x71, 0x10}; 

Click here for further information

Why the last three members in the struct are different and how to fix them?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:24:58+00:00Added an answer on May 11, 2026 at 10:24 am

    I’d expect that the root of your problem is that the three byte values

    public byte securityCount; public byte securityCRC; public byte flag; 

    cause the next 32-bit values not to be word-aligned, and your two sets of code are adding (or not adding) internal padding differently.

    I expect that the different packings look something like this:

     C++                                   C# ================================      ================================ [size          ][opcode        ]      [size          ][opcode        ] [secCnt][secCrc][flag  ][blow0 ]      [secCnt][secCrc][flag  ][blow0 ] [blow1 ][blow2 ][blow3 ][blow4 ]      [blow1 ][blow2 ][blow3 ][blow4 ] [blow5 ][blow6 ][blow7 ][seedCou      [blow5 ][blow6 ][blow7 ]..PAD... nt                     ][seedCRC      [seedCount                     ]                        ][seedSec      [seedCRC                       ] urity0                 ][seedSec      [seedSecurity0                 ] urity1                 ][seedSec      [seedSecurity1                 ] urity2                 ][seedSec      [seedSecurity2                 ] urity3                 ][seedSec      [seedSecurity3                 ] urity4                 ]              [seedSecurity4                 ] 

    … with C# inserting a byte of padding which causes later values to be one byte off.

    You can try using

    [StructLayout(LayoutKind.Sequential,Pack=1)] 

    before your struct definition, which should use the minimum amount of space possible.

    Mastering Structs in C# has some good information on how/why this happens.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 78k
  • Answers 78k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Oops, I definitely found my problem. My client is calling… May 11, 2026 at 3:49 pm
  • added an answer Yes you can play signed urls in Flash no problem...… May 11, 2026 at 3:49 pm
  • added an answer The web service doesn't do the authentication - IIS does.… May 11, 2026 at 3:49 pm

Related Questions

I've been doing a lot of research on how best to write correct network
I have a web app which connects to a server using a TCP connection
I have a server and has 2 clients connecting to it through TCP. These
I was hoping to implement a simple XMPP server in Java. What I need

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.