Com development - debugging again

fire_fly
fire_fly used Ask the Experts™
on
OK, for more details, see a previous question I asked, with a very similar title...

I am writing a com component, in Delphi, and attempting to debug it through the IDE.  I have written a test app, and set it up to run with the component, to allow debugging, which doesn't work.  For any tips on that one, please answer my other question.

I have, for now, used log files to debug.

This question is hopefully more simple.  My app is calling a CoClass.Create method, to attain an Interface, then calling my method on the interface.  The latter procedure runs, and hits a null object, which I create in the Creator.  I put some debug info into the creator, and have discovered that it's not being called at all.  The com component is obviously linked to the correct Dlphi Object, as the method I have created is running, (and creating log files) but the creator is not running.  Any ideas why this could happen?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Try moving to the afterinitialize.....
I don't know the full workings but I think COM uses the NewInstance metod to create new obejcts and create never gets called.....

Author

Commented:
sorry, I didn't understand that.  how can the create never get called?  That's what it's there for, surely?  how do I modify it to make it work?  
Software Engineer, Advisory
Top Expert 2005
Commented:

fire_fly,
When you say that your creator never gets called, can you be more specific? (I don't want to make assumptions)
Are your trying to say that the Create() you have set up never gets called? If this is this case, then Lummie is correct, it will never get called.

The reason it won't get called is due to the fact that it is getting created via COM, and not though a statement such as:

foo:=TFoo.Create()

If you need to perform logic when the object is created , (via COM) then you should provide an Initialize procedure for the object that is overridden.

Ex:

type
 TFoo = class(TAutoObject, IFoo)
 private
   //
 protected
   //
 public
   procedure Initialize; override;
 end;


procedure TFoo.Initialize;
begin

 // Do startup logic

end;

----

Russell

Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Author

Commented:
sorry Lummie, I wish I could split the points here, but Russel gave me the full answer as to how to correct the problem...

Author

Commented:
oh dear... the initialize procedure doesn't seem to be called either...  I've just put another method onto the Interface, which I call after create... that seems to be working.
Russell LibbySoftware Engineer, Advisory
Top Expert 2005

Commented:

fire_fly,

Initialize will not get called if creating the object via:

foo:=TFoo.Create()

It WILL get called when the factory creates an instance of the COM class object though. It's similar to the problem you had with the Create() not getting called, which is due to the fact that TComObject does not perform an inherited Create().

foo:=TFoo.Create() <- Delphi object, Create() gets called, Initialize does not.

foo:=CoFooCreate <- COM object, Initialize gets called, Create does not.

So it really depends on the context that the object is being used. If you are creating instances of these COM objects internally, and wish them to perform the same logic as they do when created externally, then you should probably be using the class funtions that are created for you in your tlb wrapper, ex:

class function CoFoo.Create: IFoo;
begin
  Result := CreateComObject(CLASS_Foo) as IFoo;
end;

-----

Russell

Author

Commented:
foo: Tfoo;
foo := Tfoo.Create; //is calling Create, as expected.

foo: IFoo;
foo := coFoo.Create; //is NOT calling Initialize.

Initialize declared as

procedure TFoo.Initialize;
begin
// Do stuff
end;


is Initialize supposed to be public, private or protected?

Russell LibbySoftware Engineer, Advisory
Top Expert 2005

Commented:

It should be exposed as public with override. (It is a virtual method exposed in TComObject's public section)

Question: What does your class interface typedef look like?

ie
type
  TFoo = class(TSomething, IFoo)

TSomething should be one of TComObject, TAutoObject, TTypedComObject.

Russell

Author

Commented:
that's what I assumed, and that's what I did, public, override...  hmmm...

it descends from TTypedComObject.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial