现在要开始研究com编程,哪位大侠能给我一些com开发的技巧? to [email protected] 收到即结帖

解决方案 »

  1.   

    看看com方面的书把  《Delphi COM深入编程》  什么的
    library FirstCom;uses
      ComServ,
      NumIntf in 'NumIntf.pas',
      FirstCom_TLB in 'FirstCom_TLB.pas',
      Servercom in 'Servercom.pas' {Number: CoClass};exports
      DllGetClassObject,
      DllCanUnloadNow,
      DllRegisterServer,
      DllUnregisterServer;{$R *.TLB}{$R *.RES}begin
    end.unit FirstCom_TLB;// ************************************************************************ //
    // WARNING                                                                    
    // -------                                                                    
    // The types declared in this file were generated from data read from a       
    // Type Library. If this type library is explicitly or indirectly (via        
    // another type library referring to this type library) re-imported, or the   
    // 'Refresh' command of the Type Library Editor activated while editing the   
    // Type Library, the contents of this file will be regenerated and all        
    // manual modifications will be lost.                                         
    // ************************************************************************ //// PASTLWTR : $Revision:   1.130.1.0.1.0.1.6  $
    // File generated on 2005-2-18 11:26:43 from Type Library described below.// ************************************************************************  //
    // Type Lib: D:\Program Files\Borland\Delphi6\Projects\Mycom\FirstCom.tlb (1)
    // LIBID: {6FCD684E-9390-4753-B9CB-995F1019DF58}
    // LCID: 0
    // Helpfile: 
    // DepndLst: 
    //   (1) v2.0 stdole, (C:\WINNT\system32\stdole2.tlb)
    //   (2) v4.0 StdVCL, (C:\WINNT\system32\stdvcl40.dll)
    // ************************************************************************ //
    {$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers. 
    {$WARN SYMBOL_PLATFORM OFF}
    {$WRITEABLECONST ON}
    {$VARPROPSETTER ON}
    interfaceuses Windows, ActiveX, Classes, Graphics, StdVCL, Variants;
      // *********************************************************************//
    // GUIDS declared in the TypeLibrary. Following prefixes are used:        
    //   Type Libraries     : LIBID_xxxx                                      
    //   CoClasses          : CLASS_xxxx                                      
    //   DISPInterfaces     : DIID_xxxx                                       
    //   Non-DISP interfaces: IID_xxxx                                        
    // *********************************************************************//
    const
      // TypeLibrary Major and minor versions
      FirstComMajorVersion = 1;
      FirstComMinorVersion = 0;  LIBID_FirstCom: TGUID = '{6FCD684E-9390-4753-B9CB-995F1019DF58}';  IID_INumber: TGUID = '{31443187-126F-4685-8637-DEA926384905}';
      CLASS_Number: TGUID = '{20DBA0F3-48D1-48A2-98BA-20D10EA098C4}';
    type// *********************************************************************//
    // Forward declaration of types defined in TypeLibrary                    
    // *********************************************************************//
      INumber = interface;// *********************************************************************//
    // Declaration of CoClasses defined in Type Library                       
    // (NOTE: Here we map each CoClass to its Default Interface)              
    // *********************************************************************//
      Number = INumber;
    // *********************************************************************//
    // Interface: INumber
    // Flags:     (256) OleAutomation
    // GUID:      {31443187-126F-4685-8637-DEA926384905}
    // *********************************************************************//
      INumber = interface(IUnknown)
        ['{31443187-126F-4685-8637-DEA926384905}']
      end;// *********************************************************************//
    // The Class CoNumber provides a Create and CreateRemote method to          
    // create instances of the default interface INumber exposed by              
    // the CoClass Number. The functions are intended to be used by             
    // clients wishing to automate the CoClass objects exposed by the         
    // server of this typelibrary.                                            
    // *********************************************************************//
      CoNumber = class
        class function Create: INumber;
        class function CreateRemote(const MachineName: string): INumber;
      end;implementationuses ComObj;class function CoNumber.Create: INumber;
    begin
      Result := CreateComObject(CLASS_Number) as INumber;
    end;class function CoNumber.CreateRemote(const MachineName: string): INumber;
    begin
      Result := CreateRemoteComObject(MachineName, CLASS_Number) as INumber;
    end;end.unit NumIntf;interface
    type
      INumber = interface
       ['{1B67B6A8-22B1-4886-806A-82E445221CEE}']
       function GetValue : integer ;stdcall;
       procedure SetValue(Value : integer);stdcall;
       procedure increase ;stdcall;
    end;
    implementationend.
    unit Servercom;{$WARN SYMBOL_PLATFORM OFF}interfaceuses
      Windows, ActiveX, Classes, ComObj, FirstCom_TLB, StdVcl;type
      TNumber = class(TTypedComObject, INumber)
      private
        fvalue : integer;  protected
      public
        function GetValue: Integer; virtual; stdcall;
        procedure SetValue (New: Integer); virtual; stdcall;
        procedure Increase; virtual; stdcall;
        procedure Initialize; override;
        destructor Destroy; override;
      end;implementationuses ComServ;{ TNumber }destructor TNumber.Destroy;
    begin  inherited;
    end;function TNumber.GetValue: Integer;
    begin
      result := fvalue;
    end;procedure TNumber.Increase;
    begin
      inc(fvalue);
    end;procedure TNumber.Initialize;
    begin
      inherited;
       fvalue :=1;
    end;procedure TNumber.SetValue(New: Integer);
    begin
      fvalue := New;
    end;initialization
      TTypedComObjectFactory.Create(ComServer, TNumber, Class_Number,
        ciMultiInstance, tmApartment);
    end.
    客户端调用的代码
    unit client;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls,ComServ, Spin,ComObj;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Button4: TButton;
        Label1: TLabel;
        Label2: TLabel;
        SpinEdit1: TSpinEdit;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;  INumber=interface(Iunknown)
      ['{ECB3ADF3-24A5-4442-99B2-4A5446744B25}']
      function GetValue:integer;stdcall;
      procedure SetValue(Value : integer);stdcall;
      end;
    var
      Form1: TForm1;
      Num1,Num2  : iNumber;
      class_number : TGUID='{7DDBEFCF-E3F0-49BF-8BA8-C599DBCFEE27}';
    implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
      Num1 := CreateComObject(Class_Number) as INumber;
      Num1.SetValue(SpinEdit1.Value);
      Label1.Caption := 'Num1: ' + IntToStr (Num1.GetValue);
      Button1.Enabled := True;
      Button2.Enabled := True;
       
      // create second object
      Num2 := CreateComObject (Class_Number) as INumber;
      Label2.Caption := 'Num2: ' + IntToStr (Num2.GetValue);
      Button3.Enabled := True;
      Button4.Enabled := True;end;end.
    光看代码挺烦的
      

  2.   

    找本<<delphi6 開發人員指南>>
    or
    <<delphi7 從入門到精通>>
    可有入門的知識