1

I have a DLL that reads input from a file and writes output to another file. Instead I want to read input from an XML "object" (request) and deliver a response.

Here is the call:

unit gen1;

interface

uses 
  SysUtils;

function genber(Zin: PChar): Integer; export; stdcall;

implementation

uses 
  gen2;

function genber(Zin: PChar): Integer;
begin
  Result := -1;
  try
    IBIS(ZIn);
    Result := 1;
  finally
  end;
end;

end.

And in gen2 I use this:

AssignFile(ZINXML, ZIN);
AssignFile(ZUTXML, 'UT_' + Time + '.XML');

What shall I use instead of AssignFile for reading a XML file?

Delphi Coder
  • 1,723
  • 1
  • 14
  • 25

1 Answers1

1

TL;DR:

Use LoadXMLDocument instead of AssignFile for working with XML files.

Detailed answer:

Example unit:

unit gen2;

interface

uses
  XMLDOC, XMLIntf;

procedure IBIS(const Filename: string);

implementation

procedure IBIS(const Filename: string);
var
  Doc: IXMLDocument;
  data,
  Node: IXMLNode;
begin
  if not FileExists(Filename) then
    exit;

  Doc := LoadXMLDocument(Filename);
  data := Doc.DocumentElement;

  // look for a specific node
  Node := data.ChildNodes.FindNode('Nodename');

  // or loop over all child nodes
  for i := 0 to Data.ChildNodes.Count - 1 do
  begin
    Node := Data.ChildNodes[i];
    // read and process child nodes here...

  end;

  // don't free Doc, data, Node... these are interfaces!
end;

end.

Sources:

Delphi Coder
  • 1,723
  • 1
  • 14
  • 25
  • NOTE: Because this is run inside a DLL you need to init the ActiveX environment, as de XML-DOM uses MS-XML by default, which is COM-based. – R. Hoek Jun 05 '19 at 12:52
  • In addition, you need to initialize ActiveX BEFORE using any XML-routines (like `LoadXMLDocument`) and uninitialized AFTER every reference to the XML document or any DOM interface part has been set to `nil`. So you might want to call it from your DLL-routine `function genber(Zin: PChar): Integer;`! – R. Hoek Jun 05 '19 at 13:01