1. 程式人生 > >[delphi技術] 關於sharemem造成dll錯誤的處理辦法

[delphi技術] 關於sharemem造成dll錯誤的處理辦法

今天一直在研究delphi生成dll的技術,搞了一天,略知皮毛。

其中在測試時,發現如果程式和dll之間用string作為引數傳遞時容易出現錯誤,通過查詢得知,這需要在程式的uses中使用sharemem。

但是測試了好多次都沒有成功,最後發現是自己對此理解錯誤了,這個sharemem需要用在呼叫和被呼叫的情況時候都使用。往往我們會在主程式的時候忘掉了引用。切記主程式的.dpr檔案中也要引用。

具體方法 project-view source 在uses後邊加入 sharemem,

這中情況只發生在傳遞引數未string情況下(還有動態陣列,還沒研究到,呵呵)。其他情況下不用呼叫。

另外,我們需要注意一個問題。你用sharemem編譯出的dll和程式,可能在你的機子上執行很正常,但是在一臺沒有安裝delphi的機子上就會出問題。這是因為,你咋安裝delphi的同時,把所需要的dll都安裝了。

怎麼解決這個問題?都安裝一遍delphi?!!呵呵,理論上是可以的,當然世界上沒有這麼笨的人。

當 uses sharemem, 時,請把borlndmm.dll拷貝到應用檔案的同一路徑下。borlndmm.dll在delphi的安裝路徑下,可以搜尋一下。

{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.} 

  已經說的很清楚了,如果的匯出函式中引數或返回值有string型別,那麼就需要引用ShareMem