          26 

     DLL:   

      

    
    ȗ   
             . 

           
       DLL    ^     . 

        
             
    DLL  . 

        
            . 

      DLL- 
      Turbo Dump       . 

    SJ Delphi    DLL,  
    Win32.      
  Win32    25, " DLL: 
".     ,   
 ,    DLL,   
  ,      
 DLL.         
DLL      . 
       Delphi    , 
    (,  ). , , 
   (,   ).  ,  
     .    
 DLL. 
      25    ,  
- .     DLL,   
 .  DLL    ,  
       ,        
 .   ,   DLL    
    ,   ""  . 
       '"" 
    . 

    610 

    -  

             
  DLL.     , 
(10~?""?     , 
"" .  ,  DLL *~^5^ 
_ , ,   ,  .    
     ,    . 
    "*~~"1"1   ,   
         . 
    :      
-,,.-. .       
:         
   . ^      . 
   ,     .   
,          , 
        ^_^  
-COBJI  .	"' 
     DLL-,        
   .   ClassWrap    
  . 

       26.1. \UDELPHI3\CHP26\ CLASSWRAP. DPR.     
  DLLMain -     ,     

    library ClassWrap; 
    uses 
    NumAccum in 'NumAccum.pas', wraplib in 'wraplib.pas1; 
    end. 

    DPR-    .     uses,  
   pas-.   NumAccum.pas   
,   wraplib.pas -   exports. 

      
     DLL-  Delphi    25, 
" DLL: ".       
 exports. 


       DLL 

      DLL-   ,   . 
      DLL- ClassWrap 
   26.2. 

     26.2. \UDELPHI3\CHP26\ NUMACCUM. PAS.   ( 
      ) 

    unit NumAccum; interface 
    type 
    TOrdAccum = class private 
    FNumber: integer; protected 
    procedure SetAdd(i: integer); virtual; 
    procedure SetSubtract(i: integer); virtual; 
    procedure SetDivide(i: integer); virtual; 


     26.  DLL:   611 


    procedure SetMultiply (i: integer); virtual/public 
    constructor Create(InitalValue: integer); virtual; 
    property Add: integer read FNuraber write SetAdd; 
    property Subtract: integer read FNumber write SetSubtract; 
    property Divide: integer read FNumber write SetDivide; 
    property Multiply: integer read FNumber write SetMultiply; end; 
    var 
    NoAccum: TOrdAccum; 
    implementation 
    constructor TOrdAccum.Create(InitalValue: integer); begin 
    FNumber := InitalValue; end; 
    procedure TOrdAccum.SetAdd(i: integer); begin 
    FNumber := FNumber + i; end; 
    procedure TOrdAccum.SetSubtract(i: integer); begin 
    FNumber := FNumber  i; end; 
    procedure TOrdAccum.SetDivide(i: integer); begin 
    FNumber := FNumber div i; end; 
    procedure TOrdAccum.SetMultiply(i: integer); begin 
    FNumber := FNumber * i; end; 
    end. 

      26.2   NUMACCUM. PAS,     
TOrdAccum.       FNumber  integer. 
       FNumber.    
   ,    ,    . 
       , 
    (  Set).   
      . 
       ,    
  Create, ..,       
     1,  FNumber  
  1.  ,   Add  
 2.       (  ), 
    3.     4 
 Multiply   ,    12.  
  Divide  3     4.  , 
    ,     
  FNumber,        . 
     ,  DLL      ,  
  .  TOrdAccum      
,   ( )  "" 
      . 
,   DLL-,    , 
 DLL.        
,         DLL,  
    .	' 

    612	 VI.   


     -    

      26.3   WRAPLIB.PAS DLL- CLASSWRAP.  
   .  ,     
      StdCall   
  Win32.       
Win95  WinNT         
DLL-   .       
  25, " DLL: ". 

     26.3. \UDELPHi3\CHP26\WRAPLiB. PAS.      
    

    unit wraplib; interface uses NumAccum; 
    procedure FreeOrdAccumlnst(Inst: TOrdAccum); stdcall; function 
GetOrdAccumlnst(InitialVal: integer): TOrdAccum; stdcall; function  
OrdAdd(Inst: TOrdAccum; iVal: integer): integer; stdcall; function  
OrdDivide(Inst: TOrdAccum; iVal: integer): integer; stdcall; function  
OrdMultiply(Inst: TOrdAccum; iVal: integer): integer; stdcall; function OrdSubt 
    ract(Inst: TOrdAccum; iVal: integer): integer; stdcall; 
    exportis 
    FreeOrdAccumlnst, 
    GetOrdAccumlnst, 
    OrdAdd, 
    OrdDivide, 
    OrdMultiply, 
    OrdSubtract; 
    implementation 
    function GetOrdAccumlnst(InitialVal: integer): TOrdAccum; begin 
    result := TOrdAccum.Create(InitialVal); end; 
    procedure FreeOrdAccumlnst(Inst: TOrdAccum); begin 
    Inst.Free; end; 
    function OrdAdd(Inst: TOrdAccum; iVal: integer): integer; begin 
    Inst.Add := iVal; 
    result := Inst.Add; end; 
    function OrdSubtract(Inst: TOrdAccum; iVal: integer): integer; begin 
    Inst.Subtract := iVal; 
    result := Inst.Subtract; end; 
    function OrdDivide(Inst: TOrdAccum; iVal: integer): integer; begin 
    Inst.Divide := iVal; 
    result := Inst.Divide; end; 


     26.  DLL:  	613 


    function OrdMultiply(Inst: TOrdAccum; iVal: integer): integer; begin 
    Inst.Multiply := iVal; 
    result := Inst.Multiply; end; 
    end. 

        GetOrdAccumlnst: function     
GetOrdAccumlnst(InitialVal:   integer):   TOrdAccum;   stdcall; 

     GetOrdAccumlnst    ,    
     TOrdAccum,   
  .       
 ,        
TOrdAccum.     ,      
 .     WRAPLIB. PAS    
FreeOrdAccumlnst,    ,     : 

    procedure  FreeOrdAccumlnst(Inst:   TOrdAccum);   stdcall; 

         ,     
GetOrdAccumlnst,  .      
     FreeOrdAccumlnst.  , 
  GetOrdAccumlnst , ,  ,     
      FreeOrdAccumlnst. 
        GetOrdAccumlnst    Thandle 
 TOrdAccum.  ,  ,     
,     TOrdAccum,     , , 
.  THandle     (32 ),    
  TOrdAccum.  ,       
   ,	j 
          FreeOrdAccumlnst  THandle 
    inst.   THandle   
         
.  DLL      ,  
      . 
     WRAPLIB. PAS    . 
    function	OrdAdd(Inst:   TOrdAccum;   iVal:   integer):   integer;   stdcall; 
    function	OrdDivide(Inst:   TOrdAccum;   iVal:   integer):   integer;   stdcall; 
    function	OrdMultiply(Inst:   TOrdAccum;   iVal:   integer):   integer;   stdcall; 
    function	OrdSubtract(Inst:   TOrdAccum;   iVal:   integer):   integer;   stdcall; 
            
 TOrdAccum,     .   
   .    
  . 
           exports. 
    exports 
    FreeOrdAccumlnst, 
    GetOrdAccumlnst, 
    OrdAdd, 
    OrdDivide, 
    OrdMultiply, 
    OrdSubtract; 
              
DLL-.      DLL,  ,  
(  )   LoadLibrary,     
DLL-   .   ,     ,  
   LoadLibrary   . 
        LoadLibrary     
 25. " DLL: ". 

    614	 VI.   

        WRAPLIB.PAS     
 .     ,   
   . 

       DLL 

     ,       ,    
     (User Interface 
 UI).         DLL.   
               
,    -fr ?'" Wp.flP'KHH.f'  "" DLL.  
     DLL    
(     ). 

         DLL 

    ,   -      
 DLL. ,      Tyler's Toys   
   .      ,   
Internet   TCP/IP.   -  
  ,     Internet.   
   .       
   ,       
 ,    .   
      .    , 
   ,  DLL     
  * ''.  DLL   
  .     ,    
 DLL,   . 26.1-26.4. 

    . 26.1.    ,    
   

    . 26.2.           
     

    . 26.3.            

    . 26.4.              
                                                            " 


     26.  DLL:   615 

    ,        
,     ,  
   Windows.      
 ,         , 
    ,   Tyler'sToys,  
  DLL? 
          . ,  
,   Internet,     DLL   
 .       , 
   ,       
. ,        
Internet   DLL   ,    
  . 
     ,       , ,  
    640x480.      DLL, 
      ,  
   .    ,   
    ;   
 DLL. 

       

            DLL,   
   .    
 ,       
      .        
   ,   OK, Cancel  Abort, 
^    . 
      26.4        
 ,  ,    . 26.3. 

     26.4. \UDELPHi3\CHP26\EXPORTU. PAS.     
TToyinfoDlg,     

    procedure ShowToylnfoDlgModal(AppHand: THandle; Prompt: PChar); begin 
    Application.Handle := AppHand; 
    with TToyinfoDlg.Create(Application) do 
    try 
    SetPrompt(Prompt); 
    ShowModal;	; finally 
    Free;	j end;                                                               
    ! end; 

     ShowToylnfoDlgModal ,      
   TToyinfoDlg.  AppHand     
 .     Delphi,   
  Application. Handle.  DLL  
ShowToylnfoDlgModal       
  Application.  ,  -  
TToyinfoDlg     ,   
    ,     .   
   ,  AppHand   ,  
    .  Prompt   
SetPrompt    TToyinfoDlg.  SetPrompt  
 ,     Prompt,  Caption 
  TLabel  .    , 
   TToyinfoDlg,        
       . 
     . 26.2         
   Tyler's Toys.      
  ,   -*      26.4,  
      ,     
       . 
    ,  ShowToylnfoDlgModal    TToyinfoDlg, 
       Application.   
 ,   .    , 
 ShowToylnfoDlgModal    ShowModal  TToyinfoDlg. 
      .  -  
  ,        
   . 

    616	 VI.   

     . 26.4      .  
     ,  j        
     ,     
       ,     
.          26.5. 

     26.5. \UDELPHI3\CHP2 6XEXPORTU. PAS.  ,  
    1   

    function ShowToylnputDlgModal(AppHand: THandle; Prompt, pBuf: PChar; 
    Length: integer): Integer; begin 
    result := -1; 
    Application.Handle := AppHand; ---with TToylnputDlg.Create(Application) do try 
    SetPrompt(Prompt); result := ShowModal; 
    STrTTcbpy (pBuf, GetlnputBuf, Length) finally         ""  "~~ 
    Free; end; end; 

     AppHand      ,    . 
 ShowToylnputDlgModal    Prompt     
  -,      
      .    
pBuf.      PChar,     
 . ,      
  Length.   ,  ShowToylnputDlgModal  
  ,   .       
   Cancel. -1'ù*   
ShowToylnputDlgModal. 
        TToylnputDlg   
TToylnputDlg. SetPrompt,   Caption   
 IbMessageText  TLabel.    ShowModal, 
    .      
 ,      Cancel.     ShowModal 
  1,   Cancel  2.   ShowModal  
    ShowToylnputDlgModal. 
           GetlnputBuf.    
SetPrompt,    public       
  TToylnputDlg.    ,  
   edlnputEdit  TEdit.     
StrLCopy  ,      GetlnputBuf,  
,   pBuf  ShowToylnputDlgModal,  
 ,   Length.     
TToylnputDlg       Free. 
        ,     
  ,    . 
      26.6    interface ,  
   DLL.     public  
   StdCall.     exports,   
  .     
    implementation. ,   
   exports     25, " 
DLL: ". 

     26.6. \UDELPHI3\CHP26\EXPORTU. PAS.   
  DLL- FormsLib 

    interface 
    uses 
    Forms, SysUtils, WinTypes, TTInfoDlg, TTAboutDlg, TTInputDlg, TTSplashDlg; 
    procedure ShowToylnfoDlgModal(AppHand: THandle;	Prompt: PChar); StdCall; 
    function ShowToylnputDlgModal(AppHand: THandle;	Prompt, pBuf: PChar; 
    '-        Length: integer):	Integer; StdCall; 


     26.  DLL:   617 


    exports 
    ShowToylnfoDlgModal, ShowToylnputDlgModal; 
    implementation 


       DLL 

        DLL  ,     
  :    ,     .  
,        
  ,.  , .   
   ShowModlal_Bbn^HeHHe.nporpaM 
    Mbi  ,   .     
       Free,    
.  ,        ? 
       Show.   
  Free,  ,        
.        
 : 

    function     OpenToySplashDlg(AppHand:   THandle):   THandle;   StdCall; 
procedure   CloseToySplashDlg(hSplashlnst:   THandle);   StdCall; 

            StdCall.  
     exports   . 

         

     OpenToySplashDlg        
TToySplashDlg.   ,      
        
CloseToySplashDlg.     , ,  
    OpenToySplashDlg, JVJOCHO_ 1?0  
  .      
 CloseToySplashDlg.   OpenToySplashDlg  
  26.7 

    1  26.7. \UDELPHI3\CHP26\EXPORTU.PAS.   -  
-,     

    function OpenToySplashDlg(AppHand: THandle): THandle; begin 
    Application.Handle := AppHand; 
    result := THandle(TToySplashDlg.Create(Application)); 
    with TToySplashDlg(result) do 
    try 
    Show; except 
    Free; 
    Result := 0; v. Raise; end; end; 

       OpenToySplashDlg   AppHand  
 handle   Application.  AppHand   
,        TToySplashDlg.   
^, ^  .    
     " ,    
 . 
     result  ,   
TToySplashDlg. Create.     Thandle    
result.       ,   
  ,     32 . 
       TToySplashDlg   result 
     Show,    . 
   Show,    ShowModal,  .   
      ,  
,    except.     Free, 
 result   0,    
    . 

    618	 VI.   


         -^' 

    ,   OpenToySplashDlg, _?^ 
    THandle.     
    CloseToySplashDlg   . 
  CloseToySplashDlg    26.8. 

     26.8, \UDELPHI3\CHP26\EXPORTU.PAS.  CloseToySplashDlg 
     ,    
     try... except 

    procedure  CloseToySplashDlg(hSplashlnst:   THandle); begin 
    if hSplashlnst  <   1  then Raise  Exception.Create('hSplashlnst   is  not 
 valid1); with  TToySplashDlg(hSplashlnst)   do begin Close; Free; end; end; 

    ,   OpenToySplashDlg,   
CloseToySplashDlg  -MeTpe~K5pTasTiThst.     
 hSplashlnst  ( ).    
  hSplashlnst  ,       
   ,       
   . 
      hSplashlnst   TToySplashDlg.  
 Close    Free.   hSplashlnst   Close 
       CloseToySplashDlg  
  . 

       

     Delphi,     Object Pascal,  
     .  Object Pascal 
     , ,^ Component 
Object Model (COM)  Microsoft.      Delphi 
   ,    . 
     Delphi  Borland C++   ,   
 .      
  ,    . 
   . 

       

    -   Delphi  . DK^ejjnjwrJjcjmuca, 
 ,       . _?_ 
    i jjajwofiraigy   
(Virtual Method Table VMT).        
"*? ,    ,      
  ,   ,     
.  ,         
         , 
  . 

      
           Delphi,  , 
,  ,        .   
Delphijma  _. 

         (Virtual Table, VTable 
 VT).    ijil      C++  .  
          
     VMT.    ,  
  VMT    -. 
       (VMT)     .   
  ,     .    
  . 26.5. 

     26.  DLL:   619 

    
     			>	VMT 

    Addr Field!	Pointer			 .. 
    -8	vmtFreeln... 
    Addr Field2	Member Data 
    -4	vmtDestroy 
    Addr Field3	Member Data 
    0	 1 
    Addr Field4	Member Data 
    4	 2 
     .. 
    8	 3 
    12	 4 
     .. 
    
    
        VMT  ,   
    .     
 VMT,      inherited. 

    . 26.5. -   Object Pascal 


      Delphi 

      Delphi  -,    
 :  ,     
 (RTTI)  ,        
   .     
 ,        
 Delphi    .   : -._ 
         
VMT.  . 26.1  ,    
Delphi  VMT.       System, pas, 
   \DELPHI3\SOURCE\RTL\SYS. 
    "' 

     26.1.     

    			 

    vmtDestroy	-4	vmtDynamicTable	-36 
    vmt Free Instance	-8	vmtMethodTable	-40 
    vmtNewInstance	-12	vmtFieldTable	-44 
    vmtDefaultHandler	-16	vmtTypelnfo	-48 
    vmtSafeCallException	-20	vmtlnitTable	-52 
    vmtParent	-24	vmtAutoTable	-56 
    vmtlnstanceSize	-28	vmtlntfTable	-60 
    vmtClassName	-32	vmtSelfPtr	-64 


         DLL 

      VMT,    ,    
    Delphi 1.0;       
   Delphi 2.0  3.        
         
  DLL.          
.  Microsoft     DLL   
,   ActiveX.   C++    
      Delphi,  
   ,       , 
   C++. 
          " ",     
  ,       
,    . 
        Borland  C++ Builder   
        .   
           
 .   ,   (  
  VMT).       
    VMT.	~* 

    620  VI.   


        DLL     

            DLL ,  
 ,  .       
         
Delphi.          
  ,   DLL. 
         ,   DLL, 
  ,   ,    
   . 
    	      DLL. 
    	    DLL   . 
    	      DLL    . 
    	         DLL 
 . 
    	         , 
  DLL. 
            .   
  ,        
         . 

     TCommDebug 

    ,       
       .   
     ,   
   .    ,     
    .    TCommDebug 
         
,            
 ASCII. 

      
             
 .      ,    
  .    .    , 
  :     .    
     .    
   ,     - 
     .     
   ,  ,    StringOut   
'ATDP'+555-5555 (555-5555   )   . 
     ,    ! 

     ,     SHARED! . PAS, 
   26.9. 

     26.9. \UDELPHI3\CHP26\SHAREU1. PAS. ,    
   Win32 

    unit shareul; interface 
    uses 
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
Menus, StdCtrls, Mask, ComCtrls, Buttons, ExtCtrls; 
    type 
    TCommDebug = class (TObject) private 
    FhPort: THandle; public 
    procedure  StringOut(sValue: string); virtual; Stdcall; 
    constructor Connect(sComPort: String); 
    destructor  Destroy; override; end; 


     26.  DLL:   621 


    function GetComDebuglnstance(iComPort: integer): TCommDebug; StdCall; 
    exports 
    GetComDebuglnstance; 
    implementation 
    function GetComDebuglnstance(iComPort: integer): TCommDebug; begin    "*  
    result := TCommDebug.Connect(Format('COM%d',[iComPort+1])); end/procedure 
TCommDebug.StringOut(sValue: string); var 
    wCtr,wLen,wOffset : word; 
    TxBuf : array[0..255] of byte; 
    dwSent: DWord; begin 
    if strlen(PChar(sValue)) > 255 then 
    Raise Exception.Create('Value passed to TCommDebug.StringOut exceeds' + 
    ' 255 bytes limit'); 
    dwSent  := 0; 
    wOffset := 0; 
    if LongBool(Length(sValue))  then begin 
    wLen := Length( sValue ) ; 
    if wLen > 0 then 
    begin 
    for wCtr := 0 to wLen-1 do begin 
    TxBuft wCtr ] := ord( sValue[ wCtr +1] ); wOffset := wCtr; end; 
    TxBuf[ wOffset+1 ]	:= Ord(#13); 
    TxBuf[ wOffset+2 ]	:= Ord(#10); 
    TxBuf[ wOffset+3 ]	:= Ord(#0); 
    WriteFile( FhPort, TxBuf, wLen+2, dwSent, nil ); end; end; end; 
    constructor TCommDebug.Connect(sComPort: String); 
    var DCB: TDCB; 
    begin 
    inherited Create; 
    if Length(sComPort) = 0 then 
    Raise Exception.Create('Could not retrieve port settings.'); 
    FhPort := CieateF,ile(PChar(sComPort+':'),(Generic_Read + Generic_Write), 
0, nil, Open_Existing,0,0); 
    if FhPort = INVALID_HANDLE_VALUE then begin 
    FhPort := 0; 
    Raise Exception.Create('Open failed') end; 
    _GetCommState(FhPort,DCB); 
    with DCB do 
    begin 


    622	 VI.   


    BaudRate  := 9600; 
    ByteSize  := 8; // Databits : 4-8 
    Parity    := 0; // Parity   : 0-4 = none,odd,even,mark,space StopBits  := 
0; // StopBits : 0,1,2 = 1, 1.5, 2 end; 
    if not SetCommState( FhPort, DCB ) then 
    Raise"Sxception.Create('SetState error: '+IntToStr ( GetLastError() ) ); end; 
    destructor TCommDebug.Destroy; begin 
    if FhPort <> 0 then CloseHandle(FhPort); 
    inherited Destroy; end; 
    end. 

      interface  SHAREU1.PAS   uses  type.  
 type   TCommDebug,    private  
 FhPort,       . 
     : 

    procedure       StringOut(sValue:   string);   virtual;   Stdcall; 

       TCommDebug       
StringOut    ,    sValue.  
,   TCommDebug __   Connect: 

    constructor  Connect(sComPort:   String); 

           
( ;    . !),   . 
   ,     '  

    Inst   :=  TCommdebug.Connect('Com2');   //     
  2. 

    ,   TCommDebug     Destroy: 
destructor     Destroy;   override; 
      SHAREU1. PAS     DLL-, DLL 
_    TCommDebug.  
    SHAREU1. PAS: 

    function GetComDebuglnstance(iComPort:   integer):   TCommDebug;   StdCall; 

             
iComPort.  0     1, 1  .2  
..   GetComDebuglnstance     COOT--- 
 ,      TCommDebug. 

      
      Shareul .pas,    26.9,  
. ,      ,  
    .  Win32  
-^^.^^ _,0 
-   . ,      
 ,       Win32 API. 


      TCommDebug 

       TCommDebug   Shareul .pas  DLL- 
         DLL.  
 ,   , , .  ,     
,       DLL-.    , 
   .   ? 
     -  ^ .  , , 
   ,   SffringOut   ^!: 

    procedure  StringOut(sValue:   string);   virtual;   Stdcall; 

         ,      
-.     TCommDebug   
    VMT (  ). 

     26.  DLL:   623 

          
GetComDebuglnstance,     TCommDebug,  
   ,   .    , 
         VMT.    
  VMT     StringOut. 

       

            
DLL   ,   GetComDebuglnstance, 
 .      ,   
 ,  ,  ,  ,     
    StringOut  VMT.     
  . 
             
-    ( Delphi   
   ).     , 
'  GetcomDebuginstance,  *  .  
,          
 self,   .      
   ,       
 .     , ,    
.       ,    
 Delphi ( ). 
    ,     DLL,    
GetComDebuglnstance,   . ,   
 (, ),      .  
 TCommDebug   DLL,      . 
   -"" GetComDebuglnstance, 
    ,       TCommDebug 
 : 

    function GetComDebuglnstance(iComPort:   integer):   TCommDebug;   
StdCall;   external 'Shareit.dll'; 

              
 (    StringOut).   StringOut 
     .    
,    ,  . 
           ,    
 ,       .   
         ,   
 . 
      ,         
,      DLL,      
_    ,    .  
 TCommDebug   ,    TObject.  
,   StringOut     VMT.  VMT 
     - (  
)    ,     ,   
 . 
     ,    TCommDebug     
TCustomComm,  1_ ,   TCommDebug. 
StringOut        VMT, , , . 
    ,   GetComDebuglnstance,   
VMT    StringOut,   .   
  26.10,     . 
      interface  LoadComul .pas   TCommDebug. 
, , ,    FhPort,  Connect  
 Destroy  .      ,  
       DLL.   
TCommDebug       ,      
       StringOut. 

     26.10. \UDELPHI3\CHP2 6\LOADCOMUl. PAS.      
     TButton.  StringOut   Buttoniciick 

    unit LoadComul; interface 
    uses 
    Windows, Messages, SysUtils, Classes, Graphics, 


    624	 VI.   


    Controls, Forms, Dialogs, StdCtrls; 
    type 
    TForml = class(TForm) 
    Buttonl: TButton; 
    procedure ButtonlClick(Sender: TObject); private 
    {  private. } public 
    {  public. } end; 
    TCommDebug = class(TObject) public 
    procedure  StringOut(sValue: string); virtual; Stdcall; abstract; 
end;	'"'""             " 
    var 
    Forml: TForml; 
    function GetComDebuglnstance(iComPort: integer): TCommDebug; StdCall; 
    external 'Shareit.DLL' implementation 
    {$R *.DFM} 
    procedure TForml.ButtonlClick(Sender: TObject); 
    var CommDebug: TCommDebug; 
    begin 
    CommDebug := GetComDebuglnstance (2); ~ try	'' {^define PhoneDialer} 
//1.__ , 
    //     . {$ifdef PhoneDialer} 
    //   ,    . 
CommDebug.StringOut('ATDP5555555, ,'); {$else} 
    CommDebug.StringOut(',   '); {$endif} finally 
    CommDebug.Free; end; end; 
    end. 

    ,    26.10,    
     Buttonl  .   
ButtonlClick    CommDebug  TCommDebug.  
  ,    
GetComDebuglnstance.      StringOut,  
  try. . . finally,      . 

           1 . 
         , ifdef,  
,    StringOut      
         . 

       ,   ,     
ATDP-.  'ATDP'       
 .   ' 5555555 '    . 
       _, ' '  
 .       
 :  ,      ( 
   ),    . 

     26.  DLL:   625 

           Free, ,   , 
   TCommDebug.     
 ,         , 
    DLL. 

      :   

           .  
 . 
    1.     TCommDebug     StringOut. 
          
 . 
    2.    TCommDebug   DLL-. 
    3.   DLL    GetComDebuglnstance,  
  TCommDebug . Connect      
  (  ). 
    4.        TCommDebug,   
  StringOut. 
    5.       DLL  GetComDebuglnstance. 
    6.    ButtonlClick      GetComDebuglnstance, 
   DLL   TCommDebug. 
    7.     StringOut  ,    
      Free. 
         .     
  StringOut  ?  .  ,  
       VMT,       
 . ~]" ,     
   VMT ,   GetComDebuglnstance. 
   VMT,         
     . 

       

      25  ,      Win 
16  Win32.      DLL Win32   ,  
  DLL   ,     DLL.  
, ,     DLL,    
       ,  
     DLL.   ,   
   Win 16. 
     Win 16  DLL        
 .        ,   
        .   
       . 
     Win32         , 
      DLL  .   Win 16 DLL  
    ,   Wiri32'taKOfl   
.   ,    ,    
  B^Vin32  ' ,     
   DLL.      
       ,   
      DLL-      
  . 

         Windows 

        Win32    .  
           
(RAM).  RAM      
      ,   RAM. 
       """'"   
   .     ,  
-      DLL.   Windows   
 ,        
  . 
         ,    
,  ,    , :   
     ,       
       ,     
 .     .      
    .    , 

    626	 VI.''  

         .   
     ,   
      . 
            
 .         
   -   . 

        

       (File-Mapping Object)     
CreateFileMapping. 

    function CreateFileMapping(hFile:   THandle; 
    IpFileMappingAttributes:   pSecurityAttributes; flProtect,   
dwMaximumSizeHigh,   dwMaximumSizeLow:   DWORD; IpName:   PChar):   THandle;   stdcall; 

     hFile   ,   .   
  ,  
_?_$1:Є(-,MAXDWQRD,    
WINDOWS. )1 IpFile-MappingAttributes   
!~11*    ,   
.  flProtect   ,   
 .        
PAGE_READWRITE.  dwMaximumSizeHigh  dwMaximumSizeLow  
      64-  , 
    .    
         
,   . 
     IpName    ,      
      (  ,     
). ,      CreateFileMapping.  
   _GMem,   ..  . , 
,',.,,   ^  .   
   ..     
CreateFileMapping     ,   
  . 

      

              
  .     MapViewOf File. 

    function  MapViewOfFile(hFileMappingObject:   THandle; 
    dwDesiredAccess:   DWORD; dwFileOffserHigh,   dwFileOffserLow, 
dwNumberOfBytesToMap:   DWORD):   Pointer;   stdcall; 

     hFileMappingObject  ,   
CreateFileMapping.  dwDesiredAccess     .  
     FILE_MAP_WRITE.  
dwFileOffserHigh  dwFileOffserLow  64-  , 
   ,     .  
           . 
 dwNumberOfBytesToMap   ,    . 

     
      dwNumberOfBytesToMap   0,  
MapviewOfFile    ,   
dwMaximumSizeHigh  dwMaximumSizeLow    CreateFileMapping. 
      ,     
    MapviewOfFile.     
 ,  dwNumberOfBytesToMap    1. 

         MapviewOfFile   
 ,     nil.     
  ,      . 

          

                
     .     
  UnMapViewOfFile.   WINDOWS. PAS    
 : 
    function UnMapViewOfFile(IpBaseAddress:   Pointer):   BOOL;   stdcall; 


     26.  DLL:   627 

     IpBaseAddress   ,  (  
)  MapViewOf-File.      
UnMapViewOf File  True.  ,    
  .      CloseHandle, 
   WINDOWS . PAS  : 

    function CloseHandle(hObject:   THandle):   BOOL;   stdcall; 

     hObject  ,  (  
)  CreateFileMapping.      
CloseHandle  True. 

         DLL 

    ,   DLL,     
  ,       
. DLL      ,  
      . , 
  DLL,      , 
  DLL. 
      ,        
        
  Windows. DLL   ,  
     .   26.11. 

     26.11. \UDELPHI3\CHP26\SHAREHEAP.DPR.   
       DLLMain 

    library ShareHeap; 
    uses 
    Forms, 
    SysUtils, 
    Windows; 
    const Key: String = '_GMem'; MaxSize = $FFFF; 
    var 
    hObjHand: THandle; pGMem: pointer; 
    procedure UnmapMemory; begin 
    if Assigned(pGMem) then begin 
    UnMapViewOfFile(pGMem); pGMem := nil; end; 
    if hObjHand > 0 then CloseHandle(hObjHand); 
    hObjHand := 0; end; 
    procedure MapMemory(dwAllocSize: DWORD); begin 
    hObjHand := CreateFileMapping(MAXDWORD,nil,PAGE_READWRITE, 
    0,dwAllocSize,PChar(Key)); 
    if (hObjHand = 0) then 
    Raise Exception.Create('Could not create file-mapping object1); 
    pGMem := MapViewOfFile(hObjHand,FILE_MAP_WRITE, 0,0,1); 
    if not Assigned(pGMem) then 
    begin 
    UnmapMemory; 


    628	 VI.   


    Raise  Exception.Create('Could not map   file'); end; end; 
    procedure  DLLMain(dwReason:   DWORD); begin 
    case  dwReason of dll_Process_Attach: begin 
    pGMem   :=  nil; hObjHand   :=  0; MapMemory(MaxSize); end; dll_Process_Detach: 
    UnmapMemory; end; end; 
    function GetGBlock:   Pointer;   StdCall; begin 
    result   :=  pGMem; end; 
    exports 
    GetGBlock; 
    begin 
    DLLProc   :=   SDLLMain; 
    DLLMain(dll_Process_Attach) end. 

       26.11   .  
  DLLMain    DLLProc.  
  DLL         
 .   DLL     dll_Process_Attach  
 .       Delphi  
DLLMain, ,     ,    25, 
" DLL: ". 
      DLLMain  dwReason    case. . .of. 
     dll_Process_Attach, DLL   
  .      ,  
    MapMemory ( ). 
       dwReason   dll_Process__Detach, 
DLL   ;       ,  
   .       
  UnmapMemory. 
        ? DLL  ,    
        .   
      DLL.   DLL   
      .     
  DLL,          
   DLL  . 
    ,      GetGBlock, 
 .     DLL    
GetGBlock, ,         . 
    DLL   ,     
GetGBlock,    ,    . 
         , 
        DLL. 

         

         SHAREHEAP. DLL   
     ,      
 GetGBlock    .    
 SHAREHEAP.DLL     .    
GetGBlock       ,  
.        , 
          
.  26.12      
 SHAREHEAP. DLL. 

     26.  DLL:   629 


     26.12. \UDELPHi3\CHP26\LoadShareHeapUl. PAS. -  
  TBut-   !  ton  TTimer    
 SHAREHEAP . DLL 

    unit LoadShareHeapUl; interface 
    uses 
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
StdCtrls, ExtCtrls; 
    type 
    TForml = class(TForm) 
    Memol: TMemo; 
    GroupBoxl: TGroupBox; 
    WriteButton: TButton; 
    Editl: TEdit; 
    Timerl: TTimer; 
    procedure FormCreate(Sender: TObject); 
    procedure WriteButtonClick(Sender: TObject); 
    procedure TimerlTimer(Sender: TObject}; private 
    {  private. } public 
    {  public. } end; 
    var 
    Forml: TForml;      ft  A pcBuf: PChar;     ,  &.?,, 
    function GetGBlock: Pointer; StdCall; external 'SHAREHEAP.DLL' 
    implementation 
    ($R *.DFM} 
    procedure TForml.FormCreate(Sender: TObject); begin 
    pcBuf := GetGBlock; end; 
    procedure TForml.WriteButtonClick(Sender: TObject); begin 
    Editl.GetTextBuf(pcBuf, Length(Editl.Text)+1); end; 
    procedure TForml.TimerlTimer(Sender: TObject); begin 
    Memol.SetTextBuf(pcBuf); end; 
    end. 

            GetGBlock  
 SHAREHEAP.DLL,       
    .      
   pcBuf. 
        ,   Edit   .  
       Write   
WriteButtonClick.      Edit      
  ,     pcBuf.  
      Write     
 Memo.       

    630	 VI.   

     TTimer,    1 000   TimerlTimer.  
    Memo ,       
,     pcBuf. 
         ,     
 LoadShareHeap,   LoadShareHeapUl .pas,     
   DLL- ShareHeap  \/>.. 
___13.     Edit    
     Write,      Memo  
 (  ),    ,      . 

     - 

        Win32     
 (Portable Executable  ).     ,  
    ,     .   
  DLL  ,         . 
,    ,     
 DLL     ,      
. ~~~ 
         (    
    DLL  )     
    Delphi.     
 ^         
(   -    ).  
, ,    ,     
  Turbo Dump,       . 

     
            ,  
   ,    ,   ,  
  . 

      (  DLL)      
  ,      . 
       DLL      
   , -  .  
     . : -"   
    (  Windows),    
   ,    (    
 ).        DLL  
,        .   
,       DLL,    
  .    :    
   (  ). 
            DOS ( 
 MZ).  DOS     ,   
     DOS     
 DOS, ,  ,       ,  
   -, Microsoft Windows. 
      DOS       ,  
  .    DOS, NT/Win95   OS/2. 
     ,     
  ,        NT 
 Win95.       , 
    ,      ,  
   . 
         ,  &  .  
    ,   ,   ,  
 ,    .. 
           .  , 
     .     
   :  ,  
   ,  ,   
  ,    ..      
     (   ). 
            
      .    
 ,      
.    ,      
        . 

     26.  DLL:  	631 

             
  ,  -      
  .    ^  LoadLibrary  
LoadLibraryEx    THandle,   
hlnstance.  Win32        
   . 
      ,   Delphi  ,  
$00400000.          
 .       )  
(,       DLL),     
 ,       , 
 ,   .   
  , ,  , . 
     Delphi,    ,    
      Linker   Project 
Options.       Proi-    ect^Options 
             
     ( ).   
    1 .  Win32    
         
  .       
    ,     
  I ,      
.           
 ,        , 
,  ,          
 . 

     

             
    ,      
 ,      . 
         16-  
.  ,   ,   , 
      .     
 ,  ,        .. 
             
 ,         
    (). 
    ~'"i  16-  Delphi     
     4  $.   , 
,  PRELOAD ( ),  -I   
 ,  ,   .   
  -j;  ,   PRELOAD,  .  
     ,      
PRELOAD     . 
            
   . 
    	$00000020 ( ) 
    	$20000000 () 
    8	$00000040 (  ) 
    	$40000000 ( ) 
    	$10000000 (  ) 
    	$80000000( ) 
     , ,  $10000000 (  
).      ,  
    .  ,  Delphi   
    . 

    632	 VI.   

      25, " DLL: ", ,   
LoadLibraryEx         
Load_Library_As_DataFile.    ,   
        .  
         ( 
  ),       , 
    LoadLibraryEx  Load_Library_As_DataFile. 
     -stance,  
  . 
    ,    ,    
    ,  ,  
    . Delphi, ,      
      .  Delphi 
  . "" 
       CODE (  Microsoft ,  , . text).  
.  Win 16      .  Win32  
     .    BSS (  Microsoft 
,  , .bss).  ,    . 
       DATA (  Microsoft ,  , . data).  
,   . 
       .rsrc.  ,   ,   
 . 
       .idata.         
     .    .edata.     
     ,  . 
       .reloc.         
     .      
   ,     
   ,       
 . 
              
  Turbo Dump,      .  
  DLL    . idata  . edata. 
     ,     
  (      ).   
(    )   ,   
,    ,  Turbo Dump  Microsoft Quick 
Viewer.     ,    
 ResXplor,   Delphi.      
     . 
          ,   
      ,    
Portable Exe,cutable_Bi^ceMicrosoft Dj;velo?er_Network(M.SDNJI.   
    Microsoft  Web.  ,  
         
   ,    ,    
  ResXplor,    
\Delphi3\Demos\ResXplor.       
 .	*....,-^ 

    Turbo Dump 

    ^ Delphi  C++      Turbo 
Dump.     DOS  DUMP. ,    
     \bin.      
 Turbo Dump  4.2.15.2. 
     Turbo Dump ( TDump    )   
           
 . TDump     . 
    	   DOS 
    	 New Executable (NE) 
    	   
    	   () 
    	 OMF (OBJ  LIB) 
    	 COFF (OBJ  LIB) 
    	   Borland  Microsoft 

     26.  DLL:   633 

    ,        ,   . 
      ,    DLL. 
     TDump      .  
    ,      TDump  
 <Enter>.     TDump   : 

    TDUMP MyFile.exe 

     MyFile.exe    ,   .  
          
  : 

    TDUMP  -h  c:\Win95\System\GDI32.DLL 

          .    
  ,    .   (-) 
    ,  (-)    
 ASCII   (-h)      . 
         DLL     TDUMP  
      .    
   ,       
 .   ,   , 
,     . , ,  
   . 
    ,      Turbo Dump   DLL? 
       .    
  DLL ,    (  
GetPro-cAddress)     .   :   
   . 
       ,       . 
   . 
        DLL   exports,    . 
       -    ,    ,   
   . 
       ,    ,      DLL 
,  DLL    C++,      . 
       ,    ,      DLL 
,  DLL    C++,      
   _cdecl      
  . 
       , ,    
 .    ,   
  (, ,     ).     
TDump    .     ,   ,  
  . 
         .     
 ,      .  
 . idata  . edata.        ), 
    . idata      
       TDump.    
 .  da t ^ .      
  (  ). 
      26.13    TDump   . idata 
  Windows GDI32 . DLL   \WINDOWS\SYSTEM. 

     26.13. GDI32. .  TDump   
   

    Imports from KERNEL32.dll 
    SMapLS_IP_EBP_24(hint = Olee) 
    38 
    89 
    FT_Exitl2(hint = 0076) 
    FT_Exit20(hint = 0078) 
    GetSystemDefaultLCID(hint = 012b) HeapCreate(hint = 0168) GetOEMCP(hint = 
0102) RtlUnwind(hint = Ole8) 
    Imports from ADVAPI32.dll 
    RegCreateKeyExA(hint = 0082) RegQueryInfoKeyA(hint = 0098) 


    634	 VI.   


    RegDeleteKeyA(hint  =   0085) 
    RegSetValueExA(hint   =   ) 
    RegQueryValueExA(hint  =  009d) RegCloseKey(hint =  007e) RegOpenKeyA(hint 
 =  0094) 

     ,         
.    Imports from ( ) KERNEL32 .dll 
   Imports from ADVAPI32. dll.   GDI32 . DLL 
    KERNEL32 . DLL       
  ,     ,   .  
 ,   ,  GDI32.DLL  DLL, , ,  
 ,   .   26.14  
  TDump   . edata   Windows GDI32 . 
DLL   \WINDOWS\SYSTEM. 

     26.14. GDI32 ..  TDump   , 
    ;     
      

    Exports   from GDI32.dll 
    330  exported name(s),   335  export  addresse(s).     Ordinal base  is   
100. Ordinal   RVA	Name 
    0005	000024ab	AbortDoc 
    0006	0000245b	AbortPath 
    0007	000028be	AddFontResourceA 
    0008	00007549	AddFontResourceW 
    0324	000021a5	TextOutW 
    0325	00003208	TranslateCharsetlnfo 
    0326	00002493	UnrealizeObject 
    0327	0000210	UpdateC-olors 
    0328	000088ba	UpdatelCMRegKey 
    0329	000088al	UpdatelCMRegKeyA 
    0330	00007564	UpdatelCMRegKeyW 
    0331	0000247f	WidenPath 
    0332	00006fe2	gdiPlaySpoolStream 
    0333	OOOlbOaS	pfnRealizePalette 
    0334	0001bOa4	pfnSelectPalette 

     GDI32.DLL   300 .    
   .     ,   
DLL    ,  ,      TDump  
 . idata  ,      . edata  DLL 
     .      . 
     TDump         , 
    -  .     
 ,       , 
  Notepad.   DOS       

    TDump  -e   GDI32.DLL  >  GDI32.TXT. 

        TDump    
.            
.           
,  TDump   .   26.15   
 TDump    Windows GDI32 . DLL.   
,  .     ,   
  ,    (   
 ).       . 

     26.15. \UDELPHI3\CHP26\GDI32 ..  TDump    
,       

    Turbo Dump  Version 4.2.15.2 Copyright () 1988, 1996 Borland 
International Display of File C:\WINDOWS\SYSTEM\GDI32.DLL 
    Old Executable Header 


     26.  DLL:  	635 


    DOS File Size	20000h	(131072.	) 
    Load Image Size	450h	(  1104.	} 
    Relocation Table entry count                        OOOOh	(    0.	) 
    Relocation Table address	0040h	(    64.	) 
    Size of header record	(in paragraphs)           0004h	(    4.	) 
    Minimum Memory Requirement	(in paragraphs)            OOOOh	(     0.	) 
    Maximum Memory Requirement	(in paragraphs)            FFFFh	( 65535.	) 
    File load checksum	OOOOh	(    0.	) 
    Overlay Number	OOOOh	(     0.	) 
    Initial Stack Segment  (SS:SP)	0000:0088 Program Entry Point    (CS:IP)    
             0000:0000 
    Portable Executable (PE) File Header base: 00000080 
    CPU type	80386 
    Flags	210E [ executable backwards 32bit library ] 
    DLL flags	0000 [ ] 
    Linker Version	2.32 
    Time stamp	2FF383BC 
    0/S Version	1.0 
    User Version	0.0 
    Subsystem Version	4.0 
    Subsystem	0002 [ Windows GUI ] 
    Object count	00000007 
    Symbols offset	00000000 
    Symbols count	00000000 
    Optional header size    OOEO 
    Magic #	10B 
    Code size	00019600 
    Init Data size	00006600 
    Uninit Data size	00000000 
    Entry RVA	00002B6C 
    Image base	BFF30000 
    Code base	00001000 
    Data base	0001BOOO 
    Object/File align	00001000/00000200 
    Reserved	00000000 
    Image size	00025000 
    Header size	00000400 
    Checksum	0002CB01 
    Stack reserve/commit    00100000/00001000 
    Heap reserve/commit     00100000/00001000 
    Number interesting RVAs  00000010 
    Name	RVA      Size 
    Exports	0001EOOO	00002099 
    Imports	0001DOOO	OOOOOA40 
    Resources	00022000	00000394 
    Exceptions	00000000	00000000 
    Security	00000000	00000000 
    Fixups	00023000	000016B4 
    Debug	00000000	00000000 
    Description	00000000	00000000 
    TLS	00000000	00000000 
    Callbacks	00000000	00000000 
    reserved	00000000	00000000 
    reserved	00000000	00000000 
    reserved	00000000	00000000 
    reserved	00000000	00000000 
    reserved	00000000	00000000 


    636	 VI.   


    Object table #  Name		VirtSize	RVA	PhysSize	Phys off	Flags 
    01	.text	00019580	00001000	00019600	00000400	60000020	[CER] 
    02	.data	00001910	0001BOOO	00001AOO	00019AOO	D0000040	[ISRW] 
    03	. idata	OOOOOA40	0001DOOO	OOOOOCOO	0001B400	50000040	[ISR] 
    04	. edata	00002099	0001EOOO	00002200	0001COOO	40000040	[IR] 
    05	GPFIX	0000011C	00021000	00000200	0001E200	D0000040	[ISRW] 
    06	. rsrc	00000394	00022000	00000400	0001E400	40000040	[IR] 
    07	. reloc	000017FO	00023000	00001800	0001E800	42000040	[IDR] 
    Key to section flags:   contains code D  discardable E  executable 
    I  contains initialized data R  readable S  shareable W  writeable 
    Imports from KERNEL32.dll 
    SMapLS_IP_EBP_24(hint = Olee) 38 
      .. 

     

           .  
       DLL.   , 
        Delphi  . 
      . 
    1.         DLL   
      . 
    2.         DLL     ,  
           . 
    3.        .   
VMT,          
,      . 
    4.          
            
 ,   . 
    5.        DLL,    
   ,      , 
,   . 
    6.    ,     Turbo Dump (T Dump)   
   DLL. 

     26.  DLL:   637 
