Inno Setup crashes in appendChild msxml

2019-06-25 12:54发布

问题:

I want to modify xml file in Inno Setup - but installer crashes. I tried different things, and as result got sample code with problem

procedure testXml();
var
  xmlDocLocal, nodeLocal: Variant;
begin
try
   xmlDocLocal := CreateOleObject('MSXML2.DOMDocument');
   xmlDocLocal.async := False;
   xmlDocLocal.resolveExternals := False;
   xmlDocLocal.loadXML('<root></root>');
   nodeLocal := xmlDocLocal.CreateElement('element1');
   xmlDocLocal.documentElement.appendChild(nodeLocal);
except
end;

end;

During second call, installer crashes on the appendChild method. What am I doing wrong ?

回答1:

Three ideas: first, we're using InnoSetup, but for us the OleObject needs to be created with another string ending with the specific version 6.0:

try
    XMLDoc := CreateOleObject('MSXML2.DOMDocument.6.0');
except
    RaiseException('Please install MSXML first.'#13#13'(Error ''' + GetExceptionMessage + ''' occurred)');
end;

Second idea: try adding an xml header to the XML string you have in your code. Like this:

xmlDocLocal.loadXML('<?xml version="1.0" encoding="UTF-8" ?><root></root>');

Third idea: try checking for errors (as I already showed in the first snippet). That might give you a pretty good idea what goes wrong. This is how we do it (and it works):

XMLDoc.load(XMLFileName);
if XMLDoc.parseError.errorCode <> 0 then
  XMLDoc.load(XMLFileName2);
if XMLDoc.parseError.errorCode <> 0 then
  RaiseException('Error on line ' + IntToStr(XMLDoc.parseError.line) + ', position ' + IntToStr(XMLDoc.parseError.linepos) + ': ' + XMLDoc.parseError.reason);

Hope this helps you. Hard to solve an unknown issue ;-)



标签: inno-setup