C struct to C#

  • Thread starter Thread starter Andre Lourenco via .NET 247
  • Start date Start date
A

Andre Lourenco via .NET 247

Hi,

I have a socket server that runs on digital unix true 64 (done in the old days) that receives structs and return structs. What i want is to communicate with it but in C#. My main problem his to map my c structs.
QUESTION : How can i represen this (see below) struct in c#:

struct myStruct {
short a;
short b;
int c;
short d;
char e[121];
} myStruct;

Thank you so much.

Andr?
 
Andre,

I would do it like this:

[StructLayout(LayoutKind.Sequential)]
public struct myStruct
{
public short a;
public short b;
public int c;
public short d;
[MarshalAs(UnmanagedTypes.ByValTStr, SizeConst=121)]
public string e;
}

This will allow you to pass the struct to APIs that expect it, and allow
the translation between unmanaged and managed to occur correctly.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andre Lourenco via .NET 247 said:
Hi,

I have a socket server that runs on digital unix true 64 (done in the old
days) that receives structs and return structs. What i want is to
communicate with it but in C#. My main problem his to map my c structs.
QUESTION : How can i represen this (see below) struct in c#:

struct myStruct {
short a;
short b;
int c;
short d;
char e[121];
} myStruct;

Thank you so much.

Andr?
 
You can also do stuff like this to pass structures around


private void button1_Click(object sender, System.EventArgs e)
{
ChannelData.CData CurrentChannelData = new ChannelData.CData();

if(MyChannelTest(CurrentChannelData).Alarm == true)
label1.Text = "Alarm = true";
else
label1.Text = "Alarm = false";
}

public ChannelData.CData MyChannelTest(ChannelData.CData
CurrentChannelData)
{

CurrentChannelData.Alarm = true;
return CurrentChannelData;
}


namespace ChannelData
{
public struct CData
{
public uint Time;
public uint Date;
public float [] Point;
public bool Alarm;
public bool Event;
public byte Status;
}
}

Nicholas Paldino said:
Andre,

I would do it like this:

[StructLayout(LayoutKind.Sequential)]
public struct myStruct
{
public short a;
public short b;
public int c;
public short d;
[MarshalAs(UnmanagedTypes.ByValTStr, SizeConst=121)]
public string e;
}

This will allow you to pass the struct to APIs that expect it, and allow
the translation between unmanaged and managed to occur correctly.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andre Lourenco via .NET 247 said:
Hi,

I have a socket server that runs on digital unix true 64 (done in the
old
days) that receives structs and return structs. What i want is to
communicate with it but in C#. My main problem his to map my c structs.
QUESTION : How can i represen this (see below) struct in c#:

struct myStruct {
short a;
short b;
int c;
short d;
char e[121];
} myStruct;

Thank you so much.

Andr?
 
Nicholas,

I have a quesstion. Does the struct needs to be garbage collected manually
since it allocates unmanaged memory?

--
Regards,

Jose Luis Manners, MCP

"Encuentra felicidad en tu trabajo o nunca serás feliz."
-Kung-Fu-Tsu (Confucio)

Nicholas Paldino said:
Andre,

I would do it like this:

[StructLayout(LayoutKind.Sequential)]
public struct myStruct
{
public short a;
public short b;
public int c;
public short d;
[MarshalAs(UnmanagedTypes.ByValTStr, SizeConst=121)]
public string e;
}

This will allow you to pass the struct to APIs that expect it, and allow
the translation between unmanaged and managed to occur correctly.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andre Lourenco via .NET 247 said:
Hi,

I have a socket server that runs on digital unix true 64 (done in the
old
days) that receives structs and return structs. What i want is to
communicate with it but in C#. My main problem his to map my c structs.
QUESTION : How can i represen this (see below) struct in c#:

struct myStruct {
short a;
short b;
int c;
short d;
char e[121];
} myStruct;

Thank you so much.

Andr?
 
Back
Top