[ Pobierz całość w formacie PDF ]
.ChangeColor;varIMyServer: IFirstServer;beginIMyServer := CoFirstServer.Create;IMyServer.ChangeColor;end;Unless the server is already active, a copy of the program is created, and the color ischanged, but then the server is immediately closed as the interface-typed object goes out ofscope.The alternative approach I ve used in the TLibCli example is to declare the object as afield of the form and create the COM objects at start-up, as in this procedure:procedure TClientForm.FormCreate(Sender: TObject);beginIMyServer := CoFirstServer.Create;end;With this code, as the client program starts, the server program is immediately activated.Atthe program termination, the form field is destroyed and the server closes.A further alternativeCopyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com 2874c20.qxd 7/2/01 4:43 PM Page 866866 Chapter 20 " From Automation to COM+is to declare the object in the form, but then create it only when it is used, as in these twocode fragments:// MyServerBis: Variant;if varType (MyServerBis) = varEmpty thenMyServerBis := CoFirstServer.Create;MyServerBis.ChangeColor;// IMyServerBis: IFirstServer;if not Assigned (IMyServerBis) thenIMyServerBis := CoFirstServer.Create;IMyServerBis.ChangeColor;NOTEA variant is initialized to the varEmpty type when it is created.If you instead assign the valuenull to the variant, its type becomes varNull.Both varEmptyand varNullrepresent variantswith no value assigned, but they behave differently in expression evaluation.The varNullvalue always propagates through an expression (making it a null expression), while thevarEmpty value quietly disappears.The Server in a ComponentWhen creating a client program for our server or any other Automation server, we can use a bet-ter approach, namely, wrapping a Delphi component around the COM server.Actually, if youlook at the final portion of the TlibdemoLib_TLBfile, you can find the following declaration:// OLE Server Proxy class declarationTFirstServer = class(TOleServer)privateFIntf: IFirstServer;FProps: TFirstServerProperties;function GetServerProperties: TFirstServerProperties;function GetDefaultInterface: IFirstServer;protectedprocedure InitServerData; override;function Get_Value: Integer;procedure Set_Value(Value: Integer);publicconstructor Create(AOwner: TComponent); override;destructor Destroy; override;procedure Connect; override;procedure ConnectTo(svrIntf: IFirstServer);procedure Disconnect; override;procedure ChangeColor;property DefaultInterface: IFirstServer read GetDefaultInterface;property Value: Integer read Get_Value write Set_Value;publishedproperty Server: TFirstServerProperties read GetServerProperties;end;Copyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com 2874c20.qxd 7/2/01 4:43 PM Page 867Writing an OLE Automation Server 867This is a new component, derived from TOleServer, that the system registers in the Registerprocedure, which is part of the unit.If you add this unit to a package, the new server compo-nent will become available on the Delphi Component Palette.You can also import the typelibrary of the new server (with the Project ¢' Import Type Library menu command), add theserver to the list (by clicking the Add button and selecting the server s executable file), and installit in a new or existing package.The component will be placed in the Servers page of the Palette.The Import Type Library dialog box indicating these operations is visible in Figure 20.7.FI GURE 20.7:The Import Type Librarydialog box can be used toimport an Automationserver object as a newDelphi component.I ve created a new package, PackAuto, available in the directory of the TlibDemo project.In this package, I ve added the directive LIVE_SERVER_AT_DESIGN_TIME in the Directories/Conditionals page of the Project Options dialog box of the package.This enables an extrafeature that you don t get by default: at design time, the server component will have an extraproperty that lists as subitems all the properties of the Automation server.You can see anexample in Figure 20.8, taken from the TLibComp example at design time.WARNINGThe LIVE_SERVER_AT_DESIGN_TIME directive should be used with care with the most com-plex Automation servers (including programs such as Word, Excel, PowerPoint, and Visio).Infact, this setting requires the application to be in a particular mode before you can use someproperties of their automation interfaces.For example, you ll get exceptions if you touch theWord server before a document has been opened in Word.That s why this feature is not activeby default in Delphi it s problematic at design time for many servers [ Pobierz caÅ‚ość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • funlifepok.htw.pl
  •