试图将一些C ++结构移植到C#问题。 [英] trying to port some C++ structures to C# problem.
问题描述
我正在尝试将一些C ++结构移植到C#但我遇到麻烦这个
一个。
注意:这是一个文件记录,所以我必须保留这个格式。
#pragma pack(push,1)
typedef struct {
char title [80];
DWORD dwCount;
} STLHEADER,* LPSTLHEADER;
#pragma pack(pop)
使用Google我发现这可以移植到这个C#变体,
注意''Marshal''来创建一个80字节的数组。
[StructLayout(LayoutKind.Sequential,Pack = 1)]
内部结构STLHEADER {
[MarshalAs(UnmanagedType.U1,SizeConst = 80)]
public char [] title;
public int dwCount;
}
到目前为止一切顺利,但现在我被困在这条线上sizeof(STLHEADER)需要
不安全的功能。
错误CS0208:无法获取托管
类型变量的地址或大小(''MySpace .STLHEADER'')
它与Marshal的事情有关。
我可以替换sizeof(STLHEADER)。 80 + 4,但我真的更喜欢
来获得更专业的解决方案。
也许还有另一种方法来定义''char title [80]''字节
数组?
感谢您的任何反馈。 :-)
-
http:/ /www.skyscan.be
I am trying to port some C++ structures to C# but I have troubles with this
one.
Note: this is a file record, so I must keep this format.
#pragma pack( push, 1 )
typedef struct {
char title[80];
DWORD dwCount;
} STLHEADER, *LPSTLHEADER;
#pragma pack( pop )
Using Google I discovered that this could be ported to this C# variant,
note the ''Marshal'' to create a 80 byte array.
[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct STLHEADER {
[MarshalAs (UnmanagedType.U1, SizeConst=80)]
public char[] title;
public int dwCount;
}
So far so good, but now I am stuck on this line "sizeof(STLHEADER)" needed
in a unsafe function.
error CS0208: Cannot take the address or size of a variable of a managed
type (''MySpace.STLHEADER'')
And it has something to do with the Marshal thing.
I could replace the "sizeof(STLHEADER)" with 80+4 but I would really prefer
to have a more professional sollution.
Maybe there is another alternative way to define a ''char title[80]'' byte
array?
Thanks for any feedback. :-)
--
http://www.skyscan.be
推荐答案
由于你的struct使用引用类型(char []),你需要使用
System.Runtime.InteropServices.Marshal.SizeOf确定结构大小
的方法。
还有一件事;你应该将char []成员更改为byte []。
否则,你的数据将无法正确解析,因为C#中的char [80]实际上是
160字节的大小。
byte []在C#中是什么char []在C ++中。
-
问候,
DennisJDMyrén
Oslo Kodebureau
" Olaf Baeyens" <醇********** @ skyscan.be>在消息中写道
news:41 ********************** @ news.skynet.be ...
Since you struct is using reference types(char []) you will need to use the
System.Runtime.InteropServices.Marshal.SizeOf method to determine the size
of your structure.
One more thing; you should change the char [] member into a byte [].
Otherwise, your data will not be parsed correctly, since char [80] in C# is
actually
160 bytes of size.
byte [] is in C# what char [] was in C++.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Olaf Baeyens" <ol**********@skyscan.be> wrote in message
news:41**********************@news.skynet.be...
我正在尝试将一些C ++结构移植到C#但我遇到了麻烦。
注意:这是一个文件记录,所以我必须保留这种格式。
#pragma pack(push,1)
typedef struct {
char title [80];
DWORD dwCount;
} STLHEADER,* LPSTLHEADER;
#pragma pack(pop)
使用Google我发现这可以移植到这个C#变体,
注意''Marshal''来创建一个80字节的数组。 />
[StructLayout(LayoutKind.Sequential,Pack = 1)]
内部结构STLHEADER {
[MarshalAs(UnmanagedType.U1,SizeConst = 80)]
public char []标题;
public int dwCount;
}
到目前为止一直很好,但现在我被困在这条线上sizeof(STLHEADER)
。
错误CS0208:无法获取ma变量的地址或大小naged
type(''MySpace.STLHEADER'')
它与Marshal的事情有关。
我可以替换sizeof(STLHEADER)。 80 + 4,但我真的希望
更专业的解决方案。
也许还有另一种方法来定义''char title [80]''byte
阵列?
感谢您的任何反馈。 :-)
-
http://www.skyscan .be
I am trying to port some C++ structures to C# but I have troubles with this
one.
Note: this is a file record, so I must keep this format.
#pragma pack( push, 1 )
typedef struct {
char title[80];
DWORD dwCount;
} STLHEADER, *LPSTLHEADER;
#pragma pack( pop )
Using Google I discovered that this could be ported to this C# variant,
note the ''Marshal'' to create a 80 byte array.
[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct STLHEADER {
[MarshalAs (UnmanagedType.U1, SizeConst=80)]
public char[] title;
public int dwCount;
}
So far so good, but now I am stuck on this line "sizeof(STLHEADER)"
needed
in a unsafe function.
error CS0208: Cannot take the address or size of a variable of a managed
type (''MySpace.STLHEADER'')
And it has something to do with the Marshal thing.
I could replace the "sizeof(STLHEADER)" with 80+4 but I would really
prefer
to have a more professional sollution.
Maybe there is another alternative way to define a ''char title[80]'' byte
array?
Thanks for any feedback. :-)
--
http://www.skyscan.be
和;
更改
[MarshalAs (UnmanagedType.U1,SizeConst = 80)]
进入
[MarshalAs(UnmanagedType.ByValArray,SizeConst = 80)]
所以,现在我们得到了:
[StructLayout(LayoutKind.Sequential,Pack = 1)]
内部结构STLHEADER
{
[MarshalAs(UnmanagedType.ByValArray,SizeConst = 80)]
public byte [] title;
public int dwCount;
}
-
问候,
DennisJDMyrén
奥斯陆Kodebureau
"DennisMyrén" <德**** @ oslokb.no>在消息中写道
news:2n ******************** @ news4.e.nsc.no ...
And;
change
[MarshalAs (UnmanagedType.U1, SizeConst=80)]
into
[MarshalAs (UnmanagedType.ByValArray, SizeConst=80)]
So, now we have got:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct STLHEADER
{
[MarshalAs (UnmanagedType.ByValArray, SizeConst = 80)]
public byte [] title;
public int dwCount;
}
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:2n********************@news4.e.nsc.no...
由于你的struct使用引用类型(char []),你需要使用
System.Runtime.InteropServices.Marshal.SizeOf方法来确定结构的大小
。
还有一件事;你应该将char []成员更改为byte []。
否则,你的数据将无法正确解析,因为C#
中的char [80]实际上是160字节大小。
byte []在C#中是什么char []在C ++中。
-
问候,
DennisJDMyrén
Oslo Kodebureau >Olaf Baeyens <醇********** @ skyscan.be>在消息中写道
新闻:41 ********************** @ news.skynet.be ...
Since you struct is using reference types(char []) you will need to use
the
System.Runtime.InteropServices.Marshal.SizeOf method to determine the size
of your structure.
One more thing; you should change the char [] member into a byte [].
Otherwise, your data will not be parsed correctly, since char [80] in C#
is actually
160 bytes of size.
byte [] is in C# what char [] was in C++.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Olaf Baeyens" <ol**********@skyscan.be> wrote in message
news:41**********************@news.skynet.be...
I我试图将一些C ++结构移植到C#但是我有麻烦这个
一个。
注意:这是一个文件记录,所以我必须保持这种格式。
#pragma pack(push,1)
typedef struct {
char title [80];
DWORD dwCount;
} STLHEADER,* LPSTLHEADER;
#pragma pack(pop)
使用Google我发现这可以移植到这个C#变体,
注意''Marshal''来创建一个80字节的数组。 />
[StructLayout(LayoutKind.Sequential,Pack = 1)]
内部结构STLHEADER {
[MarshalAs(UnmanagedType.U1,SizeConst = 80)]
public char []标题;
public int dwCount;
}
到目前为止一直很好,但现在我被困在这条线上sizeof(STLHEADER)
。
错误CS0208:无法获取ma变量的地址或大小naged
type(''MySpace.STLHEADER'')
它与Marshal的事情有关。
我可以替换sizeof(STLHEADER)。 80 + 4,但我真的希望
更专业的解决方案。
也许还有另一种方法来定义''char title [80]''byte
阵列?
感谢您的任何反馈。 :-)
-
http://www.skyscan .be
I am trying to port some C++ structures to C# but I have troubles with
this
one.
Note: this is a file record, so I must keep this format.
#pragma pack( push, 1 )
typedef struct {
char title[80];
DWORD dwCount;
} STLHEADER, *LPSTLHEADER;
#pragma pack( pop )
Using Google I discovered that this could be ported to this C# variant,
note the ''Marshal'' to create a 80 byte array.
[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct STLHEADER {
[MarshalAs (UnmanagedType.U1, SizeConst=80)]
public char[] title;
public int dwCount;
}
So far so good, but now I am stuck on this line "sizeof(STLHEADER)"
needed
in a unsafe function.
error CS0208: Cannot take the address or size of a variable of a managed
type (''MySpace.STLHEADER'')
And it has something to do with the Marshal thing.
I could replace the "sizeof(STLHEADER)" with 80+4 but I would really
prefer
to have a more professional sollution.
Maybe there is another alternative way to define a ''char title[80]'' byte
array?
Thanks for any feedback. :-)
--
http://www.skyscan.be
不确定为什么你需要这个尺码,但你必须:
- 将数组元素声明为UnmanagedType.ByValArray,并且
- 使用Marshal.SizeOf方法获取结构的封送大小。
[StructLayout(LayoutKind.Sequential,Pack = 1)]
内部结构STLHEADER {
[MarshalAs(UnmanagedType.ByValArray,SizeConst = 80)]
public char [] title;
public int dwCount;
}
... 。
STLHEADER stlh = new STLHEADER();
int marshaledSizeOfStruct = Marshal.SizeOf(stlh); //应该是84
Willy。
" Olaf Baeyens" <醇********** @ skyscan.be>在消息中写道
news:41 ********************** @ news.skynet.be ...
Not sure why you need this size for, but you have to:
- Declare the array element as UnmanagedType.ByValArray, and
- use the Marshal.SizeOf method to get the marshaled size of a struct.
[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct STLHEADER {
[MarshalAs (UnmanagedType.ByValArray, SizeConst=80)]
public char[] title;
public int dwCount;
}
....
STLHEADER stlh = new STLHEADER();
int marshaledSizeOfStruct = Marshal.SizeOf(stlh); // should be 84
Willy.
"Olaf Baeyens" <ol**********@skyscan.be> wrote in message
news:41**********************@news.skynet.be...
我正在尝试将一些C ++结构移植到C#但我遇到了麻烦。
注意:这是一个文件记录,所以我必须保留这种格式。
#pragma pack(push,1)
typedef struct {
char title [80];
DWORD dwCount;
} STLHEADER,* LPSTLHEADER;
#pragma pack(pop)
使用Google我发现这可以移植到这个C#变体,
注意''Marshal''来创建一个80字节的数组。 />
[StructLayout(LayoutKind.Sequential,Pack = 1)]
内部结构STLHEADER {
[MarshalAs(UnmanagedType.U1,SizeConst = 80)]
public char []标题;
public int dwCount;
}
到目前为止一直很好,但现在我被困在这条线上sizeof(STLHEADER)
。
错误CS0208:无法获取ma变量的地址或大小naged
type(''MySpace.STLHEADER'')
它与Marshal的事情有关。
我可以替换sizeof(STLHEADER)。 80 + 4,但我真的希望
更专业的解决方案。
也许还有另一种方法来定义''char title [80]''byte
阵列?
感谢您的任何反馈。 :-)
-
http://www.skyscan .be
I am trying to port some C++ structures to C# but I have troubles with this
one.
Note: this is a file record, so I must keep this format.
#pragma pack( push, 1 )
typedef struct {
char title[80];
DWORD dwCount;
} STLHEADER, *LPSTLHEADER;
#pragma pack( pop )
Using Google I discovered that this could be ported to this C# variant,
note the ''Marshal'' to create a 80 byte array.
[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct STLHEADER {
[MarshalAs (UnmanagedType.U1, SizeConst=80)]
public char[] title;
public int dwCount;
}
So far so good, but now I am stuck on this line "sizeof(STLHEADER)"
needed
in a unsafe function.
error CS0208: Cannot take the address or size of a variable of a managed
type (''MySpace.STLHEADER'')
And it has something to do with the Marshal thing.
I could replace the "sizeof(STLHEADER)" with 80+4 but I would really
prefer
to have a more professional sollution.
Maybe there is another alternative way to define a ''char title[80]'' byte
array?
Thanks for any feedback. :-)
--
http://www.skyscan.be
这篇关于试图将一些C ++结构移植到C#问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!