为什么这么简单的dll(只有一个Function,无参数且返回值是boolean)执行到最后仍报这个错误:invalid pointer operation?(

B

Boblee

Unregistered / Unconfirmed
GUEST, unregistred user!
为什么这么简单的dll(只有一个Function,无参数且返回值是boolean)执行到最后仍报这个错误:invalid pointer operation?(80分)<br />library AutoUpdate;

{ 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. }

uses
SysUtils, IniFiles,IdHTTP, Dialogs,Classes;

{$R *.res}


function checkIfNeedUpdate():boolean; stdcall;
var
ustream:tmemorystream;
newverstr,oldverstr:pchar;
updIniFile:TIniFile;
ServerURL:pChar;
myHttp:TIdHTTP;
begin
myHttp := TIdHTTP.Create(nil);
updIniFile := TIniFile.Create(ExtractFilePath(paramstr(0))+'update.ini');
oldverstr := PChar(updIniFile.ReadString('update','version','0'));
serverURL:= PChar(updIniFile.ReadString('update','Server',''));
if ServerURL[Length(serverURL)] &lt;&gt; '/' then
serverURL := PChar(String(serverURL) + '/');
ustream :=tmemorystream.Create ;
try
myHttp.Get(serverURL+'update.ini',ustream);
ustream.SaveToFile('newUpdate.ini');
updIniFile := TIniFile.Create(ExtractFilePath(paramstr(0))+'newUpdate.ini');
newverstr := PChar(updIniFile.ReadString('update','version','0'));
result := (newverstr &gt; oldverstr);
except
Raise Exception.Create('Maybe network problem or can`t find update server.');
end;
freeandnil(updIniFile);

if (FileExists(ExtractFilePath(paramstr(0))+'newUpdate.ini')) then
if (not Result) then //version is same.
DeleteFile(ExtractFilePath(paramstr(0))+'newUpdate.ini') ;

freeandnil(ustream);
myHttp.Disconnect;
freeandnil(myHttp);
showmessage('ok');
end;

exports
checkIfNeedUpdate;
begin
end.
 
ini文件和你调用dll的exe是否在同一目录?
 
这一句返回默认值 '' 时,
serverURL:= PChar(updIniFile.ReadString('update','Server',''));
这一句必然会出错
if ServerURL[Length(serverURL)] &lt;&gt; '/' then
 
感激各位,问题在于我的dll中没有shareman,但我的测试文件中用了shareman,所以出错了。
但因为这个dll中没有string,tstrings等参数传入或值返回,所以不抱错我也能理解,但对于
我已把string型的参数及返回都改成了pchar型,为何仍抱错,还有,如果参数是TStrings的话
要怎么处理它才不抱指针错误。如果能解决,马上给分。
 
在dll的应用过程中,一不能用string,二不能用pchar,返回只能用array of char,因为
string是vcl的类,dll调用可以,但在注销内存是会地址错误,pchar是一个动态的指针而
已,所以当function调用完成后很容易地址被改写,所以一定要用数组

以上是我在C++里用的时候总结的,但我想跟d差不多,如果不同请兄弟们更正
 
你的 ParamStr 是什么意思?为什么不直接将 Ini 文件的文件名传递给
DLL?因为如果 ParamStr 无效,无论你怎么改,从 Ini 读出 PChar(NULL)
的数据不是永远都会报错?

一点想法,可能有错,多包涵...
 
有一个千万要记住,你这种情况我也遇过,不过我是比较简单,就是调用的代码,!!!
你不防写出你的调用代码!要记住调用的参数压入格式和dll里面的要一致!!
比如你在dll里是用stdcall,在调用单元声明也要用stdcall,这个提示虽然简单,但我碰过,
就写出来,不过不是的话,千万不要笑我:)
 
ParamStr是Delphi自带的Function.ParamStr(0)的功能是用于得到应用程序的路径。
 
顶部