Struct Send with socket from c# to delphi

Posted on 2008-11-15
Last Modified: 2012-06-21
I want to send the struct to another Program by using TcpClient. I found a
method that converts struct to byte but it does not works correctly. What
am i missing?

public struct student
   public char[] sName;
 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)]
 public char[] sLastName;
Question by:tolgaong
    LVL 1

    Expert Comment

    How are you converting it to binary? are you using Binary Serialization, or something else?
    LVL 9

    Accepted Solution

    This code converts struct to  byte array is below with the title.

    I found my mistake. I filled with space chars. Therefore it differs from the correct result. I had to fill unused array elements with 0\'0'.

    student tolga = new student();
    char[] sName = new char[33];
    "Tolga".ToCharArray().CopyTo(sName, 0);
    tolga.sName = sName;
    char[] sLastName = new char[33];
    "Ongunsu".ToCharArray().CopyTo(sLastName, 0);
    tolga.sLastName = sLastName;

    private byte[] StructureToByteArray(object obj)
                int len = Marshal.SizeOf(obj);
                byte[] arr = new byte[len];
                IntPtr ptr = Marshal.AllocHGlobal(len);
                Marshal.StructureToPtr(obj, ptr, true);
                Marshal.Copy(ptr, arr, 0, len);
                return arr;
    LVL 1

    Expert Comment

    Ok, glad you figured it out.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now