首页 技术 正文
技术 2022年11月15日
0 收藏 617 点赞 2,944 浏览 2156 个字

今天用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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,964
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781