472,968 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Are C# structs and C structs the same? Help needed

I'm using several C functions (in a dll) that receive a struct as parameter.
Since I'm doing it in C#, I assume I need to recreate the struct in C# in
order to call the function with the required parameter. What would I need
to do in order to convert a struct that looks like this:

typedef struct
{

char rsvd0[4];
char iadl1[50+1];
char iadl2[50+1];
char ictyi[50+1];

struct { //nested struct
char a;
char b;
char c;
} foot;

ADSR_REC stack[10];
char rsvd4[194];
} DIR_PARM;

into a C# struct? I'm already able to call functions w/o parameters from my
application (through DllImport) but I'm having trouble calling a function
that requires this struct as parameter. And the "real" structure is
declared in Header file that I can't access (with #include). If I were doing
the program in C/C++ I would only need to include this header file and the
argument has to point to the struct. And how would I access DIR_PARM.foot.a
?

Any help would be appreciated.
Nov 15 '05 #1
10 2029
Assuming char is Ansi 8 bit...

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DIR_PARM
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=4)]
public char rsvd;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=51)]
public char iadl1;
.....
public foot footer;
??? need to know the type of ADSR_REC
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct foot
{
char a;
char b;
char c;
}
Willy.

"Angel" <none> wrote in message
news:OJ**************@tk2msftngp13.phx.gbl...
I'm using several C functions (in a dll) that receive a struct as
parameter.
Since I'm doing it in C#, I assume I need to recreate the struct in C# in
order to call the function with the required parameter. What would I need
to do in order to convert a struct that looks like this:

typedef struct
{

char rsvd0[4];
char iadl1[50+1];
char iadl2[50+1];
char ictyi[50+1];

struct { //nested struct
char a;
char b;
char c;
} foot;

ADSR_REC stack[10];
char rsvd4[194];
} DIR_PARM;

into a C# struct? I'm already able to call functions w/o parameters from
my
application (through DllImport) but I'm having trouble calling a function
that requires this struct as parameter. And the "real" structure is
declared in Header file that I can't access (with #include). If I were
doing
the program in C/C++ I would only need to include this header file and the
argument has to point to the struct. And how would I access
DIR_PARM.foot.a
?

Any help would be appreciated.

Nov 15 '05 #2
Why do you declare the nested struct as public foot footer? And how would
I access the members of this structure?

ADSR_REC is another structure.

Thanks again.
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:uL**************@tk2msftngp13.phx.gbl...
Assuming char is Ansi 8 bit...

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DIR_PARM
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=4)]
public char rsvd;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=51)]
public char iadl1;
....
public foot footer;
??? need to know the type of ADSR_REC
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct foot
{
char a;
char b;
char c;
}
Willy.

"Angel" <none> wrote in message
news:OJ**************@tk2msftngp13.phx.gbl...
I'm using several C functions (in a dll) that receive a struct as
parameter.
Since I'm doing it in C#, I assume I need to recreate the struct in C# in order to call the function with the required parameter. What would I need to do in order to convert a struct that looks like this:

typedef struct
{

char rsvd0[4];
char iadl1[50+1];
char iadl2[50+1];
char ictyi[50+1];

struct { //nested struct
char a;
char b;
char c;
} foot;

ADSR_REC stack[10];
char rsvd4[194];
} DIR_PARM;

into a C# struct? I'm already able to call functions w/o parameters from
my
application (through DllImport) but I'm having trouble calling a function that requires this struct as parameter. And the "real" structure is
declared in Header file that I can't access (with #include). If I were
doing
the program in C/C++ I would only need to include this header file and the argument has to point to the struct. And how would I access
DIR_PARM.foot.a
?

Any help would be appreciated.


Nov 15 '05 #3
In C++, struct and class are the same except that the default access level
of members in a class are private and in a struct they're public.

In C#, there are a lot more differences between a struct and a class. In C#:

1: structs are value types, classes are reference types
2: structs do not support inheritance
3: structs always have a default, no-parameter constructor, that can't be
replaced.
4: with structs, you can specify how fields are laid out in memory

(this list c/o Professional C#, 2nd edition)

In addition, I believe, though I'm not sure, that members of a struct
default to private access in C#.

Pete

--
http://www.petedavis.net
"Angel" <none> wrote in message
news:OJ**************@tk2msftngp13.phx.gbl...
I'm using several C functions (in a dll) that receive a struct as parameter. Since I'm doing it in C#, I assume I need to recreate the struct in C# in
order to call the function with the required parameter. What would I need
to do in order to convert a struct that looks like this:

typedef struct
{

char rsvd0[4];
char iadl1[50+1];
char iadl2[50+1];
char ictyi[50+1];

struct { //nested struct
char a;
char b;
char c;
} foot;

ADSR_REC stack[10];
char rsvd4[194];
} DIR_PARM;

into a C# struct? I'm already able to call functions w/o parameters from my application (through DllImport) but I'm having trouble calling a function
that requires this struct as parameter. And the "real" structure is
declared in Header file that I can't access (with #include). If I were doing the program in C/C++ I would only need to include this header file and the
argument has to point to the struct. And how would I access DIR_PARM.foot.a ?

Any help would be appreciated.

Nov 15 '05 #4

"Angel" <none> wrote in message
news:eN**************@TK2MSFTNGP12.phx.gbl...
Why do you declare the nested struct as public foot footer? *** because it's a nested struct of type foot, or am I missing something?
And how would I access the members of this structure?
*** DIR_PARM ar = new DIR_PARM();
ar.footer.a = 'x';
ADSR_REC is another structure.

Ok, but how does it looks like?

Willy.

Nov 15 '05 #5
Hi Angel,
Sinece foot is anonymous structure you can simply inline its members in
DIR_PARM.

If you use what Willy suggested you can access its mebers as

adsr_rect.footer.a = ....
adsr_rect i an instance of DIR_PARM structure.

You can find more info on
ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconstructssample.htm
or on-line:
http://msdn.microsoft.com/library/de...uctssample.asp
--
HTH
B\rgds
100

"Angel" <none> wrote in message
news:eN**************@TK2MSFTNGP12.phx.gbl...
Why do you declare the nested struct as public foot footer? And how would I access the members of this structure?

ADSR_REC is another structure.

Thanks again.
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:uL**************@tk2msftngp13.phx.gbl...
Assuming char is Ansi 8 bit...

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DIR_PARM
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=4)]
public char rsvd;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=51)]
public char iadl1;
....
public foot footer;
??? need to know the type of ADSR_REC
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct foot
{
char a;
char b;
char c;
}
Willy.

"Angel" <none> wrote in message
news:OJ**************@tk2msftngp13.phx.gbl...
I'm using several C functions (in a dll) that receive a struct as
parameter.
Since I'm doing it in C#, I assume I need to recreate the struct in C# in order to call the function with the required parameter. What would I need to do in order to convert a struct that looks like this:

typedef struct
{

char rsvd0[4];
char iadl1[50+1];
char iadl2[50+1];
char ictyi[50+1];

struct { //nested struct
char a;
char b;
char c;
} foot;

ADSR_REC stack[10];
char rsvd4[194];
} DIR_PARM;

into a C# struct? I'm already able to call functions w/o parameters from my
application (through DllImport) but I'm having trouble calling a function that requires this struct as parameter. And the "real" structure is
declared in Header file that I can't access (with #include). If I were
doing
the program in C/C++ I would only need to include this header file and the argument has to point to the struct. And how would I access
DIR_PARM.foot.a
?

Any help would be appreciated.



Nov 15 '05 #6
Hi Pete
In addition to what you said
4: with structs, you can specify how fields are laid out in memory
Even though the attribute is called StructLayoutAttribute it can be applied
to classes as well.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public sealed class StructLayoutAttribute : Attribute
In addition, I believe, though I'm not sure, that members of a struct default to private access in C#.

Yes. All memebers of structs and classes default to *private*

--
B\rgds
100

Pete

--
http://www.petedavis.net
"Angel" <none> wrote in message
news:OJ**************@tk2msftngp13.phx.gbl...
I'm using several C functions (in a dll) that receive a struct as

parameter.
Since I'm doing it in C#, I assume I need to recreate the struct in C# in order to call the function with the required parameter. What would I need to do in order to convert a struct that looks like this:

typedef struct
{

char rsvd0[4];
char iadl1[50+1];
char iadl2[50+1];
char ictyi[50+1];

struct { //nested struct
char a;
char b;
char c;
} foot;

ADSR_REC stack[10];
char rsvd4[194];
} DIR_PARM;

into a C# struct? I'm already able to call functions w/o parameters from

my
application (through DllImport) but I'm having trouble calling a function that requires this struct as parameter. And the "real" structure is
declared in Header file that I can't access (with #include). If I were

doing
the program in C/C++ I would only need to include this header file and the argument has to point to the struct. And how would I access

DIR_PARM.foot.a
?

Any help would be appreciated.


Nov 15 '05 #7
The documentation for the api says that the "argument must point to a
ZIP4_PARM structure".
My main concern is that since I don't know how the C function (in the dll)
accesses the struct (assuming it does it in C-style), how is it going to
know that the nested struct is not nested in the new struct? The CD with the
DLL comes from the US Postal Service, so I'm pretty sure it's compatible
with C#.
Also, one of the members of this struct is another struct (ADDR_REC) that's
composed three members of type char. How would I add this member of type
ADDR_REC to this struct?
In Parms.cs:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct ZIP4_PARM
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=4)]
string rsvd0;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=51)]
public string iadl1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=51)]
public string iadl2;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=51)]
public string ictyi;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=3)]
public string istai;

/**** This is the member I need to add. Would it use a MarshalAs? ****/
ADDR_REC stack[10];

public foot footer;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct foot
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1)]
public string a;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1)]
public string b;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1)]
public string c;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1)]
public string d;
}


"Angel" <none> wrote in message
news:OJ**************@tk2msftngp13.phx.gbl...
I'm using several C functions (in a dll) that receive a struct as parameter. Since I'm doing it in C#, I assume I need to recreate the struct in C# in
order to call the function with the required parameter. What would I need
to do in order to convert a struct that looks like this:

typedef struct
{

char rsvd0[4];
char iadl1[50+1];
char iadl2[50+1];
char ictyi[50+1];

struct { //nested struct
char a;
char b;
char c;
} foot;

ADSR_REC stack[10];
char rsvd4[194];
} DIR_PARM;

into a C# struct? I'm already able to call functions w/o parameters from my application (through DllImport) but I'm having trouble calling a function
that requires this struct as parameter. And the "real" structure is
declared in Header file that I can't access (with #include). If I were doing the program in C/C++ I would only need to include this header file and the
argument has to point to the struct. And how would I access DIR_PARM.foot.a ?

Any help would be appreciated.

Nov 15 '05 #8
[MarshalAs(UnmanagedType.ByValArray, SizeConst=10)]
ADDR_REC[] stack;

...
}

struct ADDR_REC {
.....
}

Willy.

"Angel" <none> wrote in message
news:O9**************@TK2MSFTNGP09.phx.gbl...
The documentation for the api says that the "argument must point to a
ZIP4_PARM structure".
My main concern is that since I don't know how the C function (in the dll)
accesses the struct (assuming it does it in C-style), how is it going to
know that the nested struct is not nested in the new struct? The CD with
the
DLL comes from the US Postal Service, so I'm pretty sure it's compatible
with C#.
Also, one of the members of this struct is another struct (ADDR_REC)
that's
composed three members of type char. How would I add this member of type
ADDR_REC to this struct?
In Parms.cs:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct ZIP4_PARM
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=4)]
string rsvd0;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=51)]
public string iadl1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=51)]
public string iadl2;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=51)]
public string ictyi;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=3)]
public string istai;

/**** This is the member I need to add. Would it use a MarshalAs? ****/
ADDR_REC stack[10];

public foot footer;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct foot
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1)]
public string a;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1)]
public string b;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1)]
public string c;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1)]
public string d;
}


"Angel" <none> wrote in message
news:OJ**************@tk2msftngp13.phx.gbl...
I'm using several C functions (in a dll) that receive a struct as

parameter.
Since I'm doing it in C#, I assume I need to recreate the struct in C# in
order to call the function with the required parameter. What would I
need
to do in order to convert a struct that looks like this:

typedef struct
{

char rsvd0[4];
char iadl1[50+1];
char iadl2[50+1];
char ictyi[50+1];

struct { //nested struct
char a;
char b;
char c;
} foot;

ADSR_REC stack[10];
char rsvd4[194];
} DIR_PARM;

into a C# struct? I'm already able to call functions w/o parameters from

my
application (through DllImport) but I'm having trouble calling a function
that requires this struct as parameter. And the "real" structure is
declared in Header file that I can't access (with #include). If I were

doing
the program in C/C++ I would only need to include this header file and
the
argument has to point to the struct. And how would I access

DIR_PARM.foot.a
?

Any help would be appreciated.


Nov 15 '05 #9
Hi,
/**** This is the member I need to add. Would it use a MarshalAs? ****/ ADDR_REC stack[10];


Try to declare ADDR_REC's managed equivalent and then

in the ZIP4_PARM

....
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=10)]
ADDR_REC[] stack;
.....

HTH
B\rgds
100
Nov 15 '05 #10

Hi Angel,

Is your problem resolved? Does the community's reply make sense to you?

If you still have anything unclear, please feel free to tell me, I will
help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #11

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

Similar topics

19
by: Jasper Kent | last post by:
Can anyone explain the logic behind structs being allowed neither memeber initialisers or default constructors. Doesn't this just encourage developers to create constructors with dummy...
14
by: Dave | last post by:
Hello all, I would like to use offsetof on a non-POD struct, but of course this is undefined. How else may I get the same effect? Thanks, Dave
8
by: Joe Van Dyk | last post by:
I have a vector of structs. Say I get a new struct that's supposed to replace one of the structs in the vector. I believe I could use replace_if() to do this, but I'm not sure what the syntax...
5
by: ravi.sathyam | last post by:
Hey, So say I have a sockaddr_in struct stored in a packet which I receive from my udp socket.....and it is stored within a certain offset into this packet (which is basically a char array)....
11
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out....
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
6
by: =?Utf-8?B?QWxleGFuZGVyZmU=?= | last post by:
Hi, I have a C# program that uses an unmanaged dll that has a function similar to the signature below : void f(out MyStruct arr, out int num); // num = actual array length returned The array...
24
by: Alexander Mahone | last post by:
Hello, I'm looking for an hash function to be used for an hash table that will contain structs of a certain kind. I've looked into Sourceforge.net, but so far I've found only hash functions for...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.