我撰写一个DLL檔, 在这当中要解析一个xml档案, 将其中的节点资料取出后回传, 在DLL檔中我以MessageBox来检视解析后的资料是没有问题的, 但最后要回传时就会出现错误, 请问是那里错了?DLL 檔
unit exportUnit;interface
uses
Windows,SysUtils,activex,Classes,XMLDoc,XMLIntf; function ParseXML:pchar; stdcall;implementationfunction ParseXML:pchar; stdcall;
var
xml:string;
doc:IXMLDocument;
iTop,xNode:IXMLNode;
longstr:string;
i:integer;
begin
xml:='<?xml version="1.0" encoding="big5"?><INPUTXML><PIDDATA><PID>X12345</PID><NAME>ABCDE</NAME></PIDDATA></INPUTXML>';
try
CoInitialize(nil); doc:=TXMLDocument.Create(nil);
doc.LoadFromXML(xml); longstr:='0';
iTop:=doc.ChildNodes['INPUTXML'].ChildNodes['PIDDATA'];
for i:=0 to iTop.ChildNodes.Count-1 do begin
xNode:=iTop.ChildNodes.Nodes[i];
if xNode.NodeName='PID' then
longstr:=longstr+','+xNode.NodeValue;
if xNode.NodeName='NAME' then
longstr:=longstr+','+xNode.NodeValue;
end; MessageBox(0,pchar('回传值:'+longstr),'This is MyDLL',mb_OK); result:=PChar(longstr);
finally
xNode:=nil;
iTop:=nil;
doc:=nil;
CoUnInitialize;
end;
end;end.我另外写一支程序测试去呼叫
procedure TForm1.Button2Click(Sender: TObject);
begin
Edit3.Text:=string(ParseXML);
end;每次执行都出现
Access violation at address 00235A76 in module ‘MyTest.dll’. Read of address 0148EAA8.
这样的错误讯息, 而这个错误是在DLL最后要返回时才出现错误的,不知道是什么原因,请各位帮帮忙.
unit exportUnit;interface
uses
Windows,SysUtils,activex,Classes,XMLDoc,XMLIntf; function ParseXML:pchar; stdcall;implementationfunction ParseXML:pchar; stdcall;
var
xml:string;
doc:IXMLDocument;
iTop,xNode:IXMLNode;
longstr:string;
i:integer;
begin
xml:='<?xml version="1.0" encoding="big5"?><INPUTXML><PIDDATA><PID>X12345</PID><NAME>ABCDE</NAME></PIDDATA></INPUTXML>';
try
CoInitialize(nil); doc:=TXMLDocument.Create(nil);
doc.LoadFromXML(xml); longstr:='0';
iTop:=doc.ChildNodes['INPUTXML'].ChildNodes['PIDDATA'];
for i:=0 to iTop.ChildNodes.Count-1 do begin
xNode:=iTop.ChildNodes.Nodes[i];
if xNode.NodeName='PID' then
longstr:=longstr+','+xNode.NodeValue;
if xNode.NodeName='NAME' then
longstr:=longstr+','+xNode.NodeValue;
end; MessageBox(0,pchar('回传值:'+longstr),'This is MyDLL',mb_OK); result:=PChar(longstr);
finally
xNode:=nil;
iTop:=nil;
doc:=nil;
CoUnInitialize;
end;
end;end.我另外写一支程序测试去呼叫
procedure TForm1.Button2Click(Sender: TObject);
begin
Edit3.Text:=string(ParseXML);
end;每次执行都出现
Access violation at address 00235A76 in module ‘MyTest.dll’. Read of address 0148EAA8.
这样的错误讯息, 而这个错误是在DLL最后要返回时才出现错误的,不知道是什么原因,请各位帮帮忙.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货