DLL中能不能不用ShareMem单元?

Jimy 2000-01-28 04:17:00
问题问得真过瘾,再问一个:
首先我要exports这样一个函数
Function abc: PChar;
所以我要在DLL中分配内存
Function abc: PChar;
Begin
result:=StrAlloc(xxx);
....对result赋值
End;

接下来在主程序中引用Function abc
ShowMessage(abc);
If StrBufferSize(abc)>0 Then StrDispose(abc);
运行程序会提示"无效指针操作"
在DLL和Application中同时加入ShareMem后一切正常。
请问我怎么才不需要ShareMem单元?

另外一个问题
我在DLL中声明
Type
MyDynArray=Array of PChar;

Procedure efg(Out a: MyDynArray);//或者Var a: MyDynArray
在efg中分配内存,然后传出

在主程序中使用a,并且释放其内存。除了与上面同样的问题外,
还想问一下,在Visual C++ 中是如何引用Delphi如MyDynArray类型
的动态数组的?






...全文
486 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
mutant 2000-01-29
  • 打赏
  • 举报
回复
最好不要采用ShareMem单元,否则经常会导致异常错误的发生.
由于Delphi本身的问题,在模块间或是DLL间传递复杂变量类型
会使接收方无法正常接收.
采用PChar而不是String在模块间传递字符串,因为String在Delphi中也是复杂类型.
kxy 2000-01-29
  • 打赏
  • 举报
回复
1) 改成GetMem试试
929 2000-01-28
  • 打赏
  • 举报
回复
Sharemee单元是DELPHI为了进行字符串的优化处理而设的。DELPHI中的ANSISTRING,WIDESTRING等字符串类型都是动态分配,并且引用计数的。通过这样的处理,可使字符串的处理速度大大加快。如果在DLL中完全脱离DELPHI的字符串处理及内存分配功能,而以API函数实现,我想在DLL中不用SHAREMEM单元完全是可以的。比如说通过文件映射(FILEMAPING)、虚存分配函数来实现。
tiger 2000-01-28
  • 打赏
  • 举报
回复
我想应该没用的, 应为as parameters 也要用sharemen
Jimy 2000-01-28
  • 打赏
  • 举报
回复
问题见文章:DLL中能不能不用ShareMem单元?
谢谢Tiger的回答,
你贴的那段文章我似乎在help中看过。依你的建议,我应该如何修改我的程序呢?
Function a:PChar;能不能修改成Procedure a(out return: PChar)来避开
"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), "中function result的限制?这我可以试一下,

但是动态数组我又该用什么替代,最好是在VC++中能用char*[]来引用?
char*[]是不是很类似Array of PChar???
tiger 2000-01-28
  • 打赏
  • 举报
回复
如果你的dll需要传递动态数组, 就必须加上sharemem单元,
具体请看下面

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.
olo 2000-01-28
  • 打赏
  • 举报
回复
听听

5,379

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧