我正在嘗試使用我無法訪問代碼的外部 BPL 包中的函式。
雖然我在 Delphi 中取得了成功,但在 C# 中,無論輸入如何,我仍然獲得相同的價值。
Delphi中BPL的外部使用
type
PRecord = ^TRecord;
TRecord = packed record
S: string;
LW: LongWord;
end;
function GetValue(W : Word) : PRecord; external 'package.bpl' name '@Unit@Function$qqrus';
GetValue()使用不同的輸入呼叫回傳不同的記錄,這是正確的。
C# 中 BPL 的外部使用
[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()使用不同的輸入呼叫總是回傳相同的指標,因此回傳結構。
我錯過了什么?
uj5u.com熱心網友回復:
function GetValue(W : Word) : PRecord;
external 'package.bpl' name '@Unit@Function$qqrus';
該函式使用 Delphi 的默認呼叫約定,即register. 其他工具不支持此功能,因此無法從您的 C# 代碼呼叫此函式。
您需要在 C# 代碼和 Delphi 包之間放置一個 Delphi DLL。該 Delphi DLL 將能夠呼叫register呼叫約定函式,并且可以匯出具有stdcall呼叫約定的函式以供您的 C# 代碼呼叫。
此外,您還有機會解決因使用 Delphistring型別而引起的任何潛在問題,這也是 Delphi 私有的。
對于它的價值,我高度懷疑該函式是否確實回傳了指向記錄的指標。我認為這更有可能是一個將記錄作為var或out引數的程序。而且我確實認為很可能存在與該字串的生命周期有關的問題。實際上,您不能期望使用沒有關于其介面資訊的二進制模塊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/406489.html
標籤:
