Solved

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

Posted on 2004-09-30
4
230 Views
Last Modified: 2010-04-05
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?
0
Comment
Question by:ycomp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12195399
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
 
LVL 6

Expert Comment

by:gwalkeriq
ID: 12195413
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
 

Author Comment

by:ycomp
ID: 12195699
so alex, are you saying that it is common for this to happen even though there is a type library?
0
 
LVL 6

Accepted Solution

by:
gwalkeriq earned 500 total points
ID: 12196191
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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