Dll文件里有一个函数,代码如下:
function Init(FormHandle: THandle): Boolean; stdcall;
var
Databases,Database: IXMLNode;
XMLDocument1 : IXMLDocument ;
strXmlFile : String;
begin
CoInitialize(nil);//注册
MainHandle := FormHandle;
strXmlFile := GetRegistry();//调用此函数行到注册表路径;
if strXmlFile = '' then begin
Result := false;
exit;
end;
strXmlFile := strXmlFile + 'Web\Config\DatabaseConfig.xml';
if(FileExists(strXmlFile) = false) then
begin
MessageDlg ('三星功能创建错误: 找不到数据库配置文件('+strXmlFile+')!', mtError, [mbOK], 0);
Result := false;
exit;
end ;
XMLDocument1 := TXMLDocument.Create(nil);
XMLDocument1.LoadFromFile(strXmlFile);
XMLDocument1.Active := True;
try
Databases := XMLDocument1.DocumentElement.ChildNodes.FindNode('Databases');
Database := Databases.ChildNodes.FindNode('Database');
ID := Database.AttributeNodes.Nodes['ID'].Text;
DataSource := Database.ChildNodes.FindNode('DataSource').Text;
User := Database.ChildNodes.FindNode('User').Text;
Password := Database.ChildNodes.FindNode('Password').Text;
Result := true;
except
Result:=false;
end;
XMLDocument1.Active := false;
end;在.exe程序里调用Dll里的上面这个函数,代码如下:
procedure TForm1.FormCreate(Sender: TObject);
var
result : Boolean ;
begin
result := Init(Form1.Handle);
if result=true then begin
application.MessageBox(pchar('OK'), pchar(application.Title));
end
else begin
application.MessageBox(pchar('Error'), pchar(application.Title));
end;
end;程序出现了如下问题:跟踪调试后发现,所有代码都执行完毕后弹出一个错误对话框:'access violation at 0x00000000:read of address 0x00000000'.
请问这是什么原因呀?是不是用了哪个没有创建的对像呀?我真是晕了。
function Init(FormHandle: THandle): Boolean; stdcall;
var
Databases,Database: IXMLNode;
XMLDocument1 : IXMLDocument ;
strXmlFile : String;
begin
CoInitialize(nil);//注册
MainHandle := FormHandle;
strXmlFile := GetRegistry();//调用此函数行到注册表路径;
if strXmlFile = '' then begin
Result := false;
exit;
end;
strXmlFile := strXmlFile + 'Web\Config\DatabaseConfig.xml';
if(FileExists(strXmlFile) = false) then
begin
MessageDlg ('三星功能创建错误: 找不到数据库配置文件('+strXmlFile+')!', mtError, [mbOK], 0);
Result := false;
exit;
end ;
XMLDocument1 := TXMLDocument.Create(nil);
XMLDocument1.LoadFromFile(strXmlFile);
XMLDocument1.Active := True;
try
Databases := XMLDocument1.DocumentElement.ChildNodes.FindNode('Databases');
Database := Databases.ChildNodes.FindNode('Database');
ID := Database.AttributeNodes.Nodes['ID'].Text;
DataSource := Database.ChildNodes.FindNode('DataSource').Text;
User := Database.ChildNodes.FindNode('User').Text;
Password := Database.ChildNodes.FindNode('Password').Text;
Result := true;
except
Result:=false;
end;
XMLDocument1.Active := false;
end;在.exe程序里调用Dll里的上面这个函数,代码如下:
procedure TForm1.FormCreate(Sender: TObject);
var
result : Boolean ;
begin
result := Init(Form1.Handle);
if result=true then begin
application.MessageBox(pchar('OK'), pchar(application.Title));
end
else begin
application.MessageBox(pchar('Error'), pchar(application.Title));
end;
end;程序出现了如下问题:跟踪调试后发现,所有代码都执行完毕后弹出一个错误对话框:'access violation at 0x00000000:read of address 0x00000000'.
请问这是什么原因呀?是不是用了哪个没有创建的对像呀?我真是晕了。
XMLDocument1 : IXMLDocument ;三个是引用释放, 退出时发生的吧, 可能在这, 也可以显示释放, 在用完后Databases := nil
...
我加了那一句,还是不行呀,还是那个错哟。
不行的话在dll和主程序的第一个unit(一般是.dpr文件)的uses里都加上shareMem
如果是保证运行时dll窗体与主程序融合,一般是把主程序的TApplication或Application.handle作为参数传入dll
跟踪调试到这一句时弹出错误对话框:'Access violation at address 40008EEA in module 'rtl70.bpl'.Read of address 00CEBDA8'和以前报的错不太一样。
重起 delphi
40008EEA 8B02 mov eax,[edx]这是什么呀?
我是这样的:function Init(FormHandle: THandle): Boolean;external 'SamSungDll.dll';
我加了stdcall,程序不再报错了,但是还是有一个问题,当我退出程序时,好点窗体的叉叉按钮时报了错:'Invalid pointer operation'
不加是相当于 fastcall, 对 windows 而言相当于 PASCAL 调用(不太一样, 但相当), 他用 eax 作为第一返回参数
而 dll 用的是 stack, 这个叫 cdecl calling, 对delphi 定义而言就是 stdcall, 取的方法都不一样了
你上面讲的那个MainHandle := FormHandle的确是没有用到。
不管怎么样真是太感谢你了,呵呵,我也是因为工作需要所以才刚刚学习Delphi的,之前一直是用C#开发的,所以Delphi好多东东都不是太懂,以后多多交流,谢谢啦 ……