Solved

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

Posted on 2004-09-30
4
225 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

929 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

15 Experts available now in Live!

Get 1:1 Help Now