Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: COM object written in C#, Consumed in Delphi and in C++ Builder


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Mar 18, 2018 3:43 PM Last Post By: Remy Lebeau (Te... Threads: [ Previous | Next ]
Noam Weissman

Posts: 1
Registered: 5/3/17
COM object written in C#, Consumed in Delphi and in C++ Builder  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 17, 2018 7:29 AM
Hi,

I am trying to connect a C++ application to a COM object written in C#.

After lots of hours testing numerous examples that did not solve my problem I found a simple and nice video:
http://chapmanworld.com/2015/09/03/com-object-written-in-c-consumed-in-delphi/

I am not able to do the same in C++ builder ????

First in Delphi there is a variable: DotNetClass : OleVariant;
And the code: DotNetClass := CreateOleObject('TestClassNameSpace.ComClass');

If I am doing the same in C++ I get warnings about ambiguity. CreateOleObject function is defined in several places.
After straggling I added #include <System.Win.ComObj.hpp> and now the above compiles in C++ (CPP code :-) )

Now I need to access the function inside the C# COM object. In Delphi it is simply:
DotNetClass.GetSum(Var1, Var2) ... compiles and works fine !!

In C++ what ever I do it does not even compile :-)

What am I missing here ? Any ideas would be appreciated !

BR,
Noam.

Alex Belo

Posts: 626
Registered: 10/8/06
Re: COM object written in C#, Consumed in Delphi and in C++ Builder  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 17, 2018 10:25 PM   in response to: Noam Weissman in response to: Noam Weissman
Noam Weissman wrote:

I am not able to do the same in C++ builder ?

Have you tried importing of type library? (I rarely work with COM and
always use this method)

It will create a unit for you which you can use directly + provide a
"C++ builder way" clue how to do it manually.

--
Alex
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: COM object written in C#, Consumed in Delphi and in C++ Builder  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 18, 2018 3:43 PM   in response to: Noam Weissman in response to: Noam Weissman
Noam Weissman wrote:

I am not able to do the same in C++ builder ????

Anything you can do in Delphi, you can do in C++Builder, too. Though
it may need to be done in a slightly different way, due to differences
in the way Delphi and C++(Builder) operate.

First in Delphi there is a variable:
DotNetClass : OleVariant;
And the code:
DotNetClass := CreateOleObject('TestClassNameSpace.ComClass');

If I am doing the same in C++ I get warnings about ambiguity.
CreateOleObject function is defined in several places. After
straggling I added #include <System.Win.ComObj.hpp> and now the above
compiles in C++ (CPP code :-) )

An alternative is to use the Variant::CreateObject() method, which
plays into the next step...

Now I need to access the function inside the C# COM object. In Delphi
it is simply: DotNetClass.GetSum(Var1, Var2) ... compiles and works
fine !!

In C++ what ever I do it does not even compile :-)

C++ does not support late binding of COM methods, like Delphi does.
When you access a COM propery, or call a COM method, on an IDispatch or
(Ole)Variant variable, Delphi generates code to delegate to the
IDispatch::Invoke() method for you. C++ does not do that, you have to
call Invoke() manually in your own code.

If you use the (Ole)Variant class to access the COM object, you can use
the OleProcedure(), OleFunction(), and OleProperty(Get|Put)() methods,
which are wrappers for IDispatch::Invoke(). For example:

Variant DotNetClass = CreateOleObject("TestClassNameSpace.ComClass");
//or: Variant DotNetClass =
Variant::CreateObject("TestClassNameSpace.ComClass");
DotNetClass.OleFunction(Var1, Var2);


--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02