[ Pobierz całość w formacie PDF ]
.Thedetails of this process are described in the section called "Registration Issues." For now, you need only take note of theClassID assigned to your object because you will need this ID when you try to call the object from another machine, asdescribed in the next section.The act of registering the object is not something you necessarily have to understand because it will occur automaticallywhenever you run the client application of which TMyDCOM is a part.NOTE: The object will be registered repeatedly, whenever you run the program, which ensures that you will findit easy to register the object, while simultaneously requiring very little overhead in terms of system resources.Ifyou move the application to a new location, you can register this change with the system by running it once.Thiscapability guarantees that the old items associated with your CLSID will be erased, and new items will be filled intheir place.Registering a class ID multiple times does not mean that you will end up with multiple items in theRegistry because each registration of a CLSID will overwrite the previous registration.All OLE servers worththeir name provide this service.For example, Word and Excel update the Registry each time they are run.Besides the registration procedure, the other key part of the code generated by the Automation expert is the class definitionfound at the top of the header:class TMyDCom : public TAutoObject{private:public:__fastcall TMyDCom();__automated:};This code has two sections, one called private and the other called automated.In the __automated section, you candeclare methods or properties that you want to call across program or machine boundaries.In other words, any methods orproperties that you declare in this space will automatically be marshaled for you by the underlying IDispatch objectencapsulated by TAutoObject.Consider the following code fragments:class TSimpleDCOM : public TAutoObject{private:public:virtual __fastcall TSimpleDCOM();__automated:file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch27.htm (6 of 59) [10/10/2000 1:15:49 AM] Ch 27 -- Distributed COMAnsiString __fastcall GetName();int __fastcall Square(int A);};AnsiString __fastcall TSimpleDCOM::GetName(){return "SimpleDCOM";}int __fastcall TSimpleDCOM::Square(int A){return A * A;}This object has two methods: one that states the name of the object and one that can square an integer.These two methods aredeclared in the automated section of the object, so they can be accessed from another program via another program.The TSimpleDCOM object exports two methods that IDispatch will automatically marshal for you across application ormachine boundaries.You can go on adding methods to this object as you like.Any data that you want to add to the objectshould go in the private section, and any methods or properties that you don't want to export should also go in theprivate section.All methods that you want to call from inside another application should go in the automated section.You should declare these exported methods as __fastcall.Some limits to the marshaling will be done for you by IDispatch.In particular, the following types are legal to use in thedeclarations for the methods or properties in the automated section:int,float,double,Currency,TDateTime,AnsiString,WordBoolShortStringunsigned shortVariantThe following types are illegal to use in the declarations for the methods or properties in the automated section:arrayschar *file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch27.htm (7 of 59) [10/10/2000 1:15:49 AM] Ch 27 -- Distributed COMvoid *structsFor additional information, see the "Automating properties and methods" section in the online help for the VCL.The apparent limitations created by the lack of support from IDispatch for custom types can be considerably mitigated byan intelligent use of variant arrays.These structures can be so helpful that I have added a section later in this chapter called"Using Variant Arrays to Pass Data" to describe their use.The complete source for a simple DCOM server is shown in Listing 27.3 through Listing 27.6.Notice that OleAuto isincluded in this project.This unit is essential to OLE Automation programming with the VCL.Listing 27.3.The heading for the SimpleObject file from the EasyDCOM project.///////////////////////////////////////// SimpleObject.h// EasyDCOM// Copyright (c) 1997 by Charlie Calvert//#ifndef SimpleObjectH#define SimpleObjectH#include#includeclass TSimpleDCOM : public TAutoObject{private:public:virtual __fastcall TSimpleDCOM();__automated:AnsiString __fastcall GetName();int __fastcall Square(int A);};#endifListing 27.4.The main source file of an OLE Automation object.///////////////////////////////////////// SimpleObject.cppfile:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch27.htm (8 of 59) [10/10/2000 1:15:49 AM] Ch 27 -- Distributed COM// EasyDCOM// Copyright (c) 1997 by Charlie Calvert//#include#pragma hdrstop#undef RegisterClass#include "SimpleObject.h"int Initialization();static int Initializer = Initialization();__fastcall TSimpleDCOM::TSimpleDCOM(): TAutoObject(){}AnsiString __fastcall TSimpleDCOM::GetName(){return "SimpleDCOM";}int __fastcall TSimpleDCOM::Square(int A){return A * A;}void __fastcall RegisterTSimpleDCOM(){TAutoClassInfo AutoClassInfo;file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch27.htm (9 of 59) [10/10/2000 1:15:49 AM] Ch 27 -- Distributed COMAutoClassInfo.AutoClass = __classid(TSimpleDCOM);AutoClassInfo.ProgID = "EasyDCOM.SimpleDCOM";AutoClassInfo.ClassID = "{E2674A60-2DF2-11D0-92C5-000000000000}";AutoClassInfo.Description = "Easiest possible DCOM program";AutoClassInfo.Instancing = acMultiInstance;Automation->RegisterClass(AutoClassInfo);}int Initialization(){RegisterTSimpleDCOM();return 0;}Listing 27.5.The header for the main source file for the EasyDCOM OLE server.#ifndef MainH#define MainH#include#include#include#includeclass TForm1 : public TForm{__published:TLabel *Label1;private:public:virtual __fastcall TForm1(TComponent* Owner);file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch27.htm (10 of 59) [10/10/2000 1:15:49 AM] Ch 27 -- Distributed COM};extern TForm1 *Form1;#endifListing 27.6.The main source file for the EasyDCOM OLE server.#include#pragma hdrstop#include "Main.h"#pragma resource "*.dfm"TForm1 *Form1;__fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner){}This program is meant to be run from a client.As such, it has no controls on it and no public interface other than the OLEobject itself.I do, however, give the main form a distinctive look, as you can see in Figure 27.2.FIGURE 27.2.The main form for the EasyDCOM program.Of course, there is no reason that a single program could not simultaneously have an OLE server interface and a set of standardcontrols.For example, Word and Excel are both OLE servers, and standard applications run through a set of menus and othercontrols.In fact, the same application can work as a server, a standard application, and as a client [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • szamanka888.keep.pl