在线求救dll问题,请各位大哥帮忙(20分)

  • 主题发起人 主题发起人 deardragon_2002
  • 开始时间 开始时间
D

deardragon_2002

Unregistered / Unconfirmed
GUEST, unregistred user!
dll源码:<br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;sysutils, windows, dialogs;<br><br>function MessageLook(sss: string): string; stdcall;<br><br>implementation<br><br>function MessageLook(sss: string): string; stdcall;<br>var<br> &nbsp;s: PCHar;<br>begin<br> &nbsp;Result := 'zhangqiujunDll!';<br> &nbsp;//Strcopy(PChar(p), s);<br> &nbsp;//Result := True; //<br> &nbsp;//showmessage(Result);<br> &nbsp;//Result := 5;<br>end;<br><br>end.<br><br><br><br>以下是调用过程:<br><br>implementation<br><br>uses<br> &nbsp;unit2;<br><br>{$R *.dfm}<br>var<br> &nbsp;DLLHandle: THandle;<br>//var<br>type<br> &nbsp;TMessageLook = function(s: string): string;<br>var<br> &nbsp;MessageLook: TMessageLook;<br>procedure LoadDLL;<br>var<br> &nbsp;str: string;<br>begin<br> &nbsp;try<br> &nbsp; &nbsp;DLLHandle := LoadLibrary('Project2.dll');<br> &nbsp; &nbsp;if DLLHandle &gt;= 32 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;@MessageLook := GetProcAddress(DLLHandle,'MessageLook');<br> &nbsp; &nbsp; &nbsp;Assert(@MessageLook &lt;&gt; nil);<br> &nbsp; &nbsp; &nbsp;str := Form1.edit1.text;<br> &nbsp; &nbsp; &nbsp;str := MessageLook(str);<br> &nbsp; &nbsp; &nbsp;//str := p[1];<br> &nbsp; &nbsp; &nbsp;showmessage(str);<br> &nbsp; &nbsp;end;<br> &nbsp;finally<br> &nbsp; &nbsp;//FreeLibrary(DLLHandle);<br> &nbsp;end;<br>end;<br><br><br>请高手指点为什么不能访问MessageLook函数?
 
两个问题:第一最好参数不要string,如果用string则必须应用单元叫什么sharmm好像,<br>第二:既然上面都stdcall;<br>TMessageLook = function(s: string): string;没有stdcall?
 
错了,是delphimm
 
返回值最好是pchar类型
 
留个mail给你个示例
 
传入和返回值最好用pchar或char array
 
terryapp大哥,我的mail: qiujun.zhang@kingstargroup.com
 
我刚开始用pchar不行,然后换string了,还是同样的错误。
 
没有export怎么访问
 
这个有的,没贴出来,dll加载成功了,就是函数访问后报Invalid pointer operation!
 
收邮件。。。
 
1.不要用string类型<br>2.不要用PChar参数做为返回值,而应该做为输出参数传入DLL,并应该由EXE负责分配并释放内存空间<br>你的函数应该改为<br>function MessageLook(<br> &nbsp;sss: pchar;<br> &nbsp;sResult : pchar;<br> &nbsp;iResultLength : integer<br> &nbsp;): boolean; stdcall;
 
.dll中<br>function MessageLook(sss: string): string; stdcall;<br>调用时<br>TMessageLook = function(s: string): string;//在这加 stdcall;<br>还有不知道你在dll的project中<br>exports MessageLook;写了没有
 
把你的工程文件打开,改成这样:<br>program (你的工程名);<br><br>uses<br> &nbsp;sharemem,//这个很重要<br> &nbsp;Forms,...<br>再把你的DLL文件打开,改成这样:<br>library (你的DLL工程名);<br>uses<br> &nbsp;sharemem,//这个同样很重要<br> &nbsp;SysUtils,<br> &nbsp;Classes;<br><br><br>还有我觉得你的DLL源码不太正宗,有这样的 unit Unit1;??<br>应是library (你的DLL工程名);吧,个人意见,呵呵
 
terryapp大哥,很谢谢你啊,不过我的dll函数中如果按照输入参数<br>function MessageLook(sss: string): string; stdcall;<br>begin<br> &nbsp;Result := 'zhangqiujunDll!';<br>end;<br><br>报错:Invalid pointer operation! <br><br>但是以下就正确了,<br>function MessageLook(sss: string): string; stdcall;<br>begin<br> &nbsp;Result := sss;<br>end;<br><br>谁可以解释下啊
 
lkjx2007,大哥,你真牛,谢谢了啊<br>用你的方法,好了!<br>哈哈
 
不客气,呵呵,以前我也有这情况的,
 
在建一个dll时,你先看头上有一段说明,<br>就是你要的答案!<br>{ Important note about DLL memory management: ShareMem must be the<br> &nbsp;first unit in your library's USES clause AND your project's (select<br> &nbsp;Project-View Source) USES clause if your DLL exports any procedures or<br> &nbsp;functions that pass strings as parameters or function results. This<br> &nbsp;applies to all strings passed to and from your DLL--even those that<br> &nbsp;are nested in records and classes. ShareMem is the interface unit to<br> &nbsp;the BORLNDMM.DLL shared memory manager, which must be deployed along<br> &nbsp;with your DLL. To avoid using BORLNDMM.DLL, pass string information<br> &nbsp;using PChar or ShortString parameters. }
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
647
import
I
I
回复
0
查看
830
import
I
I
回复
0
查看
806
import
I
后退
顶部