我已經搜索了很多關於這個問題,有關於如何通過從德爾福DLL中的字符串.NET 接下來的代碼沒有明確的答案是基於MSDN幫助文件:http://msdn.microsoft.com/en-us/library/4zey12w5.aspx傳遞字符串到.NET
//Delphi code
function myDelphiFunc(var Buffer: PChar): Integer; export; stdcall;
Buffer:='this is a test';
Return:=0;
end;
//vb.NET code
<DllImport(path)> _
Public Function myDelphiFunc(ByVal buffer As String) As Integer
Public Sub myNETFunc()
Dim buffer_size As integer = 25
Dim buffer As String = New String(CChar(" "), buffer_size)
Call myDelphiFunc(buffer)
MsgBox(Strings.Left(buffer, InStr(buffer, Chr(0)) - 1))
End Sub
調用myDelphiFunc時,我在「buffer」變量中收到一個空字符串。我究竟做錯了什麼?
在此先感謝
It's怪我,你的字符串找你試圖返回一個變量參數。我對VB一無所知,但在我看來,你已經分配了緩衝區,所以不需要從你的Delphi DLL中返回一個新的字符指針。不過,我決定不要質疑你的設計,併發佈一個與你目前的Delphi代碼兼容的答案。 – AlexSC