C# 如何在c中将LPRAM转换为结构# 我有下面的C++结构< /P> typedef struct { char szAccountNo[11]; char szAccountName[40]; char act_pdt_cdz3[3]; char amn_tab_cdz4[4]; char expr_datez8[8]; char granted; char filler[189]; }ACCOUNTINFO; typedef struct { char szDate [14]; char szServerName [15]; char szUserID [8]; char szAccountCount [3]; ACCOUNTINFO accountlist [999]; }LOGININFO; typedef struct{ int TrIndex; LOGININFO *pLoginInfo; }LOGINBLOCK;

C# 如何在c中将LPRAM转换为结构# 我有下面的C++结构< /P> typedef struct { char szAccountNo[11]; char szAccountName[40]; char act_pdt_cdz3[3]; char amn_tab_cdz4[4]; char expr_datez8[8]; char granted; char filler[189]; }ACCOUNTINFO; typedef struct { char szDate [14]; char szServerName [15]; char szUserID [8]; char szAccountCount [3]; ACCOUNTINFO accountlist [999]; }LOGININFO; typedef struct{ int TrIndex; LOGININFO *pLoginInfo; }LOGINBLOCK;,c#,lparam,C#,Lparam,并转换成C级 我使用以下两种方法在WndProc中转换LPRAM 1. TrStruct.LOGINBLOCK lb = new TrStruct.LOGINBLOCK(); lb = (TrStruct.LOGINBLOCK)m.GetLParam(typeof(TrStruct.LOGINBLOCK)); 2. TrStruct.LOGINBLOCK lb = (TrStruct.LOGINBLOCK) Marshal.PtrToStruct

并转换成C级

我使用以下两种方法在WndProc中转换LPRAM

1. TrStruct.LOGINBLOCK lb = new TrStruct.LOGINBLOCK();
                        lb = (TrStruct.LOGINBLOCK)m.GetLParam(typeof(TrStruct.LOGINBLOCK));

2. TrStruct.LOGINBLOCK lb = (TrStruct.LOGINBLOCK) Marshal.PtrToStructure(m.LParam, typeof(TrStruct.LOGINBLOCK));
但是,它没有成功,也不会发出常规错误消息,而是在输出窗口中收到消息“mscorlib.dll中发生了类型为'System.TypeLoadException'的第一次意外异常”。
谁能告诉我我的转换有什么问题吗?

您的代码的第一个明显问题在于
CharSet.Unicode
。非托管代码中的结构字段都定义为
char
,这是一个单字节字符。您应该对所有结构使用
CharSet.Ansi

第二个问题是
LOGININFO
的最后一个字段。它被定义为

[MarshalAs(UnmanagedType.Struct, SizeConst = 99)]
public ACCOUNTINFO[] accountlist;
但在非托管代码中,它是

ACCOUNTINFO accountlist [999];

SizeConst
参数应更改为
999

对于初学者,您的字符集是ascii,而不是unicode。对值数组也使用
fixed
关键字。@leppie我不理解对值数组使用fixed关键字。你能更具体一点或者举个例子吗?请看@leppie我仍然不知道我应该在哪里使用“fixed”。您似乎引用了ACCOUNTINFO[]数组,但该数组应该引用ACCOUNTINFO类型。我不能对非原语类型的AccountInfo使用“fixed”抱歉,我没有意识到它们不能用于具有布局的类。然后使用结构。我尝试了您提供的所有解决方案,但效果不如以前。还有其他建议吗?
ACCOUNTINFO accountlist [999];