请高人指点好急(10分)

  • 主题发起人 主题发起人 ANDREWWUYOU
  • 开始时间 开始时间
A

ANDREWWUYOU

Unregistered / Unconfirmed
GUEST, unregistred user!
请高手指点 我把下面函数做成DLL文件要用时调用他但是每次用完关掉程序时或调用时出现问题:说该程序执行了非法操作即将关闭。如果仍有问题请与程序供应商联系.<br> library ymzpdll;<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. }<br><br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes;<br><br>&nbsp; <br>{$R *.res}<br>function encrypt(entext:string;key:string):string;stdcall;<br>//自定义的加密函数<br>var<br>x,i, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// count variables<br>sText,sPW: Integer; // size of Text, PW<br>Text,PW: &nbsp; PChar; &nbsp; // buffer for Text, PW<br>begin<br>&nbsp; sText:=Length(entext)+1;<br>&nbsp; sPW:=Length(key)+1;<br>&nbsp;GetMem(Text,sText);<br>&nbsp; GetMem(PW,sPW);<br>&nbsp; StrPCopy(Text,entext);<br>&nbsp; StrPCopy(PW,key);<br>&nbsp; x:=0; // initialize count<br>&nbsp; for i:=0 to sText-2 do<br>&nbsp; begin<br>&nbsp; &nbsp; Text:=Chr(Ord(Text)+Ord(PW[x]));<br>&nbsp; &nbsp; Inc(x);<br>&nbsp; &nbsp; if x=(sPW-1)then x:=0;<br>&nbsp; end;<br>&nbsp; result:=text;<br>&nbsp; FreeMem(Text);<br>&nbsp; FreeMem(PW);<br>end;<br><br>function decrypt(detext:string;key:string):string;stdcall; //自定义的解密函数<br>var<br>x,i, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// count variables<br>sText,sPW: Integer; // size of Text, PW<br>Text,PW: &nbsp; PChar; &nbsp; // buffer for Text, PW<br>begin<br>&nbsp; sText:=Length(detext)+1;<br>&nbsp; sPW:=Length(key)+1;<br>&nbsp; GetMem(Text,sText);<br>&nbsp; GetMem(PW,sPW);<br>&nbsp; StrPCopy(Text,detext);<br>&nbsp; StrPCopy(PW,key);<br>&nbsp; x:=0; // initialize count<br>&nbsp; for i:=0 to sText-2 do<br>&nbsp; begin<br>&nbsp; &nbsp; Text:=Chr(Ord(Text)-Ord(PW[x]));<br>&nbsp; &nbsp; Inc(x);<br>&nbsp; &nbsp; if x=(sPW-1)then x:=0;<br>&nbsp; end;<br>&nbsp; result:=text;<br>&nbsp;FreeMem(Text);<br>&nbsp; FreeMem(PW);<br>end;<br>exports<br>encrypt,decrypt;<br><br><br>begin<br>end.<br>&nbsp; <br><br><br><br>后来我做一个程序测试,就是为了调用他看看有什么,结果发现我调试时他说我 invalid pointer operation!<br><br>implementation<br>&nbsp; &nbsp;function encrypt(entext:string;key:string):string;stdcall;external'ymzpdll.dll';//自定义的加密函数<br>&nbsp; &nbsp;function decrypt(detext:string;key:string):string;stdcall;external'ymzpdll.dll';<br><br><br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var temp:string;<br>begin<br>temp:=encrypt('i love you 123',inttostr(1120));<br>showmessage(temp);<br>end;<br><br>end.
 
--&gt; &nbsp;result:=text;<br>--&gt; FreeMem(Text);<br>[:D][:D][:D][:D][:D]<br>delphi中的string起始就是一个指针。<br>指针赋值给另一个指针并不会copy指针所指向的内容。因此FreeMem(Text)之后result就指向一块已经被释放掉的内存了
 
建议你把字符串调用全部改成PCHAR类型。
 
怎么同样的问题发两遍?d
 
to Another_eYes 我是初学者能不能给我说详细点要怎样做行吗
 
问题出在加解密函数里,不在DLL的调用里
 
后退
顶部