急!!!, 请教DLL!!(100分)

  • 主题发起人 主题发起人 pmis
  • 开始时间 开始时间
P

pmis

Unregistered / Unconfirmed
GUEST, unregistred user!
我做一个DLL,当我第一次调用时可正确调用,<br>当我再次调用时就出现错误(Invalid Pointer Operation)!!<br>代码如下,请大家指点!!!,谢谢!!!<br>unit Form_1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>function FormatToDate(Tdate:string):String ; stdcall;external 'DateFormat_Project.DLL';<br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>Var<br>&nbsp; &nbsp;Dat:string;<br>begin<br>&nbsp; &nbsp;Dat:=trim(edit1.text);<br>&nbsp; &nbsp;Edit1.Text:=FormatToDate(Dat);<br>end;<br><br>end.<br><br>
 
因FormatToDate的返回值是string类型,要么把你DLL里的FormatToDate返回值改成<br>PChar要么在DLL的project单元加上sharemem。
 
最好用PChar类型吧。
 
function FormatToDate(Tdate:PChar):PChar ; stdcall;external 'DateFormat_Project.DLL'<br><br>your dll must change too
 
如果DLL输出的过程或函数带有长字符类型的参数,或者函数返回类型是长字符串<br>或带有长字符串元素的构造类型,Object Pascal规定无论是DLL还是调用它的程序<br>必须把ShareMem单元加到Uses部分。而ShareMem单元是从DelphiMM.DLL这个DLL中引<br>入的接口单元,因此这种程序分发时必须带有DelphiMM.DLL。Delphi建议为了避免<br>使用DelphiMM.DLL,传递字符串信息时使用PChar或ShortString类型参数。
 
各位大哥,你们的方法我都有试,但是我里面有一个变量我用的是datetostr()怎么改呢?<br>如下:<br>&nbsp; unit DateFormat;<br><br>interface<br>&nbsp;uses SysUtils;<br><br>function FormatToDate(Tdate:pchar):Pchar ; stdcall;<br>implementation<br><br>function FormatToDate(Tdate:pchar):Pchar;<br>var<br>&nbsp; s,y:string;<br>&nbsp; dat:pchar;<br><br>begin<br>&nbsp; s:=Format(Tdate,);<br>&nbsp; dat:=s;<br>&nbsp; y:=copy(datetostr(date),3,2);<br>&nbsp; //====月和日================================================<br>&nbsp; if (length(s)=3)and((copy(s,2,1)='-')or(copy(s,2,1)='/'))<br>&nbsp; &nbsp; &nbsp;then dat:='20'+y+'-0'+s[1]+'-0'+s[3];<br>&nbsp; if (length(s)=4)and((copy(s,2,1)='-')or(copy(s,2,1)='/'))<br>&nbsp; &nbsp; &nbsp;then dat:='20'+y+'-0'+s[1]+'-'+s[3]+s[4];<br>&nbsp; if (length(s)=4)and((copy(s,3,1)='-')or(copy(s,3,1)='/'))<br>&nbsp; &nbsp; &nbsp;then dat:='20'+y+'-'+s[1]+s[2]+'-0'+s[4];<br>&nbsp; if (length(s)=5)and((copy(s,3,1)='-')or(copy(s,3,1)='/'))<br>&nbsp; &nbsp; &nbsp;then dat:='20'+y+'-'+s[1]+s[2]+'-'+s[4]+s[5];<br>&nbsp; //====年月日================================================<br>&nbsp; if (length(s)=5)and(copy(s,2,1)='-')and(copy(s,4,1)='-')<br>&nbsp; &nbsp; &nbsp;then dat:='200'+s[1]+'-0'+s[3]+'-0'+s[5];<br><br>&nbsp; if (length(s)=6)and(copy(s,2,1)='-')and(copy(s,4,1)='-')<br>&nbsp; &nbsp; &nbsp;then dat:='200'+s[1]+'-0'+s[3]+'-'+s[5]+s[6];<br><br>&nbsp; if (length(s)=6)and(copy(s,2,1)='-')and(copy(s,5,1)='-')<br>&nbsp; &nbsp; &nbsp;then dat:='200'+s[1]+'-'+s[3]+s[4]+'-0'+s[6];<br><br>&nbsp; if (length(s)=7)and(copy(s,2,1)='-')and(copy(s,5,1)='-')<br>&nbsp; &nbsp; &nbsp;then dat:='200'+s[1]+'-'+s[3]+s[4]+'-'+s[6]+s[7];<br><br>&nbsp; if (length(s)=5)and(copy(s,2,1)='/')and(copy(s,4,1)='/')<br>&nbsp; &nbsp; &nbsp;then dat:='200'+s[1]+'-0'+s[3]+'-0'+s[5];<br><br>&nbsp; if (length(s)=6)and(copy(s,2,1)='/')and(copy(s,4,1)='/')<br>&nbsp; &nbsp; &nbsp;then dat:='200'+s[1]+'-0'+s[3]+'-'+s[5]+s[6];<br><br>&nbsp; if (length(s)=6)and(copy(s,2,1)='/')and(copy(s,5,1)='/')<br>&nbsp; &nbsp; &nbsp;then dat:='200'+s[1]+'-'+s[3]+s[4]+'-0'+s[6];<br><br>&nbsp; if (length(s)=7)and(copy(s,2,1)='/')and(copy(s,5,1)='/')<br>&nbsp; &nbsp; &nbsp;then dat:='200'+s[1]+'-'+s[3]+s[4]+'-'+s[6]+s[7];<br><br>&nbsp; //--------------------------------------------------------<br><br>&nbsp; if (length(s)=6)and(copy(s,3,1)='-')and(copy(s,5,1)='-')<br>&nbsp; &nbsp; &nbsp;then dat:='20'+s[1]+s[2]+'-0'+s[4]+'-0'+s[6];<br><br>&nbsp; if (length(s)=7)and(copy(s,3,1)='-')and(copy(s,6,1)='-')<br>&nbsp; &nbsp; &nbsp;then dat:='20'+s[1]+s[2]+'-'+s[4]+s[5]+'-0'+s[7];<br><br>&nbsp; if (length(s)=7)and(copy(s,3,1)='-')and(copy(s,5,1)='-')<br>&nbsp; &nbsp; &nbsp;then dat:='20'+s[1]+s[2]+'-0'+s[4]+'-'+s[6]+s[6];<br><br>&nbsp; if (length(s)=8)and(copy(s,3,1)='-')and(copy(s,6,1)='-')<br>&nbsp; &nbsp; &nbsp;then dat:='20'+s[1]+s[2]+'-'+s[4]+s[5]+'-'+s[7]+s[8];<br><br><br>&nbsp; if (length(s)=6)and(copy(s,3,1)='/')and(copy(s,5,1)='/')<br>&nbsp; &nbsp; &nbsp;then dat:='20'+s[1]+s[2]+'-0'+s[4]+'-0'+s[6];<br><br>&nbsp; if (length(s)=7)and(copy(s,3,1)='/')and(copy(s,6,1)='/')<br>&nbsp; &nbsp; &nbsp;then dat:='20'+s[1]+s[2]+'-'+s[4]+s[5]+'-0'+s[7];<br><br>&nbsp; if (length(s)=7)and(copy(s,3,1)='/')and(copy(s,5,1)='/')<br>&nbsp; &nbsp; &nbsp;then dat:='20'+s[1]+s[2]+'-0'+s[4]+'-'+s[6]+s[6];<br><br>&nbsp; if (length(s)=8)and(copy(s,3,1)='/')and(copy(s,6,1)='/')<br>&nbsp; &nbsp; &nbsp;then dat:='20'+s[1]+s[2]+'-'+s[4]+s[5]+'-'+s[7]+s[8];<br><br>&nbsp; //====返回值================================================<br>&nbsp; result:=dat;<br>end;
 
把dat改成string类型,然后把Result := dat改成StrPcopy(Result, dat);就可以了。
 
同意用 Pchar, 写 DLL最好用 Pchar, 特别是返回值!
 
真是高兴,有人和我犯同样的错误!!!!
 
无论是DLL还是调用它的程序必须把ShareMem单元加到Uses部分,并且必须放在第一个位置.<br>Dll中 和 工程中的uses语句如下:<br>uses <br>&nbsp; &nbsp;ShareMen,......;<br><br>&nbsp; .
 
uses sharemem我认为不太可取;<br>在dll中最好避免用string,改用pchar;<br>这样才会避免指针错误。
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
775
import
I
I
回复
0
查看
789
import
I
I
回复
0
查看
699
import
I
I
回复
0
查看
753
import
I
后退
顶部