加载dll时出现的问题

Hohai2007 2006-12-13 10:45:40
我的程序是这样的
TreturnDbname=function():String;stdcall;

procedure TForm1.Button1Click(Sender: TObject);
var
returnDbname : TReturnDbname;
AHandle: THandle;
begin

CoInitialize(nil);
AHandle:=LoadLibrary('CreatePro.dll');
try
if AHandle<>0 then
@returnDbname:=GetProcAddress(AHandle,'returnDbName');
if not(@returnDbname=nil) then
begin

dbname:=returnDbname();
showmessage('dbname='+dbname);
end;
finally
freeLibrary(AHandle);
end;
end;
我把freeLibrary(AHandle);注释掉程序执行完了关闭窗口时,但是整个project1却没有关闭依然在内存里运行着;而加上freeLibrary(AHandle);在程序运行完毕连showmessage('dbname='+dbname);也执行对了点击showmessage出现的窗口后就出现这样的错误:Access violation at address 00403F7E in module 'project1.exe'.Read of address 0168B792C.请问这是什么原因啊,该怎么解决,谢谢!
...全文
134 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hohai2007 2006-12-14
  • 打赏
  • 举报
回复
请各位高手帮帮忙啊
小呆之家 2006-12-14
  • 打赏
  • 举报
回复
呵一会儿功夫就有人给出答案了.
确实是STRING类型引起的错误.
在DLL中如果要使用字符串类型.最好还是使用字符串指针PCHAR类型为好.
下面是我的测试代码.......

type
TFun=function (var source:pchar):byte;stdcall;
var
Fun:tfun;
dllhandle:thandle;
p:pchar;
begin
dllhandle:=loadlibrary('StrTestDll.dll');
try
if dllhandle>32 then
begin
@fun:=getprocaddress(dllhandle,'TEST');
if not (@fun=nil) then
begin
fun(p);
showmessage(strpas(p));
end;
end;
finally
freelibrary(dllhandle);
end;
end;
hongqi162 2006-12-14
  • 打赏
  • 举报
回复
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.
hongqi162 2006-12-14
  • 打赏
  • 举报
回复
uses ShareMem;
或将string用PChar替换

2,495

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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