Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi prevent click fast 2 242
Printing problem 2 102
JAudiorecorder record freezing the app 29 76
Delphi Ado Query To Converts Joined Columns Into Rows Based on Primary Key 4 69
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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