Dll中内存的问题(100分)

  • 主题发起人 主题发起人 龙石佛
  • 开始时间 开始时间

龙石佛

Unregistered / Unconfirmed
GUEST, unregistred user!
高手请看下面代码,该函数在Application中可以编译过去并调用成功。为什么做成Dll(可以编译成功),在调用该dll时执行下面的函数<br>就会有问题:提示 Project test.ext raised exception class EInvalidPointer whit message <br>'Invalid pointer operation',Process stopped.Use Step or Run to continue.<br>该信息是表明调用Dispose(),FreeMem()试图释放非法的或已经释放的内存时将发生这个异常。<br><br>谁能告诉我为什么?错在哪?<br>const<br>&nbsp; c1=52845;<br>&nbsp; c2=22719;<br>Function encrypt(S:string;key:LongWord):String;StdCall;<br>var<br>&nbsp; i:LongWord;<br>&nbsp; Str:String;<br>&nbsp;// Pin:String;<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; &nbsp;if Length(S)&gt;=6 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Str:=Copy(S,1,6);<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp;// SetLength(Pin,6);<br>&nbsp; &nbsp; &nbsp; &nbsp; SetLength(Result,6);<br>&nbsp; &nbsp; &nbsp; &nbsp; for i:=1 to length(Str) do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result:=char(byte(Str)xor(key shr 8));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Pin:=char(byte(Str)xor(key shr 8));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;key:=(byte(result)+key)*c1+c2;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // key:=(byte(Pin)+key)*c1+c2;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp;except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;on Exception do<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; end<br>&nbsp; except<br>&nbsp; &nbsp; &nbsp;on Exception do<br>&nbsp; end;<br>end;
 
把S和返回值的声明string改成pchar试试<br>在使用result前最好先声明一个中间变量,完成后最后再赋给result
 
老掉牙的问题了:请在DLL和应用工程中的第一个单元引用ShareMem<br>
 
TO ysai 您所说的方法我也试过了,不行!
 
To 无业游民,在Dll和Application中加了ShareMem后把出现提示的时间推后了即在我关闭<br>Application的可执行文件后。<br>那是为什么?
 
把你改过的代码贴出来。
 
to zw84611:<br>就是<br>uses<br>&nbsp; Windows, Dialogs, SysUtils, Classes,ShareMem;
 
to zw84611:<br>就是<br>uses<br>&nbsp; Windows, Dialogs, SysUtils, Classes,ShareMem;
 
1.ysai的意思是让你改成pchar<br><br>2.如果你用string:<br>&nbsp; ShareMem must be the<br>&nbsp; first unit in your library's USES clause AND your project's (select<br>&nbsp; ~~~~~~ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;~~~~~<br>&nbsp; Project-View Source) USES clause <br><br>注意:1, first,就是说要uses ShareMem,Windows,....<br>&nbsp; &nbsp; &nbsp; 2, 两边都要加
 
to zw84611:<br>我已经在<br>uses中把ShareMem放在第一位了(both add),还是一样!
 
把StdCall去掉试试
 
to zw84611:<br>不行,还是不行!<br>但ysai所说的方法我还没有完前调试通过!正在继续试!
 
要两边都去掉。
 
TO zw84611:<br>&nbsp;都去掉了,一关闭应用程序就会出现提示:<br>&nbsp; 应用程序发生异常未知的软件异常(0x0eedfade),位置为0x77e69b01。<br>我估计出现该提示是因为Application和dll之间的调用和被调用的协议被破坏了,因为我们把<br>它们中的stdcall删除了!
 
to zw84611:<br>&nbsp; 按前一提示的确定后,出现:<br>"0x00405116"指令引用的“0x00dc0928”内存。该内存不能为"read".<br>
 
你要把ShareMem Uses到工程文件中的第一个单元。不是Form之类的!!!切忌切忌!<br>如:<br>&nbsp; program Test<br>&nbsp; ...<br>&nbsp; uses<br>&nbsp; &nbsp; ShereMem,<br>&nbsp; &nbsp; ...<br>&nbsp; &nbsp; Unit1 in 'Unit1.pas';<br>&nbsp; &nbsp;...<br><br>明白了吗?
 
to 无业游民:<br>&nbsp; &nbsp;都是呀!<br>unit test;<br><br>interface<br><br>uses<br>&nbsp; ShareMem, Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls, Buttons;<br><br>--------------<br><br>library testdll;<br><br>uses<br>&nbsp; ShareMem, Windows, Dialogs, SysUtils, Classes;
 
错了,是dpr文件!<br>菜单:Project-&gt;View Source
 
to zw84611:<br>&nbsp; &nbsp;为什么是在program中而不是在unit中加
 
我不知道,但它的注释中是这样说的。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
576
import
I
I
回复
0
查看
718
import
I
后退
顶部