Com development - passing objects

fire_fly
fire_fly used Ask the Experts™
on
Yet another question from my first big com project...

I have a com component, with a method that needs to take two parameters.  One is a string containing certain connection details, and the other is a connection (TADOConnection) that needs to connect using them.  The problem is: what type do I declare the second parameter to be, how do I make my connection object cast as that type, and how do I cast it back?

Thanks people,
Firefly

Comment
Watch Question

Do more with

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

Commented:
You can use the type long, thats a 32bit number

Param2: long;
ADO   : TADOConnection;

Param2 := integer(pointer(ADO));

and the other way...

ADO := TADOConnection(pointer(Param2));


I havent tested any of this, but I hope it helps ;)

Author

Commented:
I still get a "Read of address 00000000" error...
What type of com server do you have? If it is the external then you can't pass the object because windows can't do the marshalling for such data types. And you better pass the interface to your ADO connection
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Author

Commented:
how do I know if it's external?  and how do I get an interface to the connection?
Sorry fire_fly, I'm stuppid. Even if your com server is of internal type you still can't simply pass object :(
Internal server is the com object inside Dll and external is inside EXE.


You should to create the com server which will provide some inteface to ADOConnection.

Commented:
I assumed it was in-process since you wanted to pass a pointer. You can only pass a pointer if your COM object is in a DLL. If it's an .exe your program and the COM object will be running in different processes, and you cant use passed pointers.


Author

Commented:
ok, the server is a dll, not an exe.  

And I've tried typecasting the TADOConnection to an IUnknown, to a pointer, and to a pointer, which I typecast to a long, and all of these give me a null reference at the other end.  I know I can't pass the object directly, that's why I'm asking this question.  I want to know what I need to cast it as, or how to create something that will work.
Sorry, I was mistaken, even if your com-server is in-process you can't do it. Because actually the code of the com-server is running not by your application, but by the
libraries of the com kernel. So you can't pass your object.
As I said the only way is to create the com server which will provide the interface for TADOConnection class and then you will be able to pass IDispatch interface.

Author

Commented:
How do i create this?  I can't re-declare TADOConnection to implement an interface, it is a built-in type.  how do I get an interface from a native delphi object?
You havn't to redaclare TADOConnection. You may create the interface wrapper for your TADOConnection object and implement all methods you needed. Why want you pass the TADOConnection object to your function? What then do you do with it? May be set some params and calls some methods, so define the interface which will has appropriative methods and properties than in coclass implementation you may simply call the methods of TADOConnection object.
Commented:
ok, the TADOConnection has a property on it called ConnectionObject, which implements IDispatch, so it can be passed through COM.  This can then be used to call the Execute method, or set the Properties on the ADOConnection.

Thanks for your help everyone, but as I found this myself, I can't accept an answer for this q.
fire_fly:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.

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