473,672 Members | 2,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2065
Assuming char is Ansi 8 bit...

[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Ansi)]
public struct DIR_PARM
{
[MarshalAs(Unman agedType.ByValT Str, SizeConst=4)]
public char rsvd;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=51)]
public char iadl1;
.....
public foot footer;
??? need to know the type of ADSR_REC
}

[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Ansi)]
struct foot
{
char a;
char b;
char c;
}
Willy.

"Angel" <none> wrote in message
news:OJ******** ******@tk2msftn gp13.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******** ******@tk2msftn gp13.phx.gbl...
Assuming char is Ansi 8 bit...

[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Ansi)]
public struct DIR_PARM
{
[MarshalAs(Unman agedType.ByValT Str, SizeConst=4)]
public char rsvd;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=51)]
public char iadl1;
....
public foot footer;
??? need to know the type of ADSR_REC
}

[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Ansi)]
struct foot
{
char a;
char b;
char c;
}
Willy.

"Angel" <none> wrote in message
news:OJ******** ******@tk2msftn gp13.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******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP12.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.foote r.a = ....
adsr_rect i an instance of DIR_PARM structure.

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

"Angel" <none> wrote in message
news:eN******** ******@TK2MSFTN GP12.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******** ******@tk2msftn gp13.phx.gbl...
Assuming char is Ansi 8 bit...

[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Ansi)]
public struct DIR_PARM
{
[MarshalAs(Unman agedType.ByValT Str, SizeConst=4)]
public char rsvd;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=51)]
public char iadl1;
....
public foot footer;
??? need to know the type of ADSR_REC
}

[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Ansi)]
struct foot
{
char a;
char b;
char c;
}
Willy.

"Angel" <none> wrote in message
news:OJ******** ******@tk2msftn gp13.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 StructLayoutAtt ribute it can be applied
to classes as well.
[AttributeUsage( AttributeTarget s.Class | AttributeTarget s.Struct)]
public sealed class StructLayoutAtt ribute : 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******** ******@tk2msftn gp13.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(La youtKind.Sequen tial, CharSet = CharSet.Ansi)]
public struct ZIP4_PARM
{
[MarshalAs(Unman agedType.ByValT Str, SizeConst=4)]
string rsvd0;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=51)]
public string iadl1;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=51)]
public string iadl2;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=51)]
public string ictyi;
[MarshalAs(Unman agedType.ByValT Str, 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(La youtKind.Sequen tial, CharSet = CharSet.Ansi)]
struct foot
{
[MarshalAs(Unman agedType.ByValT Str, SizeConst=1)]
public string a;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=1)]
public string b;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=1)]
public string c;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=1)]
public string d;
}


"Angel" <none> wrote in message
news:OJ******** ******@tk2msftn gp13.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(Unman agedType.ByValA rray, SizeConst=10)]
ADDR_REC[] stack;

...
}

struct ADDR_REC {
.....
}

Willy.

"Angel" <none> wrote in message
news:O9******** ******@TK2MSFTN GP09.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(La youtKind.Sequen tial, CharSet = CharSet.Ansi)]
public struct ZIP4_PARM
{
[MarshalAs(Unman agedType.ByValT Str, SizeConst=4)]
string rsvd0;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=51)]
public string iadl1;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=51)]
public string iadl2;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=51)]
public string ictyi;
[MarshalAs(Unman agedType.ByValT Str, 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(La youtKind.Sequen tial, CharSet = CharSet.Ansi)]
struct foot
{
[MarshalAs(Unman agedType.ByValT Str, SizeConst=1)]
public string a;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=1)]
public string b;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=1)]
public string c;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=1)]
public string d;
}


"Angel" <none> wrote in message
news:OJ******** ******@tk2msftn gp13.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.B yValArray, SizeConst=10)]
ADDR_REC[] stack;
.....

HTH
B\rgds
100
Nov 15 '05 #10

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

Similar topics

19
2110
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 arguments? Thanks in advance, Jasper Kent.
14
2635
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
2446
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 would be. Something to do with bind2nd or something, I bet. If there's another struct in the vector who's "id" data member is the same as the new struct, that's the one that should be replaced. Otherwise, I just want to add the struct to the end...
5
3071
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). Currently, the first four bytes store an ID information and the next sizeof(struct sockaddr_in) bytes store this struct. Now, say that i declare a struct sockaddr_in temp_addr variable...would the following lines be valid??
11
2626
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. This works without any problems. I now would like to filter "duplicate" records. They aren't really duplicate, I can't just qsort as is and eliminate the matching rows. I have number of fields that will be the same, but some of the fields will...
43
3811
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 "there is no difference between a struct and a class except the public/private access specification" (and a few minor other things). When I create a class, I always start by declaring the default constructor, copy constructor and assignment operator...
13
2520
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 functions in common implementations? And where is the 'this' ptr tucked away at for POD structs with member functions? John
6
8962
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 must be allocated (with known max length = 10) before the call to the dll function (the dll just fills it ,with no allocations). The definitions of Mystruct and :
24
3297
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 strings (string->index)...Do you know if there exist such a function somewhere? Thanks
0
8486
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8828
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8608
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8680
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6238
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5705
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.