DLL里面的共享数据段怎么写? 用delphi,谢谢!

解决方案 »

  1.   

    内存映射文件,Delphi好似没有共享段的声明。
      

  2.   

    楼上,不是吧,我现在的情况是在调用dll之前,先要向dll里传一个值,有什么方案可以解决?
      

  3.   

    如果仅仅是传值,就不必要大动干戈了吧。
    共享数据段是用来多个进程同时调用一个DLL时,做进程间通讯用途的。
      

  4.   

    我可以给你参考下我以前写过的关于 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.