我正在编写一个与旧 C DLL 通信的 C# 应用程序。我查看了 C DLL 的标头并看到了需要在 C# 中创建的 2 个结构:
typedef struct
{
char user_field_name[MAXLENGTH];
WORD user_field_length;
char user_field_contents[80];
char user_field_requested[1];
char user_field_clear[1];
char user_field_attribute[1];
} U_FIELD_STRUCT;
typedef U_FIELD_STRUCT FAR * P_U_FIELD_STRUCT;
typedef struct
{
char user_begin_literal[7];
char user_screen_name[MNFRMSCRNAMELENGTH];
char key_to_be_sent[256];
WORD num_of_user_fields;
U_FIELD_STRUCT user_field[MAXFIELDCOUNT];
char user_end_literal[7];
} USER_FIELD_STRUCT;
在我的 C# 类中,我将它们编写如下:
public const Int32 MAXLENGTH = 51;
public const Int32 MAXTAGLENGTH = 80;
public const Int32 MNFRMSCRNAMELENGTH = 5;
public const Int32 MAXFIELDCOUNT = 100;
public const Int32 MAXTAGCOUNT = 40;
public const Int32 MAXSCREENCOUNT = 150;
public const Int32 NO_SCREEN_DATA = 99;
[StructLayout(LayoutKind.Sequential, Pack=4, CharSet=CharSet.Ansi)]
public struct U_FIELD_STRUCT
{
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=MAXLENGTH)]
public char [] user_field_name;
public short user_field_length;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=80)]
public char [] user_field_contents;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=1)]
public char [] user_field_requested;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=1)]
public char [] user_field_clear;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=1)]
public char [] user_field_attribute;
};
[StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Ansi)]
public struct USER_FIELD_STRUCT
{
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=7)]
public char [] user_begin_literal;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=MNFRMSCRNAMELENGTH)]
public char [] user_screen_name;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=256)]
public char [] key_to_be_sent;
public short num_of_user_fields;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=MAXFIELDCOUNT)]
public U_FIELD_STRUCT [] user_field;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=7)]
public char [] user_end_literal;
};
当我在 C# 中初始化结构时,我执行如下操作:
USER_FIELD_STRUCT UserFieldsData = new USER_FIELD_STRUCT();
UserFieldsData.user_begin_literal = new char[7];
UserFieldsData.user_screen_name = new char[MNFRMSCRNAMELENGTH];
UserFieldsData.key_to_be_sent = new char[256];
UserFieldsData.user_field = new U_FIELD_STRUCT[MAXFIELDCOUNT];
UserFieldsData.user_end_literal = new char[7];
for (int i = 0; i < MAXFIELDCOUNT; i++)
{
UserFieldsData.user_field[i].user_field_name = new char[MAXLENGTH];
UserFieldsData.user_field[i].user_field_contents = new char[80];
UserFieldsData.user_field[i].user_field_requested = new char[1];
UserFieldsData.user_field[i].user_field_clear = new char[1];
UserFieldsData.user_field[i].user_field_attribute = new char[1];
}
问题是,当我调用我的方法时,我收到一条错误消息(基本上 DLL 必须验证字符串,它会发送一条消息,指出字符串不是它们应该是的)。所以我安装了 VS6 并在调试模式下运行 DLL。我查看了我正在发送的结构,字符串确实包含我放入其中的数据(一些 char[] 没有数据,而另一些则组合了 2 个字段。)
例如,如果我这样设置字段,则字段:
user_begin_literal=”T0k0a10”
user_end_literal=”T0k0b10”
user_screen_name=”SC00”
当我在 VS6 中查看对象时:
user_begin_literal=” T0k0a10”
user_end_literal=””
user_screen_name=”SC00”
我搞砸了我的结构设计吗?