Solved

Com example in Mastering Delphi 5

Posted on 2001-06-30
3
3,022 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now