【发布时间】:2022-01-07 13:19:24
【问题描述】:
我正在尝试使用我无法访问其代码的外部 BPL 包中的函数。
虽然我在 Delphi 中取得了成功,但在 C# 中,无论输入如何,我仍然获得相同的价值。
BPL在Delphi中的外部使用
type
PRecord = ^TRecord;
TRecord = packed record
S: string;
LW: LongWord;
end;
function GetValue(W : Word) : PRecord; external 'package.bpl' name '@Unit@Function$qqrus';
用不同的输入调用GetValue()返回不同的记录,这是正确的。
BPL 在 C# 中的外部使用
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TRecord
{
public string S;
public int LW;
}
[DllImport("package.bpl", EntryPoint = "@Unit@Function$qqrus", CharSet = CharSet.Unicode)]
static extern IntPtr GetValue(ushort number);
public static TRecord GetDelphiValue(ushort number)
{
var ptr = GetValue(number);
return (TRecord) Marshal.PtrToStructure(ptr, typeof(TRecord))
}
使用不同的输入调用GetDelphiValue() 总是返回相同的指针,因此返回结构。
我错过了什么?
【问题讨论】: