C#结构和C结构是否相同?需要帮助 [英] Are C# structs and C structs the same? Help needed

查看:56
本文介绍了C#结构和C结构是否相同?需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用几个接收结构作为参数的C函数(在dll中)。

因为我在C#中这样做,我假设我需要重新创建结构C#in

命令用所需参数调用该函数。为了转换如下所示的结构,我需要做什么?



typedef struct

{


char rsvd0 [4];

char iadl1 [50 + 1];

char iadl2 [50 + 1];

char ictyi [50 + 1];


struct {//嵌套结构

char a;

char b;

char c;

} foot;


ADSR_REC堆栈[10];

char rsvd4 [194];

} DIR_PARM;


进入C#结构?我已经能够通过我的

应用程序(通过DllImport)调用没有参数的函数但是我在调​​用函数时遇到了麻烦

需要这个结构作为参数。并且真实的结构是在Header文件中声明的我无法访问的
(使用#include)。如果我用C / C ++做程序,我只需要包含这个头文件,并且

参数必须指向结构。我将如何访问DIR_PARM.foot.a




任何帮助都将不胜感激。

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.

推荐答案

假设char是Ansi 8位...


[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;

.....

公共脚页脚;

???需要知道ADSR_REC的类型

}


[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Ansi)]

struct foot

{

char a;

char b;

char c;

}

Willy。


" Angel" <无>在消息中写道

新闻:OJ ************** @ 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...
我正在使用接收结构作为
参数的几个C函数(在一个dll中)。
因为我在C#中这样做,我假设我需要在C#中重新创建结构。使用所需参数调用该函数。我需要做什么才能转换如下所示的结构:

typedef struct
{/ /
char rsvd0 [4];
char iadl1 [50 + 1];
char iadl2 [50 + 1];
char ictyi [50 + 1];

struct {//嵌套struct
char a;
char b;
char c;
} foot;

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

进入C#结构?我已经能够通过
我的
应用程序调用函数(通过DllImport),但是我在调​​用一个需要这个结构作为参数的函数时遇到了麻烦。并且真实的在Header文件中声明了我无法访问的结构(使用#include)。如果我用C / C ++编写程序,我只需要包含这个头文件,而
参数必须指向struct。我将如何访问
DIR_PARM.foot.a


任何帮助将不胜感激。
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.



为什么要将嵌套结构声明为公共页脚?怎么会

我访问这个结构的成员?


ADSR_REC是另一种结构。


再次感谢。

" Willy Denoyette [MVP]" <无线************* @ pandora.be>在消息中写道

news:uL ************** @ tk2msftngp13.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...
假设char是Ansi 8位...

[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Ansi)]
公共结构DIR_PARM
{
[MarshalAs(UnmanagedType.ByValTStr,SizeConst) = 4)]
public char rsvd;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = 51)]
公共字母iadl1;
....
公共脚页脚;
???需要知道ADSR_REC的类型


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

Willy。

天使 <无>在消息中写道
新闻:OJ ************** @ 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...
我正在使用几个C函数(在接收结构作为
参数的dll中。
因为我在C#中这样做,我假设我需要在C#
中重新创建结构以便调用该函数必需的参数。为了转换看起来像这样的结构,我需要做什么?b $ b:

typedef struct


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

struct {//嵌套struct
char a;
char b;
char c;
} foot;

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

进入C#结构?我已经能够通过我的
应用程序(通过DllImport)调用没有参数的函数,但是我在调​​用需要这个结构作为参数的
函数时遇到了麻烦。并且真实的在Header文件中声明了我无法访问的结构(使用#include)。如果我用C / C ++做程序,我只需要包含这个头文件和
参数必须指向结构。我将如何访问
DIR_PARM.foot.a


任何帮助将不胜感激。
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.




在C ++中,struct和class是相同的,除了默认访问级别

类中的成员是私有的并且在结构中它们' '公开。


在C#中,结构和类之间存在很多差异。在C#中:


1:结构是值类型,类是引用类型

2:结构不支持继承

3 :structs总是有一个默认的,无参数的构造函数,不能替换


4:使用结构,你可以指定字段的布局方式记忆


(此列表c / o专业C#,第2版)


另外,我相信,虽然我不确定,结构的成员

默认使用C#进行私人访问。


Pete


-
http://www.petedavis.net

" ;天使" <无>在消息中写道

新闻:OJ ************** @ tk2msftngp13.phx.gbl ...
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...
我正在使用接收结构为
参数的几个C函数(在dll中)。因为我在C#中这样做,所以我假设我需要在C#中重新创建结构,以便用所需的参数调用该函数。我需要做什么才能转换如下所示的结构:

typedef struct
{/ /
char rsvd0 [4];
char iadl1 [50 + 1];
char iadl2 [50 + 1];
char ictyi [50 + 1];

struct {//嵌套struct
char a;
char b;
char c;
} foot;

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

进入C#结构?我已经能够从
我的应用程序(通过DllImport)调用没有参数的函数,但是我在调​​用一个需要这个结构作为参数的函数时遇到了麻烦。并且真实的在Header文件中声明了我无法访问的结构(使用#include)。如果我用$ C $ b在C / C ++中编写程序,我只需要包含这个头文件,而
参数必须指向struct。我将如何访问
DIR_PARM.foot.a?

任何帮助将不胜感激。
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.



这篇关于C#结构和C结构是否相同?需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆