O'Reilly logo

Python Programming On Win32 by Mark Hammond, Andy Robinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Instantiating the Server

We declared a variable of type Variant in the main form to hold the COM server. Delphi supports Variants purely in order to support COM, although they go somewhat against the grain in Delphi programming where the type of every variable is specified. The Delphi code to instantiate the server is as follows:

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  try
    BookServer := CreateOleObject('Doubletalk.BookServer');
    StatusBar1.SimpleText := 'BookServer running';
  except
    MessageDlg(
      'An instance of the "Doubletalk.BookServer" COM class ' +
      'could not be created. Make sure that the BookServer application ' +
      'has been registered using a  command line.  If you have modified ' +
      'the source of the server, make very sure all public methods and '+
      'attributes are spelled correctly',
      mtError, [mbOk], 0);
    Application.Terminate;
  end;
end;

As with Visual Basic, one line does the work.

Calling simple methods

Delphi does exactly as good a job as VB as far as passing simple arguments is concerned. The following example, behind the File Open menu item, shows how to call methods of the BookSet with integer and string arguments:

procedure TfrmMain.Open1Click(Sender: TObject); {prompt for a filename and ask BookSet to load it} var trancount: integer; filename: string; begin if OpenDialog1.Execute then begin filename := OpenDialog1.FileName; BookServer.load(OpenDialog1.FileName); trancount := BookServer.Count; StatusBar1.SimpleText := Format('Loaded file %s, %d transactions', ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required