DLL裡面的共用資料區段怎麼寫?

來源:互聯網
上載者:User
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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.