我有一個DLL從那裏我只知道他的簽名就像在Delphi編寫:打電話給德爾福DLL,並通過一個C#流它
function GeResultToStream(Param1: PChar; Param2: PChar; Param3:PChar; Param4: integer; out Param5: DWord; ParamStream: IStream; Error: PChar; ErrorSize: integer):BOOL;stdcall; external 'MYDLL.dll';
我有一些麻煩調用從C#這個DLL方法。如果一切正常,該方法應該也傳輸ParamStream
中的一些數據並返回true
。
我曾嘗試以下(以及其它各種DLL的方法參數的變化),但是不成功的(每次我打電話使用引用或OUT參數我是越來越錯誤此功能):
public class TestDLL
{
internal class DLLInternal
{
const string _dllLocation = "MYDLL.dll";
[DllImport(_dllLocation,
CallingConvention = CallingConvention.StdCall,
CharSet = CharSet.Ansi,
EntryPoint = "GeResultToStream")]
public static extern bool GeResultToStream(string Param1, string Param2, string Param3, int Param4, char[] Param5, ref Stream ParamStream, string Error, int ErrorSize);
}
public TestDLL()
{
}
~TestDLL()
{
}
public Stream GetStream(string Param1, string Param2, string Param3, int Param4, char[] Param5, string Error, int ErrorSize)
{
try
{
Stream stream = new MemoryStream();
bool x = DLLInternal.GeResultToStream(Param1, Param2, Param3, Param4, Param5, ref stream, Error, ErrorSize);
//here i think i might need to use somehow the Marshal class
return stream;
}
catch (Exception ex)
{
Console.WriteLine("Exception GetStream:" + ex.ToString());
return null;
}
}
}
我的猜測是我沒有理解如何在C#中正確使用它來轉換籤名。如果需要,我可以提供更多信息(比如我得到的錯誤,但大多數是腐敗的內存)。
我想你想使用'System.Runtime.InteropServices.ComTypes.IStream' – dman2306
我希望你有點困惑。像C#Streams這樣的.NET對象類型不能有意義地傳遞給Delphi,除非我們在Delphi方面介紹一些很難編寫的.NET interop代碼。你所做的事實上是瘋了。 –
事實上,使用Stream對象是一個錯誤。就像David Heffernan所說的那樣,使用一個IStream接口和一個基本的包裝器就可以做到這一點。 – avi