0
令说,鉴于LegacyLib.dll以下特征:编组char函数参数的最佳方法是什么?
int Login(SysInst *inst, char username[8], char password[6]);
简单的方法元帅在C#中这个功能将是:
[DllImport("LegacyLib.dll", CharSet=CharSet.Ansi)]
public static extern int Login(ref SysInst inst, string username, string password);
在这么幼稚的做这件事的问题方式是我们传递给username
或password
参数的托管字符串可能比数组边界更长,这可能会导致LegacyLib.dll中的缓冲区溢出。
有没有更好的方法来克服这个问题?即是否有任何快速的[MarshalAs(…)]
魔法可以用来抵制这种魔法?