XML writting error

96 views
Skip to first unread message

Petr Šikola

unread,
Aug 19, 2022, 1:35:57 AM8/19/22
to innosetup
Hi, I have a problem editing an xml file using ole automation. The first node addition works, on the second or third call I end up with error 0xC0000005 (setup exit code).

Here is my sample:

function WriteXmlAttrib(const filename, xmltag, atribute, value: string): boolean;
var
  XMLDocW, editNodeW, editNodeW2, parentNodeW: Variant;
  s, s1, s2, s3: string;
begin
  Result := false;
  try

    XMLDocW := CreateOleObject('MSXML.DOMDocument'); //CreateOleObject('Msxml2.DOMDocument.6.0'); //same result
    XMLDocW.async := False;
    XMLDocW.resolveExternals := False;
    XMLDocW.load(filename);
    if XMLDocW.parseError.errorCode <> 0 then
      //RaiseException('Error on line ' + IntToStr(XMLDocW.parseError.line) + ', position ' + IntToStr(XMLDocW.parseError.linepos) + ': ' + XMLDocW.parseError.reason);
      Exit;
    try
      try
      //XMLDocW.setProperty('SelectionLanguage', 'XPath');
      except
      end;

      editNodeW := XMLDocW.selectSingleNode(xmltag);

      s2 := editNodeW.Text;
      editNodeW.setAttribute(atribute, value);
    except
      s := xmltag;
      s3 := s;
      editNodeW := null;
      parentNodeW := XMLDocW.documentElement;

      while pos('/', s) > 0 do
      begin
        if s1 = '' then
          s1 := copy(s, 1, pos('/', s) - 1)
        else
          s1 := s1 + '/' + copy(s, 1, pos('/', s) - 1);
        editNodeW := XMLDocW.selectSingleNode(s1);

        try
          s2 := editNodeW.Text;
        except
          s2 := s;
          s2 := copy(s2, 1, pos('/', s2) - 1);
          editNodeW := null;
          editNodeW := XMLDocW.createElement(s2);
          parentNodeW.appendChild(editNodeW);
        end;                            
        parentNodeW := editNodeW;
        editNodeW := null;
        s3 := s1;
        s := copy(s, pos('/', s) + 1, 200);
      end;

      parentNodeW := null;
      parentNodeW := XMLDocW.selectSingleNode(s3);

      s1 := s;
      if pos('[@', s) > 0 then
        s1 := copy(s1, 1, pos('[@', s1) - 1);
      editNodeW2 := null;
      editNodeW2 := XMLDocW.createElement(s1);

      if pos('[@', s) > 0 then
      begin
        s := copy(s, pos('[@', s) + 2, 200);
        s1 := copy(s, 1, pos('=', s) - 1);
        s := copy(s, pos('''', s) + 1, 200);
        s := copy(s, 1, pos('''', s) - 1);
        editNodeW2.setAttribute(s1, s);
      end;
      editNodeW2.setAttribute(atribute, value);
      parentNodeW.appendChild(editNodeW2);
    end;
    XMLDocW.Save(filename);                            
    Result := True;
  finally
  end;
end;


and theirs calling:

//allways ok
  WriteXmlAttrib(ExpandConstant('{tmp}\abc.xml'), 'configuration/appSettings/add[@key=''Key3'']', 'value', 'key 3 value');

//sometime ok, sometime crash
  WriteXmlAttrib(ExpandConstant('{tmp}\abc.xml'), 'configuration/appSettings/add[@key=''Key4'']', 'value', 'key 4 value');

//allways crash
  WriteXmlAttrib(ExpandConstant('{tmp}\abc.xml'), 'configuration/appSettings/add[@key=''Key5'']', 'value', 'key 5 value');


I found the similar problems here https://stackoverflow.com/questions/26034815/inno-setup-error-255-while-writing-xml-files-with-msxml-6 or here https://stackoverflow.com/questions/16482070/adding-nodes-to-xml-with-dom-in-inno-setup-strange-problems.

Is there any way to solve this other than using an external dll or an application (with the same code of method WriteXmlAttrib) that handles the write?

Thanks for sugestions
PS

Eivind Bakkestuen

unread,
Aug 19, 2022, 4:39:23 PM8/19/22
to inno...@googlegroups.com
Using the "always crash" call, debug your code. Work out exactly what the contents of your variables are, and where exactly it crashes. Anything pop out then?

--
You received this message because you are subscribed to the Google Groups "innosetup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to innosetup+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/innosetup/4cb9ec8d-b916-4a97-8230-3360f69db2ean%40googlegroups.com.

Petr Šikola

unread,
Aug 23, 2022, 6:15:36 AM8/23/22
to innosetup
Hi, of course I try it, but the setup ends silently up with code 0xC0000005.
The order of the calls of  WriteXmlAttrib  does not matter, the setup is always terminated at the second or subsequent call. I can put the full test script here if you want. The problem is with the repeated use of the appendChild method of the xmlnode ole object.

Regards
PS

Dne pátek 19. srpna 2022 v 22:39:23 UTC+2 uživatel eivind.b...@gmail.com napsal:
Reply all
Reply to author
Forward
0 new messages