【发布时间】:2012-05-14 13:52:35
【问题描述】:
我一直在尝试通过以下方式调用在 Delphi 中创建的方法:
function _Func1(arrParams: array of TParams): Integer;stdcall;
type
TParams = record
Type: int;
Name: string;
Amount : Real;
end;
我的代码是:
[DllImport("some.dll", EntryPoint = "_Func1", CallingConvention = CallingConvention.StdCall)]
public static extern int Func(
[MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.Struct)] TParams[] arrParams)
结构是:
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct TParams
{
public int Type;
[MarshalAs(UnmanagedType.AnsiBStr)]
public string Name;
public double Amount;
}
当我调用这个方法时,我得到了错误: 无法封送类型为“TParams”的字段“名称”:托管/非托管类型组合无效(字符串字段必须与 LPStr、LPWStr、BStr 或 ByValTStr 配对)。
但是这些组合都不起作用,因为 Delphi 的字符串以它的长度为前缀,而且它肯定是 Ansi(我已经尝试过使用其他字符串参数)。有人知道如何解决这个问题吗?
【问题讨论】: