Solved

Com example in Mastering Delphi 5

Posted on 2001-06-30
3
3,039 Views
Last Modified: 2012-05-04
I am learning Delphi 5 com tech.
In the book Mastering Delphi5, there is a sample(page 698).

I tried the sample, but when the client side program run the intruction: Num1 := CreateComObject (Class_Number) as INumber; It got an error message: 'EIntfCastError: Interfacenot supported'.
I have registered the server, and I checked  the regstry, it looks right.

The following are the units' code:

/////////////Client Side ////////////////////
unit TestComF;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Spin, NumIntf;

// redeclare the server GUID
const
  Class_Number: TGUID =
  '{2970D631-68F3-11D5-8384-8B6680C10D27}';

type
  TForm1 = class(TForm)
    SpinEdit1: TSpinEdit;
    Button1: TButton;
    Button2: TButton;
    SpinEdit2: TSpinEdit;
    Button3: TButton;
    Button4: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Button5: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    Num1, Num2 : INumber;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses
  ComObj;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // create first object
  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;

procedure TForm1.Button1Click(Sender: TObject);
begin
  // change
  Num1.SetValue (SpinEdit1.Value);
  Label1.Caption := 'Num1: ' + IntToStr (Num1.GetValue);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  // increase
  Num1.Increase;
  Label1.Caption := 'Num1: ' + IntToStr (Num1.GetValue);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  Num2.SetValue (SpinEdit2.Value);
  Label2.Caption := 'Num2: ' + IntToStr (Num2.GetValue);
end;


procedure TForm1.Button4Click(Sender: TObject);
begin
  Num2.Increase;
  Label2.Caption := 'Num2: ' + IntToStr (Num2.GetValue);
end;

procedure TForm1.Button5Click(Sender: TObject);
var
  Num3: INumber;
begin
  // create a new temporary COM object
  Num3 := CreateComObject (Class_Number) as INumber;
  Num3.SetValue (100);
  Num3.Increase;
  ShowMessage ('Num3: ' + IntToStr (Num3.GetValue));
end;

end.


///////////////Server side ////////////////////////////
library FirstCom;

uses
  ComServ,
  NumIntf in 'NumIntf.pas',
  NumServ in 'NumServ.pas',
  FirstCom_TLB in 'FirstCom_TLB.pas';

exports
  DllGetClassObject,
  DllCanUnloadNow,
  DllRegisterServer,
  DllUnregisterServer;

{$R *.TLB}

{$R *.RES}

begin
end.

unit NumIntf;

interface
  type
    INumber = interface
      ['{2970D630-68F3-11D5-8384-8B6680C10D27}']
      function GetValue: integer; stdcall;
      procedure Setvalue (new: integer); stdcall;
      procedure Increase; stdcall;
    end;

implementation

end.


unit NumServ;

interface

uses
  Windows, ActiveX, Classes, ComObj, NumIntf;

type
  TNumber = class(TComObject, INumber)
  private
    fValue : integer;
  public
    function GetValue: integer; virtual; stdcall;
    procedure Setvalue (new: integer); virtual; stdcall;
    procedure Increase; virtual; stdcall;
    procedure Initialize; override;
    destructor Destroy; override;
  end;

const
  Class_Number: TGUID = '{2970D631-68F3-11D5-8384-8B6680C10D27}';

implementation

uses ComServ;

{ TNumber }

destructor TNumber.Destroy;
begin
  inherited;
  MessageBox (0, 'Object Destroyed',
    'TDLLNumber', mb_OK); // API call
end;

function TNumber.GetValue: integer;
begin
  Result := fValue;
end;

procedure TNumber.Increase;
begin
  Inc (fValue);
end;

procedure TNumber.Initialize;
begin
  inherited;
  fValue := 10;
end;

procedure TNumber.Setvalue(new: integer);
begin
  fValue := New;
end;

initialization
  TComObjectFactory.Create(ComServer, TNumber, Class_Number,
    'Number', 'Number server', ciMultiInstance, tmApartment);
end.


Peter
0
Comment
Question by:petershaw8
  • 2
3 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 6242157
It works fine here. Try reregistering it using:

Run menu > Register ActiveX Server
0
 
LVL 13

Accepted Solution

by:
Epsylon earned 100 total points
ID: 6242194
Btw, do you have the lastest Delphi 5 patches installed?
0
 

Author Comment

by:petershaw8
ID: 6244410
Hi Epsylon,
I installed Delphi 5 patche files.
I tried to reregister it (Run menu > Register ActiveX Server)and used regsvr32.exe in windows\system as well. But it still doesn't work.


Peter
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question