今天用Delphi編寫了個dll,用到了TStringList來傳遞多參數,如下:
[delphi] view plaincopy
01.function DBM_SetParam(procName: string; paramValues: TStringList = nil): Boolean;stdcall;
02.var
03. i: integer;
04.begin
05. try
06. if (paramValues<>nil) then
07. begin
08. for i := 0 to paramValues.Count - 1 do
09. begin
10. GParameters.AddParameter;
11. GParameters[i].Value := paramValues[i];
12. end;
13. end;
14. Result := True;
15. except on e: Exception do
16. begin
17. Result := False;
18. end;
19. end;
20.end;
然而在應用程序中調用的時候,卻出了點問題:整個運行過程是有效的,並且參數也都傳遞成功,但是當結束了該API函數執行后每次都會彈出“Invalid pointer operation”的異常。調用代碼如下:
[delphi] view plaincopy
01.procedure TForm1.Button3Click(Sender: TObject);
02.var
03. paramList: TStringList;
04.begin
05. paramList := TStringList.Create;
06. paramList.Add('2');
07. if (not DBM_SetParam('GetUserName', paramList)) then
08. begin
09. ShowMessage('Set parameters failed!');
10. end;
11.
12.end;
在google里搜索Invalid pointer value發現遇到同樣問題的同志也不在少數。順藤摸瓜,終於在Delphi聯機幫助里找到了下面的描述:
On Windows, if a DLL exports routines that pass long strings or dynamic arrays as parameters or function results (whether directly or nested in records or objects), then the DLL and its client applications (or DLLs) must all use the ShareMem unit. The same is true if one application or DLL allocates memory with New or GetMem which is deallocated by a call to Dispose or FreeMem in another module. ShareMem should always be the first unit listed in any program or library uses clause where it occurs.
ShareMem is the interface unit for the BORLANDMM.DLL memory manager, which allows modules to share dynamically allocated memory. BORLANDMM.DLL must be deployed with applications and DLLs that use ShareMem. When an application or DLL uses ShareMem, its memory manager is replaced by the memory manager in BORLANDMM.DLL.
Note
Linux uses glibc's malloc to manage shared memory.
【2010-6-3:翻譯一下,供大家參考:
Windows中如果一個動態庫導出函數以參數形式傳遞或者返回了一個長字符串或者動態數組(無論是直接傳遞還是包含在一個對象中),那么動態庫以及其調用程序都必須得引用ShareMem單元。同樣,如果程序或者庫通過New或者GetMem申請了內存,並且該內存在另一個模塊中通過Dispose或FreeMem釋放,也得這么做。務必把ShareMem放在程序或者庫所有引用單元之首。
ShareMem是BORLANDMM.dll內存管理單元接口,它允許模塊間共享動態申請內存。BORLANDMM.dll必須與使用了改單元的程序或者Dll一塊兒部署。當程序或者動態庫使用了ShareMem,其內存管理將會由BORLANDMM.dll接管。
注意:
Linux下使用glibc的malloc來管理共享內存。】
上述問題僅在windows里才有啊,呵呵,於是在dll里把Uses子句的top 1增加ShareMem引用,另外在應用程序的Project域的Uses子句第一個引用也加上ShareMem。最后編譯,運行,沒有看到哪個煩人的異常提示了:P