Solved

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

Posted on 2004-09-30
4
222 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
  • 2
4 Comments
 
LVL 17

Expert Comment

by:Wim ten Brink
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

12 Experts available now in Live!

Get 1:1 Help Now