Accesing a COM object (created by Delphi) from VC++

Hi,

If a COM object is written in Delphi, it should be accessible by VC++ , right?

A customer is asking something about an error he is getting... something about different calling conventions between C++ and pascal.

But shouldn't this be handled correctly simply because my Delphi COM object is a COM object?
ycompAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wim ten BrinkSelf-employed developerCommented:
No, the calling convention determines how method parameters are passed between caller and callee. It is likely that the Delphi COM methods are defined as either safecall or stdcall, while the C++ code is expecting the other calling convention. The result? A wrong calling convention.
The COM object should have a type library and VC++ should create code with the proper calling convention to these Delphi methods, but apparantly something goes wrong.
0
gwalkeriqCommented:
Yes, I've accessed COM objects writtin in Delphi from VC++ before.

However, customer must be using it as a COM object, not a DLL call. There are different calling conventions between VC++ & Delphi when you interface via DLL's, basically you cannot (easily at least) using object interfaces, only undecorated (classless) functions and procedures.

As COM interfaces, there is no problem.
0
ycompAuthor Commented:
so alex, are you saying that it is common for this to happen even though there is a type library?
0
gwalkeriqCommented:
I know Workshop_Alex is very knowledgable, but I'll give this a try. COM calling conventions are language independent, a com object has a specific layout that all COM implementer must follow. The interface callinging conventions are defined by the Windows API and all COM interfacing. COM interfaces are implement in Windows in terms of a VTABLE like structure but this is discoverable at runtime by calls to DllGetClassObject or QueryInterface (to get the interface reference). The actual parameter passing is done via parameter marshalling. Marshalling is compiler generated code to pull/pull stack paramaters and translate them into the internal structures the are defined by the COM interface

Now, the difference comes if you implement the COM object as an in-process server (DLL), or out of process server (EXE). Of course, DCOM is always out of process too. Use an out of process server and you have no problem with Pascal vs. C++ (or VB, or VBScript, etc.)

I not sure if COM goes through the the marhsalling step with an in-process server (but my recollection is that is does not marshall, it just uses a function call.)  I've not used in-process COM servers with multiple languages for caller/callee

Type libraries can apply to out of process servers too. It is used as a mechanism to build get/set functions, etc. instead of having to use the generic (and slower) iDispatch interfaces when manipulating a COM object. Of course, this assumes you have dual interfaces so you can have option for early (compile time) bind vs. late (iDispatch) bind.

Perhaps if you compile the C++ program without using the type-library, then marshalling will kick in  and things work correctly -- this is just a stab in the dark though.

Hopefully, this helps to explain, and is perhaps closer to what I should have said initiiallly, COM is not a cross language problem if you use out of  process server (exe) instead of in-process server (DLL)

You might find these links useful too.

http://www.sei.cmu.edu/str/descriptions/com_body.html
http://weblogs.asp.net/oldnewthing/archive/2004/02/05/68017.aspx
http://arcgisdeveloperonline.esri.com/ArcGISDeveloper/ArcGISDevHelp/DevelopmentEnvs/COM/IntroToCOM.htm

You should definitely have a copy of Delphi COM programming if writing COM from Delphi. For COM itself, Inside COM was very good. Neither of these is easy reading because COM guts are very ugly/complicated.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.