DLL裡面的共用資料區段怎麼寫? Delphi / Windows SDK/API
http://www.delphi2007.net/DelphiAPI/html/delphi_20061208112551103.html
DLL裡面的共用資料區段怎麼寫? 用delphi,謝謝!
怎麼沒人回啊!?
記憶體對應檔,Delphi好似沒有共用段的聲明。
樓上,不是吧,我現在的情況是在調用dll之前,先要向dll裡傳一個值,有什麼方案可以解決?
僅僅是傳值的話,就用正常的傳值方法唄。
如果僅僅是傳值,就不必要大動幹戈了吧。
共用資料區段是用來多個進程同時調用一個DLL時,做進程間通訊用途的。
我可以給你參考下我以前寫過的關於 dll共用一個整數的例子 你可以參考下
library DLLMem;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
windows,
SysUtils,
Classes,
SHareMem in 'SHareMem.pas';
exports
setsharedata,getsharedata; //提供介面
{$R *.res}
begin //程式從這裡進入
DllProc:=@DllEntrypoint;
DllEntryPoint(DLL_PROCESS_ATTACH);
end.
unit SHareMem;
interface
uses
SysUtils,windows;
type
Tsharedata=integer;
Psharedata=^Tsharedata;
const
sharedata_size=sizeof(Tsharedata);
mapfilenamw='DLLMem';
var
maphandle:Thandle;
sharedata:psharedata;
procedure opensharedata;
procedure closesharedata;
procedure setsharedata(value:Tsharedata);stdcall;
function getsharedata:Tsharedata;stdcall;
procedure DllEntrypoint (reason:dword);
implementation
procedure opensharedata; //把檔案對應到記憶體
begin
maphandle:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,sharedata_size,mapfilenamw);
//if maphandle=0 then Raise Exception('建立公有資料的buf不成功');
sharedata:=mapviewoffile(maphandle,File_Map_Write,0,0,sharedata_size);
end ;
procedure closesharedata; //取消記憶體對應檔
begin
UnMapViewOfFile(sharedata);
closehandle(maphandle);
end;
procedure setsharedata(value:Tsharedata);stdcall; //從程式中讀入直到檔案中
begin
sharedata^:=value;
end;
function getsharedata:Tsharedata;stdcall; //從檔案中取出直
begin
result:=sharedata^;
end;
procedure DllEntrypoint (reason:dword); //入口函數,目的是把檔案對應到記憶體或取消記憶體影射檔案
begin
case reason of
Dll_PROCESS_ATTACH:opensharedata;
Dll_PROCESS_DETACH:closesharedata;
end;
end;
end.
如果你看得滿意就給我點分吧
TMapFile