473,385 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C struct to C#

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?

--------------------------------
From: Andre Lourenco

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>MgwpfPR15UOztF4b8dYn5A==</Id>
Nov 16 '05 #1
3 1324
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]
- mv*@spam.guard.caspershouse.com

"Andre Lourenco via .NET 247" <an*******@dotnet247.com> wrote in message
news:Oz*************@TK2MSFTNGP09.phx.gbl...
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?

--------------------------------
From: Andre Lourenco

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>MgwpfPR15UOztF4b8dYn5A==</Id>

Nov 16 '05 #2
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:un*************@tk2msftngp13.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Andre Lourenco via .NET 247" <an*******@dotnet247.com> wrote in message
news:Oz*************@TK2MSFTNGP09.phx.gbl...
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?

--------------------------------
From: Andre Lourenco

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>MgwpfPR15UOztF4b8dYn5A==</Id>


Nov 16 '05 #3
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:un*************@tk2msftngp13.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Andre Lourenco via .NET 247" <an*******@dotnet247.com> wrote in message
news:Oz*************@TK2MSFTNGP09.phx.gbl...
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?

--------------------------------
From: Andre Lourenco

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>MgwpfPR15UOztF4b8dYn5A==</Id>


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
10
by: Rick Anderson | last post by:
All, I am receiving the following compilation error on LINUX (but not Solaris, HPUX, WIN32, etc): compiling osr.c LBFO.h(369): warning #64: declaration does not declare anything extern...
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
7
by: Alex | last post by:
If I have two struct. See below: struct s1 { int type; int (*destroy)(struct s1* p); } struct s2 { struct s1 base;
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
4
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
4
by: Sheldon | last post by:
Hi, I have a unique case where I need an array of structs that grows and within this array is another struct that grows in some cases. I'm having trouble allocating memory. Since I have never...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.