473,513 Members | 2,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting C struct to C#

VMI
How can I convert the following C struct into a C# struct? The struct is
sent to an API function (written in C) as parameter, and the function fills
it with data. I tried to convert it and most of it works but the data in the
foot struct is not correct (I believe it's a conversion problem between C#
and C).
The class I created in C# (from the struct) is at the bottom.

typedef struct
{
char iadl1[50+1];
char ictyi[50+1];
char auto_zone_ind;
char retcc;
struct {
char a;
char b;
char c;
char d;
char rsvd3[6];
} foot;
} ZIP4_PARM;

*****************************
/* My struct converted */
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public class ZIP4_PARM
{
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=51 )]
public string iadl1;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=51 )]
public string ictyi;
public char auto_zone_ind;
public char retcc;
public footer foot;
public struct footer
{
public char a;
public char b;
public char c;
public char d;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=6)]
public string rsvd3;
}
}
Nov 16 '05 #1
3 9397
VMI,

The problem comes from using a character type. In .NET this is a
unicode character, and not an 8-bit ANSI character like it is in C. You can
declare the type two ways:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public class ZIP4_PARM
{
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=51 )]
public string iadl1;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=51 )]
public string ictyi;
public byte auto_zone_ind;
public byte retcc;
public footer foot;
public struct footer
{
public byte a;
public byte b;
public byte c;
public byte d;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=6)]
public string rsvd3;
}
}

The other way you can declare it is by declaring all of the character
fields as strings, and then tagging them so they are marshaled as
ByValTStrs, with a size of one. Either way should work.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
How can I convert the following C struct into a C# struct? The struct is
sent to an API function (written in C) as parameter, and the function fills it with data. I tried to convert it and most of it works but the data in the foot struct is not correct (I believe it's a conversion problem between C# and C).
The class I created in C# (from the struct) is at the bottom.

typedef struct
{
char iadl1[50+1];
char ictyi[50+1];
char auto_zone_ind;
char retcc;
struct {
char a;
char b;
char c;
char d;
char rsvd3[6];
} foot;
} ZIP4_PARM;

*****************************
/* My struct converted */
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public class ZIP4_PARM
{
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=51 )]
public string iadl1;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=51 )]
public string ictyi;
public char auto_zone_ind;
public char retcc;
public footer foot;
public struct footer
{
public char a;
public char b;
public char c;
public char d;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=6)]
public string rsvd3;
}
}

Nov 16 '05 #2
VMI
Thanks. IT worked perfectly.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ew**************@tk2msftngp13.phx.gbl...
VMI,

The problem comes from using a character type. In .NET this is a
unicode character, and not an 8-bit ANSI character like it is in C. You can declare the type two ways:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public class ZIP4_PARM
{
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=51 )]
public string iadl1;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=51 )]
public string ictyi;
public byte auto_zone_ind;
public byte retcc;
public footer foot;
public struct footer
{
public byte a;
public byte b;
public byte c;
public byte d;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=6)]
public string rsvd3;
}
}

The other way you can declare it is by declaring all of the character
fields as strings, and then tagging them so they are marshaled as
ByValTStrs, with a size of one. Either way should work.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
How can I convert the following C struct into a C# struct? The struct is
sent to an API function (written in C) as parameter, and the function

fills
it with data. I tried to convert it and most of it works but the data in

the
foot struct is not correct (I believe it's a conversion problem between

C#
and C).
The class I created in C# (from the struct) is at the bottom.

typedef struct
{
char iadl1[50+1];
char ictyi[50+1];
char auto_zone_ind;
char retcc;
struct {
char a;
char b;
char c;
char d;
char rsvd3[6];
} foot;
} ZIP4_PARM;

*****************************
/* My struct converted */
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public class ZIP4_PARM
{
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=51 )]
public string iadl1;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=51 )]
public string ictyi;
public char auto_zone_ind;
public char retcc;
public footer foot;
public struct footer
{
public char a;
public char b;
public char c;
public char d;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=6)]
public string rsvd3;
}
}


Nov 16 '05 #3
Hi,

<snip>
*****************************
/* My struct converted */
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public class ZIP4_PARM
{
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=51 )]
public string iadl1;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=51 )]
public string ictyi;
public char auto_zone_ind;
public char retcc;
public footer foot;
[....] public struct footer
{
public char a;
public char b;
public char c;
public char d;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=6)]
public string rsvd3;
}
}


You must *also* prefix the child-struct with
[StructLayout(LayoutKind.Sequential, CharSet=Charset.Ansi)]. Other then
that I do not see any problems...
HTH,
greetings
Nov 16 '05 #4

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

Similar topics

8
3540
by: Grant Edwards | last post by:
Perhaps I'm doing something wrong: the struct module docs say it's IEE 754, but I can't figure out how to get it to handle NaN values correctly (either packing or unpacking). >>> x =...
1
1690
by: Majed | last post by:
hi, I'm trying to convert some .h files to API Declaration to use the function and structs. one of it is this: NTMS_GUID CurrentLibrary; // the current library NTMS_GUID MediaPool; // media...
5
17572
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...
4
2177
by: taskswap | last post by:
I'm converting an application that relies heavily on a binary network protocol. Within this protocol are a lot of byte arrays of character data, like: public unsafe struct MsgAddEntry {...
3
4827
by: David Bear | last post by:
I found this simple recipe for converting a dotted quad ip address to a string of a long int. struct.unpack('L',socket.inet_aton(ip)) trouble is when I use this, I get struct.error: unpack...
4
5053
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
5702
by: acap | last post by:
my problem is converting C to java, pls help..... this the code.... #include <stdio.h> #include <string.h> #include <ctype.h> #define FIRST_NAME_LEN 31 #define SECOND_NAME_LEN 51
1
2569
by: DaTurk | last post by:
I'm having an issue converting an unmanaged struct to a CLI managed struct. It's a value struct and this cannot be changed. public value struct UpdateItem { public: System::String ...
2
1661
by: lbscprogrammer | last post by:
Hi, I'm in trouble here... I need to get this program working to proceed with my application. The program is very complex, it works fine on a 10 year old system written in IBM C/2. I am showing...
4
3587
by: screamer81 | last post by:
Hello, I've a SDK functions description for a scanner and I try to convert unmanaged DLL C++ functions to c#. I converted all except one of them. This function is getting a struct parameter. ...
0
7259
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
7158
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
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.