Classic COM to
Building a Managed C++ Client using Late Binding
Gopalan Suresh Raj
work with any of these samples, you will need the
.........................................Microsoft .NET SDK
.........................................Microsoft Visual Studio.NET Beta 2 or higher
Late binding is implemented by using the Namespace Reflection mechanism.
1. Create a Visual C++, Managed C++ Application Project
Create a Visual C++, managed Application project. In our sample we use a managed C++ class as the client. To create the client you can use the Visual Studio AppWizard. Choose 'Managed C++ managed Application' as the target, I called the project cppLateBinding, so the main source is called cppLateBinding.cpp.
2. Develop the cppLateBinding.cpp File
Clients that use COM objects should import
metadata. In C++ you should use #using directive. The syntax is:
where filename is the name of the metadata
Metadata should be accessible at compile time as well as at run time, so for simplicity, lets place QuoteServer.dll in the folder where your client code is located, as well as in the debug and release subfolders. As an alternative you can put the assembly in the folder with the client executable and reference it from source using a relative path.
Add the following line to QuoteClient.cpp file
At runtime the program finds the metadata assembly, uses this information to create a Runtime Callable Wrapper (RCW) object that is responsible for:
The RCW itself is a managed object so it is garbage collected. To import the library namespace, you should use the using namespace directive. Add the following line to your code
using namespace SimpleStocks;
Now you can use object the same way you use any managed CLR class.
To import metadata at runtime the Type class
from the System namespace is used. The Type class has a static method
GetTypeFromProgID("ProgID") that returns a Type object for a COM object based on
To obtain an instance of a COM object we use the static member CreateInstance(Type type) of the Activator class from System namespace. We pass the Type object that we got at the previous step as a parameter.
Now we can call the methods of our COM object using the InvokeMethod member of our Type object.
3. Configure your Project Property Pages with the right information
Identify the folder that contains the QuoteServer.dll Assembly.
4. Build the Project Files
Rebuild All started: Project: cppLateBinding, Configuration: Debug Win32
Deleting intermediate files and output files for project 'cppLateBinding', configuration 'Debug|Win32'.
Build log was saved at "file://c:\MyProjects\Cornucopia\ClassicCOM\cppLateBinding\Debug\BuildLog.htm"
cppLateBinding - 0 error(s), 0 warning(s)
---------------------- Done ----------------------
Rebuild All: 1 succeeded, 0 failed, 0 skipped
5. Run the Client
Usage: cppLateBinding <symbol>
Assuming Stock symbol is iCommware and proceeding...
The price of iCommware is : US $186.4
The price of MSFT is : US $62.8
The price of SUNW is : US $66.6
|Classic COM to .NET Interoperability|
|Developing a Classic COM Server Component using C# and .NET - The QuoteServer Example|
|Developing a Classic COM Client Application using Visual Basic ver 6.0 to access the QuoteServer - The VB6Client Example|
|Developing a Managed C++ Client Application which binds to the Server using Early Binding - The QuoteClient Example|
|Developing a Managed C++ Client Application which binds to the Server using Late Binding - The cppLateBinding Example|
Download the entire source code as a zip file.
click here to go
My Advanced C#/.NET Tutorial Page...
|About the Author...|
|Gopalan Suresh Raj is a Software Architect, Developer and an active Author. He has co-authored a number of books including "Professional JMS", "Enterprise Java Computing-Applications and Architecture" and "The Awesome Power of JavaBeans". His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia© site (http://gsraj.tripod.com/) or mail him at email@example.com.|
This site was developed and is maintained by Gopalan Suresh Raj
This page has been visited times since December 27, 2001.
Last Updated : Dec 27, '01
All products and companies mentioned at this site are trademarks of their respective owners.