        30 

     Delphi  C++ 

    Object Pascal  C++      .   
   ,    
       (Virtual table; VT  C++ 
 VMT   Delphi).       Delphi  C++  
 . 
           ?  :   
        
  .      
  C++ ,   Delphi no  
 ,   -     Web.  
    C++  Delphi       
 ,       . 

     
         25, " 
DLL: ",  26,   " DLL:  ",    
   Object Pascal,    Borland C++.    
" " C++.    C++   , 
  . ,   stdcall  WINAPI   . 

      

         Pascal  C++,   
    .      , 
    . 
    
        Delphi  C++ 
            Delphi  C++. 

           
        ,   
       . 

701


      

              
      ,     , 
   , ,   ,    
ateProces5.         
   " *'  
 . ,      
  wm_CopyData ( 32, "  ").   
    {file-mapping}.    
  ,   . 
     , ',   ,      
        . 
        Component Object Model (^ 
         (   
   19, "   "). 

    OBJ- 

    Delphi       Intel.   
  Borland C++,   Delphi (    
 ;    DCU-). Visual C++  
COFF-  ,         . 

    DLL 

    ,      Delphi  C++    
DLL.     Delphi,   C++,    DLL, 
   ,      ,  
 .       . 
    -          
 . 

       

          (calling conventions).  
            
 .  C++  Delphi    
,      .      
  ,    , 
  . 30.1. ( ,     C++ 
     ;   
     Watcom C++.. .) 

     30.1.        ? '  " " -.'-,;:,,,.. 

    	 C++	 Delphi	   

    C-Decl	cdecl	cdecl	C++ 
    Pascal	pascal	pascal	Delphi 1.0  Windows . 
    StdCall	stdcall	stdcall	Win95  WinNT 
    SafeCall		safecall	 Delphi! 
    Fast call	fastcall	register.	Delphi 2.0, 3 

      ""  ,     .  
 " C++"       
  C++,   " Delphi"        Delphi  
 , "  ", ,    
      . 
    , , ,   C++  Delphi   
    .    ,  
  , ,    ,  
.          
 . 

    702                                        VI.   

     . 30.1 ,   Delphi      
Fast Call,   C++  Decl.     
 ,     ,    
. !  . ,   
^_,^ (  StdCall);    , 
       , 
  . 

       

         ,       
  .    ,    
,    ,      
  (,  Fast call).    
,     . 
        DLL ,_.0+,   
  ,     ( )  
C-Decl   C++    (mangled . , -  
     ,     
  ,     . 
              
,    ,   StdCall.  
,          . 
      26, " DLL:  ",  
     , ,  
,     .  
             , 
 C-Decl    .  , 
       StdCall,  
  ,     . 
      C-Decl  Delphi    ,  
C-Decl    .  ,    
    (_) (,     
  ,   ...). ?   C++  
Delphi  ,      DLL  
  (  ,  ,  ,   
  , .. ). ,    DLL,   
  GetHeartRate   C-Decl.    
  _GetHeartRate      Delphi  
  . 
           C++ (  mangled names 
 ,  ).   ,   C++   
 (function overloading)       
 . ,  foo (int)  foo (int, int)   
 ,          
.          
 @foofunc$qinqminzc$v. A Delphi,  ,    . 
              
extern "", ..      : 

    extern "" int   stdcall SomeFunction(int) 


          C++  Object Pascal 

     C++     Delphi   
  {$L FileName}.      
  OBJ.    Delphi   
     ,    
  (static link). 

     30.  Delphi  C++                                    703 


       C++  Delphi 

    ,       Delphi    
   .   30.1,    
  . 

    -30.1. \UDELPHI3\CHP30\SAMP1.CPP                                     

                \ int iGlobal = 0; 
    int _stdcall SetGlobalValue (int iValue) (   int iTmp = ,iGlobal; 
    iGlobal = iValue; 
    return iTmp; 
    } 
    int _stdcall GetGlobalValue() ( return iGlobal; 
    ) 

       .     
  iGlobal.  ,    
_stdcall,         
 (       
).   30.2 ,   4>    Delphi. 

     30.2. USEOBJUI . .   C++ ()   

    var 
    Formi: TFormI ; 
    implementation 
    ($R *.DFM) 
    ($L SAMP1} // SAMP1.0BJ! 
    function SetGlobalValue(iValue: integer): integer; stdcall; external
    function GetGlobalValue: integer; stdcall; externals
    procedure TFormI.ButtonlClick(Sender: TObject) ; 
    begin 
    SetGlobalValue(10); //    OBJ-  10. 
    ShowMessage(Format('Variable in OBJ contains %d',[GetGlobalValue])); 
    end; 
    end. 

      ( $L SAMP1},     SAMP1. 
OBJ,    SetGlobalValue  GetGlobalValue.   
    external-,      
      . 
        ?    C++,  
   C++ (C++ Run-time library)    
  ,   errno?     
     ,      . 
             ,  
        (,  
    Windows API),  Delphi " 
 ".           
C++ (   ,     C++    ). 

    704                                        VI.   

           Windows API   , 
  I  "     ". 
,      API " MessageBox.    
      extern   ""   int   _stdcall MessageBox (long, 
char*, char*, long) ; (..  ,    ).  
 Delphi ,    ,       
. ,   Delphi,    C++.     ,  
   . 
             
?    ,    ,     ,  
     .  , Dejrlhi 
    (LIB) .     
,      ... 
               
  C++.   ,     , 
            
        . 

       Delphi  C++ 

     ,   C++, Delphi       
    C++. , Borland C++ Builder  
    Delphi 2.0 VCL (-!).  C++ 
     DCC32 (  
   ,   C++ Builder   5.02),  
    Pascal   ,      
   C++. 
        Delphi  C++   
   Delphi.  ,   . 
    ,    Pascal    : 

    var iGlobal: integer; 

          C++ ,    : 

    extern int iGlobal; 

    Delphi      .    
  Generate Object Files   Linker   
Project Options,     Project^Options. 

     DLL    C++  Object Pascal 

    ,         
C++  Object Pascal   DLL. DLL    
,        
 ,     .      
  DLL,   ,         
 26  27,       . 
    ,        Delphi  C++   DLL. 
      -. 
       ,   ""  
      . 
        . 
           .    , 
    Microsoft WinX     . 
     "". 

      

      (flat functions}   ,   
DLL. DLL       DLL    
   ,    LoadLibrary, LoadLibraryEx  
GetProcAddress.       
 ( 

     30.  Delphi  C++                                    705 
    23  Delphi 3.   

        ),      , 
        . 
   Function!,     
TFoo.   Function2     , a 
Functions     .     
   (wrapping)   . 

      

    ,    C++   ,   
 ,  .  Delphi TStringList   
  ,           
   C++.   30.3   DLL   
 CDSort,      . 

     30.3. \UDELPHl3\CHP30\SDFMOD.DPR.    TStringList 

    library SDFMod; 
    uses 
    SysUtils, 
    Dialogs, 
    Classes; 
    procedure CDSort(pStrings: PChar); stdcall; 
    begin 
    if not Assigned(pStrings) then Raise 
    EInvalidPointer.Create('Buffer is nil'); 
    with TStringList.Create do try 
    CommaText := pStrings; 
    Sorted := True; 
    StrCopy(pStrings,PChar(CommaText)) ; 
    finally 
    Free; 
    end; 
    end; 
    exports CDSort; 
    begin 
    end. 

     CDSort      ,     
 Delphi       C++.   
         DLL.   30.4 
       CDSort 
  C++. 

     30.4. \UDELPHI3\CHP30\USELIB. .   SDFMOD. 
DLL    CDSort 

    #include <windows.h> 
    #include <string.h> 
    #define C_STRING \ "\"CString\",\"DString\",\"BString\",\"AString\"" 
    void (_stdcall *CDSort)(char*) = NULL; 
    #pragma argsused 
    int WINAPI WinMain(HINSTANCE hCurInstance, 
    HINSTANCE hPrevInstance, LPSTR IpCmdLine, int nCmdShow) ( char* pBuf = new char[256]; 


    706                                        VI.   


    strcpy(pBuf,C_STRING) ; 
    MessageBox(0,pBuf,"Before",MB_OK) ; 
    HINSTANCE hinst = LoadLibrary("SDFMod.dll"); 
    if (hinst) { 
    (FARPROC)CDSort = GetProcAdd.r:e.ss.(.hJn.st, "CDSort") ; 
    if (CDSort) CDSort'(pBuf') ; 
    MessageBox(O.pBuf,"After",MB_OK);                                          
    } 
    delete pBuf; 
    return 1; 
    ) 

        void (_stdcall *CDSort) (char*) = NULL;, 
    CDSort,     
 .          
 DLL.      __stdcall,    
    StdCall,      DLL. 
          GetProcAddres5,   
    ,     
LoadLibrary,       .  
         : 

    (FARPROC)CDSort = GetProcAddress(hinst,"CDSort"); 

      GetProcAddress   (  
),  CDSort         
   pBuf,        DLL  
     MessageBox. 
             Delphi  
  C++.      : 

    function ShowModalForm(hMainWnd: THandle): integer; stdcall; 
    begin 
    Application.Handle := hMainWnd; 
    with TFormI.Create(Application) do 
    try 
    result := ShowModal; 
    finally Free; 
    end; 
    end; 

      C++        30.4. 
      . 

    int (_stdcall *ShowModalForm)(HWND) = NULL; 

     ,        CDSort. 

    if (hinst) { 
    (FARPROC)ShowModalForm = GetProcAddress(hinst, "ShowModalForm") ; 
    if (ShowModalForm) ShowModalForm(0); 

          DLL     
"" . 

      

    ,     Delphi    
  C++.        
           
.        30.5. 

     30.  Delphi  +-                                    707 


     30.5. \UDELPHI3\CHP30\CWRAP.CPP.    
   TCharQue         , 

    #include <windows.h> 
    #include <queues.h> 
    typedef TQueue <char> TCharQue; 
    extern "C" ( 
    //Returns an instance of TCharQue 
    HANDLE _stdcall _export QueLoadQueueInst (int iSize) 
    { TCharQue* CharQue = 
    new TCharQue(( iSize ? iSize : DEFAULT_QUEUE_SIZE)); 
    return (HANDLE)CharQue; 
    } 
    //Releases an instance of TCharQue 
    void stdcall _export QueFreeQueueInst(TCharQue* CharQue) 
    ( delete CharQue; 
    } 
    void _stdcall _export QueFlush(TCharQue* CharQue) 
    { CharQue->Flush() ; 
    } 
    int stdcall _export QueIsEmpty(TCharQue* CharQue) 
    { return CharQue->IsEmpty(); 
    } 
    int _stdcall _export QueIsFull(TCharQue* CharQue) 
    ( return CharQue->IsFull(); 
    } 
    void _stdcall _export QuePut( TCharQue* CharQue, char t ) 
    { CharQue->Put(t) ; 
    ) 
    char _stdcall _export QueGet(TCharQue* CharQue) 
    ( return CharQue->Get(); 
    } 
    int   stdcall-"export QueGetItemsInContainer( 
                               TCharQue* CharQue) 
    ( return CharQue->GetItemsInContainer(); 
    ) } 

     ,    30.5,   
    . 
     TQueue   (    ; FIFO), 
      Put,       
 Get. 
       , ,  ,    
  ,    ,    - 
    (,     ). 
     QueLoadQueueInst      
TCharQue   .     
   ,      
   .       
QueFreeQueueInst,     delete    
  . 
          ,     
Delphi,       DLL    Delphi 
( 30.6). 

    ,  30.6. \UDELPHI3\CHP30\CWRAPU1. PAS.  CWRAP. DLL 
    
    unit cwrapul; 
    interface 
    uses 
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 


    708                                        VI.   


    StdCtrls; 
    type 
    TFormI = class(TForm) 
    Buttoni: TButton; 
    procedure ButtonlClick(Sender: TObject); 
    private 
    { Private declarations } 
    public 
    ( Public declarations } 
    end; 
    var 
    Formi: TFormI; 
    const 
    CWRAP = 'C:\BOOKS\DELPHI30\CHAPT30\CODE\CWRAP.DLL'; 
    function  QueLoadQueueInst(iSize: integer): THandle; 
    stdcall  external CWRAP; 
    procedure QueFreeQueueInst(hCharQue: THandle); 
    stdcall  external CWRAP; 
    procedure QueFlush(hCharQue: THandle); 
    stdcall  external CTRAP; 
    function  QueIs Empty(hCharQue: THandle)  longbool; 
    stdcall  external CWRAP; 
    function  QueIsFull(hCharQue: THandle): longbool; 
    stdcall  external CWRAP; 
    procedure QuePut( hCharQue: THandle; t: char ); 
    stdcall  external CWRAP; 
    function  QueGet(hCharQue: THandle): char; 
    stdcall  external CWRAP; 
    function  QueGetItemsInContainer(hCharQue: THandle)': integer; 
    stdcall  external CWRAP; 
    implementation 
    ($R *.DFM) 
    procedure TFormI.ButtonlClick(Sender: TObject); 
    var 
    hinst: THandle; 
    begin 
    hinst := QueLoadQueueInst(0) ; 
    if QueIsEmpty(hinst) then 
    ShowMessage('IsEmpty = TRUE') ; 
    QuePy( hinst, 'H' ); 
    QuePut( hinst, 'E' ) ; 
    QuePut( hinst, 'L' ) ; 
    QuePut( hinst, 'L' ) ; 
    QuePut( hinst, '0' ) ; 
    if not QueIsEmpty(hinst) then 
    ShowMessage(IntToStr(QueGetItemsInContainer(hinst))) ; 
    ShowMessage(Format('%s%s%s%s%s', 
    [ QueGet(hinst),QueGet(hinst), QueGet(hinst),QueGet(hinst), QueGet(hinst)] 
    )) ; 
    QueFreeQueueInst(hinst); 
    end; 
    end. 


     30.  Delphi  C++                                    709 

     QueIsEmpty  QueIsFull   C++    
 ,    Delphi     ,  
           
(         , 
   LongBool  Pascal  int  C++   ). 

        


        Delphi  C++  DLL   
   .  DLL    
  ,   ,  
 .   .   C++,   
Delphi.       ,  
 -        DLL . 
          DLL  C++,  
  - Delphi.   26, " DLL:  
",    ,    -    
 ,   DLL,   Pascal. 

       C++ 

                
    C++ TQueue.      
-.   30.7      C++ CClass, 
       .   
   -,    ,  
,     VT ( )  . 
(,   '])'?{1'_'    Delphi 
 Borland C++    ,       
 C++,     ,    
         
. . .) 

     30.7. \UDELPHl3\CHP30\CCLASS. .  GetQueue,  
  TCharQueue s 

    #include <windows.h> 
    #include <queues.h> 
    #include <stdio.h> 
    #include "charque.h" 
    extern "C" TCharQueue* stdcall export GetQueue(int iMax) 
    { return new TCharQueue(iMax); 
     

      GetQueue      , 
   .     
  TCharQueue,       
TQueue.   TCharQueue    30.8. 

     30.8. \UDELPHI3\CHP30\CHARQUE.H.   TCharQueue        
                      ; 
    #ifndef charque_h 
    #define charque h 
    #include <queues.h> 
    typedef TQueue <char> TCharQue; 
    class TCharQueue { public: 
    TCharQueue(int i) ; 
    -TCharQueue() ; 
    private: 
    TCharQue* FCharQue; 
    public: 
    virtual char _stdcall Get(); 


    710                                       VI.   


    virtual void stdcall Put( char t ); 
    virtual int _stdcall IsEmptyO; 
    virtual int _stdcall IsFullO; 
    virtual int   stdcall GetItemsInContainer(); 
    virtual void stdcall Flush(); 
    virtual void stdcall Delete (); 
    }; 
    #endif 

      30.8   CharQue.h,     
TQueue    TCharQue.  TQueue   ,   
     (    char). 
     TCharQueue  ,    ,    
   ,    . 
     TCharQueue    ,    
 .  ,    ,   
.       . ,   
  ,        
.   , ,   ,  
     . Delphi    
        . ,  
   ,         
stdcall.    ,       
   . ~'  30.9  
  . 

     30.9. \UDELPHI3\CHP30\CHARQUE. .   TCharQueue 

    #nclude "charque.h" 
    TCharQueue::TCharQueue(int i) 
    ( FCharQue = NULL; 
    FCharQue == ( ! i ? new TCharQue : new TCharQue(i) ); 
    if (!FCharQue) throw; 
    TCharQueue::-TCharQueue() 
    { if (FCharQue) delete FCharQue; 
    ) 
    char _stdcall TCharQueue::Get () 
    { return FCharQue->Get(); 
    } 
    void _stdcall TCharQueue::Put ( char t ) 
    ( FCharQue->Put(t) ; 
    } 
    int _stdcall TCharQueue::IsEmptyO 
    { return FCharQue->IsEmpty(); 
    } 
    
    int _stdcall TCharQueue::IsFull() 
    {return FCharQue->IsFull() ; 
    } 
    int _stdcall TCharQueue::GetItemsInContainer() 
    { return FCharQue->GetItemsInContainer() ; 
    } 
    void _stdcall TCharQueue::Flush() 
    { FCharQue->Flush(); 
    } 
    void _stdcall TCharQueue::Delete() 
    9 { ::delete this; 


     30.  Delphi  C++                                   711 

     ,    TCharQueue    
  TCharQue.      
TCharQue        , 
   . 
            , 
    ,    . 

        Delphi 

      CCLAS S.DLL    ,   
.    ,    30.10. 

     30.10. \UDELPHI3\CHP30\LOADU1. .  TCharQueue 

    unit loadul; 
    interface 
    I  usea 
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; 
    'type 
    TFormI = class(TForm) 
    GroupBoxl: TGroupBox; 
    Button3: TButton; 
    procedure Button3Click(Sender: TObject); 
    private 
    { Private declarations } 
    public 
    { Public declarations ) 
    end; 
    TCharQueue = class 
    public 
    function    Get: char 
    stdcall virtual abstract 
    procedure   Put( const t: char ) 
    stdcall virtual abstract 
    function    IsEmpty Longboo.. 
    stdcall virtual abstract 
    function    IsFull: Longbool 
    stdcall virtual abstract 
    function    GetItemsInContainer: integer 
    stdcall virtual abstract 
    procedure   Flush 
    stdcall virtual abstract 
    procedure   Delete 
    stdcall virtual abstract 
    end; 
    var 
    Formi: TFormI; 
    function GetQueue(iMax: integer): TCharQueue; 
    stdcall; external 'CClass'; 
    implementation 
    ($R *.DFM) 
    procedure TFormI.Button3Click(Sender: TObject); 
    begin 
    With GetQueue(5) do 
    try 


    712                                        VI.   


    Put ( ' H ' ) ; 
    Put ( ' E ' ) ; 
    Put ( ' L' ) ; 
    Put (  L' ) ; 
    Put ('0'); 
    if IsFulK) then 
    ShowMessage(Format('There are %d characters in Queue', 
    [GetItemsInContainerQ])) ; 
    ShowMessage( Format('%s%s%s%s%s', 
    [ GetO , 
    Get(), 
    GetO, 
    GetO , 
    Get 0 ] ) ) ; 
    if IsEmptyO then 
    ShowMessage(Format('There are %d characters in Queue', 
    [GetItemsInContainer()])); 
    finally 
    Delete; 
    end; 
    end; 
    end. 

           CCLASS . DLL 
   GetQueue. function GetQueue(iMax: integer): 
TCharQueue; stdcall; external 'CClass'; 
    ,      , ,    
  TCharQueue  ,       .  
        C++,     
 abstract.          
_       ''  
"""                                           , 
    ,          
     DLL.   ,     
   ,      .   
  ,     (     
 ,      DLL).    ,    DLL,  
  . 
       ,   "Hello",     
        .    
    .         
       (0). 

       

     . 30.2     C++  Delphi, ..    
C++       Delphi. 

    \  30.2,   Delphi  C++ 

    Delphi                      C++ 

      
    Shortint                  short 
    Smallint                  unsigned short 
    Byte                      BYTE 
    char                      unsigned short 


     30.  Delphi  C++                                   713 


     . 30.2 

    Delphi_______________C++________________________________________ 

     ' 
    Cardinal	unsigned int 
    Integer	int 
    Word	unsigned int 
    Longint	long 
    DWORD	unsigned long 
    DWORD	UINT 

        
    Double	double 
    Extended	long double 
    Comp	unsigned long 
    Real  
    Single	float 

        
    Char	char 
    String  
    ShortString  
    Wchar	wchar t 
    Pchar	char*,LPSTR 

      
    Boolean	 : BYTE, Char, enum 
    Bool	bool 
    WordBool	unsigned int 
    LongBool	long 

      
    pointer	void* 
    Pinteger	int* 
    Variant  
    Currency  
    Set  
    
    
         (enumerated):  Delphi   
 (unsigned byte),   C++ (unsigned short).   
           
  Delphi    C++  .  Delphi,  
,         : 

    ($2),($MINENUMSIZE). 

     Delphi  (const)   ,   C++.   
Foo  C++  : 

    int _stdcall _export Foo(const T tVar); 

        Delphi    : 

    function Foo(const tVar: T); integer; stdcall; 


    714                                        VI.   

           .  C++ 
        typedef,  

    typedef int (_stdcall TMyProc)(char) ; 

          TMyProc MyProc; .  
        Pascal. 

    type TMyProc = function(c: PChar): integer; stdcall; 
    var MyProc: TMyProc; 

             . 
     Delphi  C++ "."" 

    var MyProc: function(c: PChar): integer; stdcall; 
    int (_^stdcaTl MyProc)(char*); 


     

         Delphi  C++.   
  . 
         . 
      1     2. 
    K     DLL. 
         DLL. 
     -       
        
   .     
    . 
        DLL,    
DLL          
   StdCall. 
        ,  DLL    
    . 
      ,      ( 
Delphi,   C++),         , 
".          
    Delphi  C++. 
     30.  Delphi  C++    715 
