Automation is a method of automatically controlling one application from within another application, such as the code below.
PRocedure CreateNewWord;
var
WordObj: Variant;
begin
WordObj := CreateOleObject('Word.Basic'); {This function is declared in the ComObj unit}
WordObj.AppShow;
WordObj.FileNew;
end;
This code will open WORD and automatically create a new document (of course, the premise is that WORD is installed on your machine). This seems very interesting and a very useful function. So how can our program also have Can automation functions similar to WORD allow programs developed in any other language to automate our programs? It is very simple to implement it with DELPHI.
This article will explain step by step how to develop a simple automation server with examples.
Create a new ordinary application and save the project as AutoSrv.bpr. Place an Edit control on the main form and save it as MainForm.pas. Here we plan to add the window title, form color, and To automatically control the text of the Edit control (of course, this implements very few functions, but it is enough to explain how to develop an automated server program), add the following code to the main window: (Note: Please add the declarations of these functions and procedures to TForm1 by yourself public area)
function TForm1.GetCaption: string;
begin
result := Self.Caption;
end;
procedure TForm1.SetCaption(ACaption: string);
begin
Self.Caption := ACaption;
end;
procedure TForm1.SetColor(AColor: TColor);
begin
Self.Color := AColor;
end;
procedure TForm1.SetEditText(AText: string);
begin
Self.Edit1.Text := AText;
end;
Then let's add automation functions to this program, click the New Items button, click the ActiveX tab in the pop-up New Items window, select Automation Object, click the OK button, and enter the CoClass Name in the pop-up Automation Object Wizard window. MyAutoSrv, Delphi will automatically generate an AutoSrv_TLB.pas file (class library) and a unit that implements the class library interface class, and save this new unit as AutoSrvClass.pas
Now that this program has become an automated server, let's add automated interface functions to it:
(1) Click the View->Type Libray menu, select the IMyAutoSrv interface in the Type Library Editor, click New Property, select its property as Read|Write, name it Caption, and set the Type to BSTR.
(2) Click New Method, name it SetColor, click the Parameters tab on the right, click ADD to add a parameter to the newly added interface function, set the parameter name to AColor, and set the parameter Type to OLE_COLOR.
(3) Click New Method again, name it SetEditText, add a parameter to it using the above method, set the parameter name to AText, and set the parameter Type to BSTR.
Finally, add the implementation code of the interface function and it’s OK:
Add MainForm in the Uses section of AutoSrvClass.pas and change its code to the following code.
unit AutoSrvClass;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
ComObj, ActiveX, AutoSrv_TLB, StdVcl, MainForm;
type
TMyAutoSrv = class(TAutoObject, IMyAutoSrv)
protected
function Get_Caption: WideString; safecall;
procedure Set_Caption(const Value: WideString); safecall;
procedure SetColor(AColor: OLE_COLOR); safecall;
procedure SetEditText(const AText: WideString); safecall;
end;
implementation
uses ComServ;
function TMyAutoSrv.Get_Caption: WideString;
begin
Result := Form1.GetCaption;
end;
procedure TMyAutoSrv.Set_Caption(const Value: WideString);
begin
Form1.SetCaption(Value);
end;
procedure TMyAutoSrv.SetColor(AColor: OLE_COLOR);
begin
Form1.SetColor(AColor);
end;
procedure TMyAutoSrv.SetEditText(const AText: WideString);
begin
Form1.SetEditText(AText);
end;
initialization
TAutoObjectFactory.Create(ComServer, TMyAutoSrv, Class_MyAutoSrv,
ciMultiInstance, tmApartment);
end.
Run this program once and it will automatically register as an automation server. You can find its related registration information under the HKEY_CLASSES_ROOT primary key in the registry.
The above demonstrates how to develop an automation server, which we will call here.
Create a new program, add a Button, and declare a Variant variable in its VAR area: AutoSrv: variant
Then add the following code to Button1.
procedure TForm1.Button1Click(Sender: TObject);
begin
AutoSrv := CreateOleObject('AutoSrv.MyAutoSrv'); {This string is the project name of the automation server plus CoClass Name}
Self.Caption := AutoSrv.Caption;
AutoSrv.Caption := 'HEHE';
AutoSrv.SetColor(CLRed);
AutoSrv.SetEditText('HAHA');
end;
The CreateOleObject function will return an IDispatch type interface. It is the IDispatch interface that allows our program to make late connections to calls to the automated server interface. For example, if we add an AutoSrv.Hello above, the program can also be compiled and passed. But an error will occur during execution. The execution efficiency of using Variant will be slower than using the interface declaration directly.
Run and click the button, you can see that the automated service program is loaded, and the form color and the string in EDIT1 are set according to our code. Haha, is it very simple?