标签:style io ar os 使用 sp for on 问题
今天用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
Delphi的"Invalid pointer operation"异常的解决办法
标签:style io ar os 使用 sp for on 问题
原文地址:http://www.cnblogs.com/smartlittleant/p/4081350.html