[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Struct Send with socket from c# to delphi

Posted on 2008-11-15
3
Medium Priority
?
1,341 Views
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?


[StructLayout(LayoutKind.Sequential)]
public struct student
  {
    [MarshalAs(UnmanagedType.ByValArray,SizeConst=33)]
   public char[] sName;
 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)]
 public char[] sLastName;
 }
0
Comment
Question by:tolgaong
  • 2
3 Comments
 
LVL 1

Expert Comment

by:colonel720
ID: 22968004
How are you converting it to binary? are you using Binary Serialization, or something else?
0
 
LVL 9

Accepted Solution

by:
tolgaong earned 0 total points
ID: 22968057
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;

STRUCT TO BYTE
-------------------------------------------------------------
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);
            Marshal.FreeHGlobal(ptr);
            return arr;
        }
0
 
LVL 1

Expert Comment

by:colonel720
ID: 22968085
Ok, glad you figured it out.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month17 days, 14 hours left to enroll

830 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