假设我TMemoryStream
需要传递给我的DLL并从DLL中获取TMemoryStream
(位图流).
我在想我的DLL会:
procedure Process( InBuff: Pointer; InBuffSize: Integer; var OutBuff: Pointer; var OutBuffSize: Integer ); stdcall;
该InBuff
很容易(我认为).我通过TMemoryStream.Memory
和TMemoryStream.Size
.
问题是我如何OutBuff
在DLL中分配,并且调用者应用程序可以将其转换回TMemoryStream
以后释放该内存(由调用者应用程序)?
调用者将使用dynamic LoadLibrary
/ FreeLibrary
each DLL调用.
我非常想要一个示例代码.希望我不是太粗鲁.
注1:调用者应用程序不知道输出大小,并假设它不能指定MAX buff大小.
注2:我不确定我的DLL签名.如果我做错了,请原谅我.我正在寻找一种效果很好的模式(也许不仅适用于Delphi而且适用于C++/C#Calller以及=我的奖金)
稍微不同的方法是将每个内存流包装为IStream,并传递生成的接口引用.所以,从DLL的一面:
uses System.SysUtils, System.Classes, Vcl.AxCtrls; procedure DoProcess(InStream, OutStream: TStream); begin //...do the actual processing here end; //wrapper export procedure Process(AInStream: IStream; out AOutStream: IStream); safecall; var InStream, OutStream: TStream; begin InStream := TOleStream.Create(AInStream); try OutStream := TMemoryStream.Create; try DoProcess(InStream, OutStream); AOutStream := TStreamAdapter.Create(OutStream, soOwned); except OutStream.Free; raise; end; finally InStream.Free; end; end;
我个人也喜欢使用safecall,因为它是一种简单安全的方法,但我想这是一个品味问题.
编辑
上述的一种变体是让调用者同时提供要读取的流和要写入的流:
//wrapper export procedure Process(AInStream, AOutStream: IStream); safecall; var InStream, OutStream: TStream; begin InStream := TOleStream.Create(AInStream); try OutStream := TOleStream.Create(AOutStream); try DoProcess(InStream, OutStream); finally OutStream.Free; end; finally InStream.Free; end; end;
EXE端可能看起来像这样:
//wrapper import type TDLLProcessProc = procedure(AInStream, AOutStream: IStream); safecall; procedure Process(AInStream, AOutStream: TStream); var InStream, OutStream: IStream; DLLProc: TDLLProcessProc; Module: HMODULE; begin InStream := TStreamAdapter.Create(AInStream, soReference); OutStream := TStreamAdapter.Create(AOutStream, soReference); Module := LoadLibrary(MySuperLib); if Module = 0 then RaiseLastOSError; try DLLProc := GetProcAddress(Module, 'Process'); if @DLLProc = nil then RaiseLastOSError; DLLProc(InStream, OutStream); finally FreeLibrary(Module); end; end;