               20 

     OLE 

      
    
       OLE 

        OLE. 

        

             Delphi. 

        

        (in-process)   
(out-of-process)       Delphi 
ActiveX (DAX). 

       

            
   Delphi    . 

       Windows,  Microsoft   
  ,      
 ,   .  ,   
      ,    
   .  ,      
 ;          
  ,    . 
       ,        
  ()    
(),  Microsoft   Windows   
 (  Distributed ).      
      . 
            
 Microsoft        
 OLE.          
        Delphi. 
        . 
       OLE 
       
       
         
       OleVariant   OleVariant 
    (    
        
    !   

    466 


       OLE 

      .  OLE,   ,   
    ,   
   DLL   ( )  
   . 
       ,    Delphi,    , 
  ,    . 
      ,       
 ,    IDispatch  
,      ,   
,  ,    IDispatch.  
          
 .  ,   IDispatch   
    Delphi      
.    ,     
      Delphi. 
         . - 
            
       . 

      

        : 
      (in-process); 
      (out-of-process),  . 

      

     (out-of-process)    ,   
     .    
 ,        
,      .  
   ^  ,   
 ,   . Microsoft Word  Windows  
  .   Word     
 .                \ 

       
     ,   ,   
 . 


      

     (in-process)     
^  IDLL^ ,    
     . 
    .  ,  
         
   ,    .  
         , 
    . 
    ,     ,     
  ,       
 . 
    /         
,   .  . . 

     20.  OLE                                        467 


       

        ,  ,  
    .       
  .     ?   
,   ,    
,      .   , 
         4  ! 

         

              , 
  ActiveX   .    
       , 
' VIlual Basic,      .  
,       Delphi (Type 
Library Editor).          
 ,    .   
  ,   ;     
 . 
       ,  Delphi     
  ActiveX   -,   
Delphi        . 

     
         ,   -  
,  . 

       . 
         ,   , 
 ( dispinterface)     
( CoClass). 
            . 
            Type Library (TLB)  
Object Type Library (OLB),  ^ ,   -  
DLL-, ,   ,    OLE.   
Deiphi    TLB,      
   . 
          ,  
  . 
    1.   File^Open. 
    2.   File of type  Type Library. 
    3.        . 
    4.    Open,       
 Type Library Editor      .          
         Delphi     
.  ^_     Delphi    
   ,       
.   ,     Delphi   
       .   
          
.   ,  Delphi ,     
 ,    . 

        

    Delphi      .   
,         
      Delphi.    ,  
      ,   
   Delphi CreateOleObject. 
     CreateOleObject      
ProgID,    ,    IDispatch 
 .   ProgID    
,      .  
  

    468                                            V.   OLE 

             
Microsoft Word   CreateOleObject.    
   Microsoft Word Word. Basic.  
      Word. 

    Uses ComObj ; 
    var 
    WordBasicObject: OleVariant; 
    procedure TFormI.ButtonlClick(Sender: Tobject); 
    begin 
    WordBasicObject := CreateOleObject('Word.Basic'); 
    WordBasicObject.Insert("inserting text into Word via Automation'); 
    end; 

    He ,      - . 
 ,    CreateOleObject   uses   
 ComObj.    WordBasicObject  OleVariant. 
 OleVariant    ,     
   OLE,       .  
    OleVariant      
IDispatch,      OLE. 
        CreateOleObject     
 .    Windows CLSIDFromProgID,  
  ProgID,     (CLSID). 
  CLSID   Windows CoCre:. atelnstance 
    IDispatch  .  
Result     IDispatch    
.     CreateOleObject. 

    Function CreateOleObject(const CiassName: string): IDispatch; 
    var ClassID: TCLSID; 
    begin ClassID := ProgIDToClassID(CiassName); 
    01eCheck(CoCreateInstance(ClassID, nil, CLSCTS_INPROC_SERVER or 
CLSCTX_LOCAL_SERVER, IDispatch, Result)); 
    end; 

      :  CLSID  ProgID   
       Windows. 
          DCOM   
createRemoteComObject,     ComObj. 
    ,       Word .Basic,  
        .   
     Insert    ,   
    Microsoft Word. ,    
      Word (    
 ).        Word  
   Microsoft Word  Windows. 

    |   
        ?     
 ,     (   
  )  .    
   ,  .  , , 
  WordBasicObject  ,    ' 
  ButtonlClick. ,    
  ( )    
   .    ,  
,     OleVariant     
, Delphi      
  IDispatch,        
OleVariant.  ,       
      Microsoft Word. , 
   WordBasicObject ,   
    ,     , 
   . 

     20.  OLE                                       469 

        ,  Delphi ,    
   ,    .    Delphi 
   OleVariant. Delphi ,    
    ,    
Delphi     .  , Delphi ,   
 OleVariant         
  . ,     
  OleVariant      IDispatch, 
   .    ( 
),     (.),      
        . 

       

            
 Delphi.        
,    . ,  
      Sound   
Beeper,       .     
Beeper   OleVariant. 

    Beeper.Sound := MB_ICONEXCLAMATION; 

        ,  
    Delphi,       
 . ,    Oracle (  
 )  ,      , 
    .    , 
  . 

    var 
    DBMSServer : OleVariant; 
    begin 
    DBMSServer.OpenDatabase['US.borland.corn', 'SysAdmin / Password', 0] 

            , 
          ,   
      . ,   , 
  .        
   Beeper,     . 

    Beeper.SoundBeep[MB_ICONEXCLAMATION] ; 

         ( )    , 
       . 
            
  ,      ". 
   ,'      
  .          
     Byte 
     Integer 
     Smallint 
     Currency 
     Double 
     Single 
     WideString                                                               
    I TDateTime 
     WordBool 
     OleVariant                                                               
            '' 
          Delphi    
 .      
OLE,     ,     
      ( Delphi  
OleVariant).  ,    ,   
     ,   , 
    OLE,      
 ,      .   OLE 
       (  
 IStorage  IDataObject),       
 .         
( ),       . 

    470                                           V.   OLE 


       

        ,    
   ,    
   . ,     
    Delphi,  ,   .  
   . 

    SomeObject.OpenFile('noname.dat', 'File Description Text'); 

     Delphi   ,     
   64 .  ,     
  65 ,     . 
     ,   
  MaxDispArgs   ComObj.     
,    '  
. (  ,     64   
   .) 

       

           OLE 
     ,    
   .      ,  
   . ,     
 ,   Rectangle.  Rectangle  
  .      Basic 
  ,   . 

    Rectangle [.Color = number] [,.Left = number] [,.Top = number] 
    4> [,.Width = number] [,.Height = number] 

    ,        
   .  Rectangle     
     ,     , 
 100 ,   . 

    SomeObject.Rectangle(cIRed, , , 100, 100); 

       ,        
20, 40  ,   ,   . 

    SomeObject.Rectangle(, 20, 40); 

    ,         ,  
       . 

       

      ,    OLE,   
 .   ,     , 
    ,    , 
   . ,    
     ,  100 ,  ,  
  ,    . 

    SomeObject.Rectangle(Color = cIRed, Width = 100, Height = 100); 

    ,        . 
          ,  
   20.1. , 
     ,    
aBTO

    Word.Basic,   Microsoft Word  

    This example demonslrales how loconnecllolheWo.d Base ; automation 
ob]eclwilhinWo(d (01 Windows. The text you see  , here wilbe copied oveilo new 
Wold document and Ihen printed^ toeeie                                    
                                                       homWoid foi Windows via 
OLE Automation                : 
    Windows 7.0,     TMemo   
    Word    .                     
                  I 
    . 20.1.                                                                 
                              ! 
    . 20.1.  ,    word. 
Basic  I_______________ 


    |   20.  OLE                                       471 


     20.1. \UDELPHl3\CHP20\woRDCLNT\CLiENTFM. PAS.  
    Word. Bas ic   Word  Windows 

    unit ClientFm; 
    interface 
    uses 
    Windows, Messages, SysUtiis, Classes, Graphics, Controls, Forms, Dialogs, 
    Buttons, StdCtrls, ExtCtrls; 
    type 
    TWordClinetForm = class(TForm) 
    Panell: TPanel; 
    Memol: TMemo; 
    CloseButton: TSpeedButton; 
    CopyButton: TSpeedButton; 
    PrintButton: TSpeedButton; 
    procedure CopyButtonClick(Sender: TObject); 
    procedure CloseButtonClick(Sender: TObject); 
    procedure PrintButtonClick(Sender: TObject); 
    private 
    ( Private declarations ) 
    public 
    WordBasicObject : OleVariant; 
    end; 
    var 
    WordClinetForm: TWordClinetForm; 
    implementation 
    ($R *.DFM} 
    Uses ComObj ; 
    function HandleException(E : Exception): Boolean; 
    // 
    // .     
    //               OLE. 
    // 
    const 
    WordError = "Error connecting to Word for Windows - '; 
    begin 
    Result := True; 
    if (E is EOleError) or (E is EOleSysError) or 
    (E is EOleException) then 
    ShowMessage(WordError + E.Message) 
    else 
    Result := False; //    . 
    end; 
    procedure TWordClinetForm.CopyButtonClick(Sender: TObject); 
    // 
    // .    Word  Windows  
    //             Memol   
    //           Word. 
    // 
    var 
    I : Integer; 
    begin try 
    //     Word  Windows?  , 
    //   . 


    472                                            V.   OLE 


    if VarType(WordBasicObject) <> varDispatch then 
    WordBasicObject := CreateOleObject('Word.Basic'); 
    WordBasicObject.FileNew; 
    for I := 0 to Memol.Lines.Count - 1 do 
    WordBasicObject.Insert( Memol.Lines[I] ); 
    except 
    on E : Exception do 
    if not HandleException(E) then Raise; 
    end; 
    end; 
    procedure TWordClientForm.CloseButtonClick(Sender: TObject); 
    // 
    // .    Word,    
    //          ,   .    
    //          TWordClientForm .     
    //          Word    . 
    // 
    begin 
    try 
    //     Word  Windows?  ,   
    //  . 
    if VarType(WordBasicObject) = varDispatch then 
    WordBasicObject.FileClose (2); // 1) /. 2)  . 
    Close; 
    except 
    on E : Exception do 
    if not HandleException(E) then Raise; 
    end; 
    end; 
    procedure TWordClinetForm.PrintButtonClick(Sender: TObject); 
    // 
    // .    Word,   
    //               
    //          Word  Windows. 
    // 
    begin 
    try 
    //     Word  Windows? 
    //  ,    . 
    if VarType(WordBasicObject) <> varDispatch then 
    raise EOleError.Create('No connection established'); 
    WordBasicObject.FilePrint; 
    except 
    on E : Exception do 
    if not HandleException(E) then Raise; 
    end; 
    end; 
    end. 


     

       Word        
   Word. Basic. 


       OLE 

               
variant      .  
,      variant. 

     20.  OLE                                       473 


        OLE 

     OleVariant        (, 
 )  Delphi.   OieVariant   TVarData, 
   System.     
TVarData. 

    TVarData = record 
    VType: Word; 
    Reservedl, Reserved2, Reserved3: Word; 
    case Integer of 
    varSmallint: (VSmallint: Smallint); 
    varlnteger:  (VInteger: Integer); 
    varSingle:   (VSingle: Single); 
    varDouble:   (VDouble: Double); 
    varCurrency: (VCurrency: Currency) ; 
    varDate:     (VDate: Double); 
    varOleStr:   (VOleStr: PWideChar); 
    varDispatch: (VDispatch: Pointer) ; 
    varError:    (VError: Integer) ; 
    varBoolean:  (VBoolean: WordBool) ; 
    varUnknown:  (VUnknown: Pointer) ; 
    varByte:     (VByte: Byte); 
    varString:   (VString: Pointer) ; 
    varArray:    (VArray: PVarArray) ; 
    varByRef:    (VPointer: Pointer); 
    end; 


       Delphi   

     OleVariant   Delphi      
 OLE.  OLE   variant,   
  Microsoft Visual Basic    ,  
        
   . ,    
,         , 
   Delphi.       
 variant   ,    .   
   ,   variant    
,       |       
Delphi       i   variant.  
     Delphi,   variant 
- ^1        
.                              , 
       ,  OleVariant       , 
   ,    (   ). 
,   OleVariant     
.    Delphi  ,   . 

    V := 25; 

         Delphi ,   
,   .     . 

    TVarData(V).Type := varlnteger; 
    TVarData(V).VInteger := 25; 

           ,    
      OleVariant,  
    . ,  
        
,       .    
        . 

    TVarData(V).VType := varError; 
    //       . 
    ServerObj ect.Method(V) 

     . 20.1     OleVariant,  
   ,    OleVariant. 

    474                                            V.   OLE 


     20.1.    OleVariant,    System 

                        Delphi 

    varEmpty	  	 
    varNull	 Null	 
    varByte	8-   	Byte 
    varSmallint	16-   	Smallint 
    varlnteger	32-   	Integer 
    varSingle	     	Single 
    varDouble	     	Double 
    varCurrency	   	Currency 
    varDate	   	TDateTime 
    varOleStr	       JU^J^DE,	WideString 
    varString	      Pascal	AnsiString 
    varDispatch	    OLE	   iDispatch 
    varError	   	 
    varBoolean	16-  	WordBool 
    varVariant 
    varUnknown	    OLE	   I Unknown 
    

     variant,    

          ,    
   .  varEmpty  ,    
 .      varNull.  
 OleVariant,      ,  
    .      
,  Delphi    , Null  
Unassigned,   varNull  varEmpty . 

     variant,   

         OLE    varOleStr. 
 OLE (varOleStr)     Delphi (AnsiString) 
,    OLE       ,   
  Delphi   . ,  OLE   
   Delphi (AnsiString),   
  varString.      variant, 
  Delphi     AnsiString   
 variant.  varString     ,  
 Delphi.     varString,     
   varString     
 varOleStr. 

     variant,   

     varDispatch   variant,   
   IDispatch. Delphi ,   variant, 
    varDispatch,  ,    
 IDispatch.  varUnknown   variant,  
     lUnknown.    
         Querylnterface. 

        variant 

     . 20.2    ,    
    variant.     
  . 

     20.  OLE                                       475 


     20.2.   ,    System 

            

    varTypeMask	  (SOFFF)     
    varArray	, ,    variant  
    varByRef	, ,   :    
   


     variant,     

         ,      
  16-   variant.     varByRef 
  ( OR)    VType,     
.      variant,    
 ( AND)   VType   varTypeMask,   . 

    if (TVarData(V).VType and varTypeMask) = varDispatch then... 


     variant,   variant 

     varVariant  varArray    .  varVariant 
      variant.    
varArray     -   ( 
varVariant), ,   OleVariant   ,  
     VType. ,     
,   V     Integer. 

    If TVarData(V).VType = (varlnteger and varArray) then... 

            variant. 

       variant 

     . 20.3         
variant.      System. 

     20.3.      variant,   
 System 

           

    VarClear	   OleVariant  Unassigned 
    VarCopy	  -  -.  
   OleVariant 
    VarCast	  - OleVariant   
 varXXXX      - OleVariant. 
 varXXXX    varArray  varByRef 
    VarType ^-ni	-   varXXXX  .  12 
  VType     OleVariant. 
      VType  TVarData 
    VarAsType	 - OleVariant    
varXXXX    .     
   varArray  varByRef 
    VarIsEmpty	  True,    OleVariant 
  Unas-signed (varEmpty) 
    VarIsNull	  True,    OleVariant 
  Null (varNull) 
    VarToStr	  ,    ,  
      OleVariant  .   
         
     OleVariant 
    VarFromDateTime	   OleVariant,   
     TDateTime 
    VarToDateTime	   OleVariant    TDateTime 


    476  V.   OLE 

     ,   VarType.    
TVarData     variant   
 VarType.   ,     
 Word. Basic,   VarType  , 
     .     
  ,      
  varDispatch. 

    If VarType(WordBasicObject) = varDispatch then 
    WordBasicObject.FileClose(2); // 1 -   . 
    // 2 -   . 
    ,        VarIsEmpty,   
Delphi     OleVariant  varEmpty. 
    If not VarIsEmpty(WordBasicObject) then 
    WordBasicObject.FileClose(2); // 1 -   . 
    // 2 -   . 


     OleVariant ( ) 

      ,        
,    OleVariant.   OleVariant   
  OleVariant,      , 
  16-  TVarData.     
  OleVariant       OleVariant, 
 . ,        
  OleVariant,      .   
  ,   Visual Basic,  
        OLE.  
,          
OleVariant  ,    .   
  OleVariant   VarArrayCreate,  
  . 

    function VarArrayCreate(const Bounds: array of Integer; VarType: Integer): Variant; 

            
VarArrayOf,   ,   . 

    function VarArrayOf(const Values: array of Variant): Variant; 

     VarArrayCreate       
  OleVariant,       
,          
  OleVariant,     OleVariant.  
     OleVariant    
  . 

    Procedure TFormI.VarArrayCreateButtonClick(Sender: TObject); 
    var 
    V: OleVariant; 
    begin 
    V := VarArrayCreate([0, 4], varVariant) ; 
    V[0] := 1; 
    V[1] := 1234.5678; 
    V[2] := 'Using Delphi 3': 
    V[3] := True; 
    ShowMessage(V[2]); //   'Using Delphi 3'. 
    end; 

     VarArrayOf     VarArrayCreate.  
        
 OleVariant    .    
  VarArrayOf     OleVariant. 

    Procedure TFormI.VarArrayOfButtonClick(Sender: TObject); 
    var 
    V: OleVariant;                                                       .ii 
    begin                                                            I 
    V := VarArrayOf([1, 10, 100, 1000]); 
    ShowMessage(V[2]); //   '100'. 
    end; 


     20.  OLE                                       477 

            
OleVariant,   , ,    
   OleVariant,      
     OleVariant.    
     ,     
   OleVariant. 

    Procedure TFormI.MultiDimensionVariantArrayButtonClick(Sender: TObject); 
    var 
    V: OleVariant; 
    begin 
    V := VarArrayCreate ( [1, 2], varVariant); 
    V[1] := VarArrayOft[1, 10, 100, 1000]); 
    V[2] := VarArrayOf([1, 20, 200, 2000]); 
    ShowMessage(V[2][3]); // - '2000'. 
    end; 

     . 20.4   ,   Delphi 
     OleVariant. 

    1  20.4.      OleVariant, 
   System 

             

    VarArrayRedim          OleVariant   
  .     . 
       varEmpty 
    VarArrayDimCount      
    VarArrayLowBound        
   OleVariant 
    VarArrayHighBound       
   OleVariant 
    VarArrayLock             
  OleVariant    
    VarArrayUnLock       ,    
  VarArrayLock 
    VarArrayRef         OleVariant  varByRef,   
  OleVariant 
    VarIsArray            True,    
 OleVariant   OleVariant (    varArray) 

              
 Delphi. 

       

           Delphi 2.  Delphi 3  
        .  Delphi 
2         automated 
  Object Pascal,     TAutoObject. 
       Delphi 3,      
    Delphi 3. 
     Delphi 3      ActiveX  
,      DAX (Delphi AcliveXframework). 
 DAX     , OLE  ActiveX. 
        TAutoObject,  
   ComObj.  ,  DAX   
   ,       
     ActiveX.     
   ,     
        . 
         Delphi 3    
  . 
    1. ,         
 .       File^New 
Application.        
 File^New    ActiveX   ActiveX Library. 

    478                                            V.   OLE 

    2.    ,   File^New   
 ActiveX     Automation Object.   
  ,   Class Name     
 .       
,   Delphi. 
    3.           
   . 
    4.       ,  
   . 
    5.     Windows.   
 (<Ctrl+F9>).  ,    ,  
  (<F9>).       
Run^>Register ActiveX Server. 

        

       Delphi 3  . ,     
  ,       ,  
  .       
 .       
 ,       . 
            . 
          MessageBeep. 
          . 
       Sound,       
   . 
             . 
       SoundBeep,       
   . 
        ,   . 
      File'=>New Application.     
   File^New    ActiveX.  . 
20.2   ActiveX   (Object Repository). 
          Automation Object (. . 
20.2).      Automation Object (. 20.3). 

    . 20.2.  ActiveX   

    . 20.3.    Automation Object     
    ( Interfacing) 

      Class Name   Beeper      
 Instancing.  Multiple Instances ,    
        
.       Single Instance   
,          , 
   .  Internal  Delphi  ,  
          
       Windows. 

     20.  OLE                                       479 

          ,   . 20.3,  
 Automation Object.       , 
    (   PROJECTI. TLB)   
       (Type Library Editor) 
(. 20.4). 

    . 20.4.         
   

       . 20.4,       
.      ,      
.     BeeperLib. 
            
(IBeeper),          
Beeper.  IBeeper    IDispatch.    
       . 
            (compound 
object class, coclass) Beeper.  ,  coclass,    
 .       Beeper  
  Members,       IBeeper. 
        , 
  IDispatch.      IBeeper. 

    . 20.5.     

            . , 
  ,   ,   
 .         , 
     ,    New=>Enum 
(. 20.5).         Enum  
   . 

    480                                            V.   OLE 

       ,    Name,  SoundTypes   
 .   <Enter>.     
,   Members (    )   
       ,   
  (. 20.6). 

    MB_Ok = 0; 
    helpstring = 'Ok sound'; 
    MB_ICONHAND = 16; 
    helpstring = 'ICONHAND sound'; 
    MB_ICONQUESTION = 32; 
    helpstring = 'ICONQUESTION sound'; 
    MB_ICONEXCLAMATION =48; 
    helpstring = 'ICONEXCLAMATION sound'; 
    MB_ICONASTERISK = 64; 
    helpstring = 4CONASTERISK sound'; 

    . 20.6.    ,     
,          
  New 

             
 Beeper.   IBeeper   Members  
.           
  DispID (. 20.7). 

    property Sound: SoundTypes; dispid 1; 
    property SoundBeep[SoundType: SoundTypes]: SoundTypes; readonly; dispid 2; 
    procedure Beep; dispid 3; 
    
    . 20.7.          
 IBeeper 

     20.  OLE  481 
    16  Delphi 3.   

             
        ^"' ( View^Type Library) 
      
 .         
      ,   
   Edit^Add To Interface,     
  Add To Interface.    ,   
    ,       
 . 
    ,   SoundTypes     
  .   SoundTypes   . 
       Beeper ,     
  ,      
 .      ,  
         . 
     , Delphi   ,    ,   
,   _TLB (, Project_TLB).    
  BeepLib.   20.2   BeepLib,  
   ,     , 
    . 

     20.2. \UDELPHI 2  \BEEPERI \BEEPLIB . .  BeepLib, 
    ,    IBeeper 

    unit BeepLib; 
    { ProjectlLib Library } 
    { Version 1.0 } 
    interface 
    uses Windows, ActiveX, Classes, Graphics, OleCtrls, StdVCL; 
    const 
    LIBID_BeepLib: TGUID = '{12CFDC67-5519-11DO-A45E-444553540000}'; 
    const 
    {  SoundTypes. } 
    MB_Ok = 0; 
    MB_ICONHAND = 16; 
    MB_ICONQUESTION = 32; 
    MB_ICONEXCLAMATION = 48; 
    MB_ICONASTERISK = 64; 
    const 
    (    . } 
    Class_Beeper: TGUID = '{12CFDC69-5519-11DO-A45E-444553540000}'; 
    type 
    {  . } 
    IBeeper = interface; 
    DBeeper = dispinterface; 
    Beeper = IBeeper; 
    SoundTypes = TOleEnum; 
    (  Dispatch   Beeper. } 
    IBeeper = interface(IDispatch) 
    ['{12CFDC68-5519-11DO-A45E-444553540000}'] 
    function Get Sound: SoundTypes; safecall; 
    procedure Set Sound(Value: SoundTypes); safecall; 


    482                                            V.   OLE 


    function Get_SoundBeep(SoundType: SoundTypes): SoundTypes; safecall; 
    procedure Beep; safecall; 
    property Sound: SoundTypes read Get Sound write Set Sound; 
    property SoundBeep[SoundType: SoundTypes]: SoundTypes read Get_SoundBeep; 
    end; 
    (  Displnterface    Ibeeper. } 
    DBeeper_ = dispinterface 
    ['(12CFDC68-5519-11DO-A45E-444553540000}'] 
    property Sound: SoundTypes dispid 1; 
    property SoundBeep[SoundType: SoundTypes]: SoundTypes readonly dispid 2; 
    procedure Beep; dispid 3; 
    end; 
    {  Beeper. } 
    CoBeeper = class 
    class function Create: IBeeper; 
    class function CreateRemote(const MachineName: string): IBeeper; 
    end; 
    implementation 
    uses ComObj ; 
    class function CoBeeper.Create: IBeeper; 
    begin 
    Result := CreateComObject(Class Beeper) as IBeeper; 
    end; 
    class function CoBeeper.CreateRemote(const MachineName: string): IBeeper; 
    begin 
    Result := CreateRemoteComObject(MachineName, Class_Beeper) as IBeeper; 
    end; 
    end. 

     BeepLib   IBeeper,    .  
   , ,   ,   .  
    UNIT2. PAS,       . 
    20.3, Delphi     , 
  IBeeper,     ,  
    . 

     20.3, UNIT2. PAS.   IBeeper,   Delphi 

    unit Unit2; 
    interface 
    uses 
    ComObj, Projectl_TLB; 
    type 
    TMyBeep = class(TAutoObject, IBeeper) 
    protected 
    function Get_Sound: SoundTypes; safecall; 
    function Get_SoundBeep: BeepTypes; safecall; 
    procedure Beep; safecall; 
    procedure Set_Sound(Value: SoundTypes); safecall; 
    end; 
    implementation 


     20.  OLE                                       483 


    uses ComServ; 
    function TBeeper.Get_Sound: SoundTypes; 
    begin 
    end; 
    function TBeeper.Get_SoundBeep: SoundTypes; 
    begin 
    end; 
    procedure TBeeper.Beep; 
    begin 
    end; 
    procedure TBeeper.Set_Sound(Value: SoundTypes); 
    begin 
    end; 
    initialization 
    TAutoObjectFactory.Create(ComServer, TBeeper, Class_Beeper, ciMultiInstance); 
    end. 

         ,     
    ,    . 
 TBeeper        
  TAutoObject  DAX,  ,    
 TBeeper    .   ,  
 TBeeper   I  ,    
   IBeeper. 
     ,        safecall. 
       FastCall,  safecall 
         
     OLE  . 
     ""         
UNIT2,  . 

    TAuotObjectFactory.Create(ComServer, TBeeper, Class_Beeper, ciMultiInstance); 

     TAutoObjectFactory     DAX.  
 TAutoObjectFactory    ,  . 
       Beeper       Delphi. 
       Windows       
Beeper    .     
 : <   >.<   
>.     BeepLib.Beeper. 
      TAutoObjectFactory     
IClassFactory ,       
     . 
         Beeper.   
  ,   BEEPER. DPR,   
\UDELPHI3\CHP20\BEEPER1\.   20.4     
BeepAuto,       Beeper. 
   Beeper  .  ,  
   Beeper,    BeepAuto.  
 ,     . 

     20.4. \UDELPHl3\CHP20\BEEpERl\BEEPAUTO. PAS.  BeepAuto 
      |   Beeper 

    unit BeepAuto; 
    interface 
    uses 


    484                                            V.   OLE 


    ComObj, BeepLib; 
    type 
    TBeeper = class(TAutoObject, IBeeper)* 
    private                     ~' 
    FSound : Integer; 
    protected 
    function Get Sound: SoundTypes; safecall; 
    function Get SoundBeep(SoundType: SoundTypes): SoundTypes; safecall; 
    procedure Beep; safecall; 
    procedure Set_Sound(Value: SoundTypes); safecall; 
    end; 
    implementation 
    uses ComServ, Windows, Beep.Form, SysUtils; 
    function TBeeper.Get_Sound: SoundTypes; 
    begin 
    BeeperLogForm.AddActionToLog('Get Sound value: ' + IntToStr(FSound) ); 
    Result := FSound; 
    end; 
    function TBeeper.Get_SoundBeep(SoundType: SoundTypes): SoundTypes; safecall; 
    begin 
    BeeperLogForm.AddActionToLog('Get SoundBeep') ; 
    Set_Sound(SoundType) ; 
    Beep; 
    Result := FSound; 
    end; 
    procedure TBeeper.Beep; 
    begin 
    BeeperLogForm.AddActionToLog('Beep method called'); 
    MessageBeep(FSound) ; 
    end; 
    procedure TBeeper.Set Sound(Value: SoundTypes); 
    const 
    InvalidSound = 'Invalid Sound property parameter. ' + 
    'The possible values are: MB_OK(0), MB_ICONHAND(16) , ' + 
    'MB_ICONQUESTION(32), MB_ICONEXCLAMATION(64) ' + 
    'and MB_ICONASTERISK(128)'; 
    begin 
    BeeperLogForm.AddActionToLog('Set Sound value to ' + IntToStr(Value)); 
    if not (Value in [MB_OK, MB_ICONHAND, MB_ICONQUESTION, 
    MB_ICONEX5LAHATION, MB_ICONASTERISK]) then 
    Raise EOleError.Create(InValidSound); //Exit; 
    if FSound <> Value then FSound := Value; 
    end; 
    initialization 
    TAutoObjectFactory.Create(ComServer, TBeeper, Class_Beeper, 
ciMultiInstance) ; 
    end. 

     Delphi      .   
      DLL  .  
    ( View^ Project Source),   
,      $R      . 

    {$R *.RES) 
    ($R *.TLB) 

      ,     ,  
       TLB. 

     20.  OLE                                       485 

        Beeper    
,     .   
  ,    Beeper 
    ,       
Windows.       , 
      CLIENT.DPR,   
 \UDELPHI3\CHP20\BEEPCTRL\.   20.5    
   .  . 20.8   
      . 

    . 20.8.  ,     Beeper 

     20.5. \UDELPHI3\CHP20\BEEPCTRL\CLIENTFM. PAS.     
  Beeper    

    unit ClientFm; 
    interface 
    uses 
    Windows, Classes, Controls, Forms, Dialogs, ComCtrls, 
    StdCtrls, ExtCtrls; 
    type 
    TAutomationController = class(TForm) 
    SoundTypes: TRadioGroup; 
    ConnectButton: TButton; 
    SetSoundButton: TButton; 
    BeepButton: TButton; 
    SoundBeepButton: TButton; 
    DisconnectButton: TButton; 
    procedure ConnectButtonClick(Sender: TObject); 
    procedure SoundBeepButtonClick(Sender: TObject); 
    procedure BeepButtonClick(Sender: TObject); 
    procedure SetSoundButtonClick(Sender: TObject); 
    procedure DisconnectButtonClick(Sender: TObject); 
    private 
    BeeperObject : OleVariant; 
    function GetMBSoundType : Byte; 
    end; 
    var 
    AutomationController: TAutomationController; 
    implementation 
    <$R *.DFM} 
    uses ComObj; 
    const 
    NotConnectedMsg = 'Please connect to the Beeper automation object first'; 
    ConnectFailMsg = "Make sure automation object is registered'; 


    486                                            V.   OLE 


    function TAutomationController.GetMBSoundType : Byte; 
    // 
    // .   Sound Type,  
    //             . 
    // 
    const 
    SoundTypes : array[0..4] of Integer = (MB_Ok, MB_ICONHAND, 
    MB_ICONQUESTION, MB_ICONEXCLAMATION, MB_ICONASTERISK) ; 
    begin 
    Result := SoundTypes[Self.SoundTypes.Itemlndex]; 
    end; 
    procedure TAutomationController.ConnectButtonClick(Sender: TObject) ; 
    // 
    // .      
    // Beeper.  CreateOleObject '"?' "'^ 
    // IDispatch,     Delphi           | 
    // OleVariant.        
    // OLE. 
    // 
    begin 
    if VarIsEmpty(BeeperObject) then 
    try 
    BeeperObject := CreateOleObject('BeepLib.Beeper'); 
    except 
    ShowMessage(ConnectFailMsg); 
    end; 
    end; 
    procedure TAutomationController.SoundBeepButtonClick(Sender: TObject) ; 
    // 
    // .     
    //  SoundBeep  Beeper. 
    //  Beeper    . 
    // 
    begin 
    if VarIsEmpty(BeeperObject) then 
    ShowMessage(NotConnectedMsg) 
    else 
    BeeperObject.SoundBeep[GetMBSoundType] ; 
    end; 
    procedure TAutomationController.BeepButtonClick(Sender: TObject) ; 
    // 
    // .      Beeper. 
    // 
    begin 
    if VarIsEmpty(BeeperObject) then 
    ShowMessage(NotConnectedMsg) 
    else 
    BeeperObject.Beep; 
    end; 
    procedure TAutomationController.SetSoundButtonClick(Sender: TObject) ; 
    // 
    // .     //  
Sound   Beeper. // 
    begin 
    if VarIsEmpty(BeeperObject) then 
    ShowMessage(NotConnectedMsg) 
    else 
    BeeperObject.Sound := GetMBSoundType; 
    end; 


     20.  OLE                                       487 


    procedure TAutomationController.DisconnectButtonClick(Sender: TObject) ; 
    // 
    // .     . 
    //     OleVariant    
    // , Delphi   ,    
    //     .   ,  
    //       Release 
    // ,      . 
    // ,   ,   
    // Beeper,     ,    
    //  Beeper  . 
    // 
    begin 
    BeeperObject := Unassigned; 
    end; 
    end. 


        

            
  ,      ,  
 ,    ,    
.  ,   File^New Application   
 File^New     ActiveX,   ActiveX  
   ActiveX Library     
,    .   20.6 
       Beeper.  
       . 

    1  20.6. \UDELPHi3\CHP20\BEEPER2\BEEPER.DPR.   
  

    library Beeper; 
    uses 
    ComServ, 
    Forms, 
    BeepAuto in 'BeepAuto.pas' (Beeper: CoClass), 
    BeepLib in 'BeepLib.pas', 
    BeepForm in 'BeepForm.pas' {BeeperLogForm}; 
    {$R *.RES} 
    ($R *.TLB} 
    exports 
    DllGetClassObject, 
    DllCanUnloadNow, 
    DllRegisterServer, 
    DllUnregisterServer; 
    begin 
    Application.Initialize; 
    end. 

          Beeper  
   ,    
  .       
 .    ,   , 
   Beeper.      
   Windows,         
,     . 
       Delphi    Run^Register ActiveX Server. 
       REGSVR32  Microsoft    
 .  ,      
Windows NT   - MSDN. 

    488                                            V.   OLE 

      REG-,        
,     . 
           ,  
  Register/Unregister ActiveX server,    
      . 
      ,       
  DllRegisterServer. 
          Beeper    
 \UDELPHI3\CHP20\BEEPER2    . 
        BEEPER. REG, 
   . 

     

             
 .       
.   ,    
.   DCOM  Microsoft,    
  Windows NT 4.       Microsoft 
    DCOM   Windows 95.  
,    ,    OLE 
Enterprise  Open Environment (   
Borland).  OLE Enterprise      
Windows 95  Windows NT 4.         
  Delphi Client/Server. OLE Enterprise    
       
     .  ,   
      , OLE Enterprise  
     . 
        ,   OLE   
    .   
     ,     
 ActiveX. 

     20.  OLE                                       489