Classic COM to
.NET Interoperability
Building a Managed C++
Client using Early Binding
Note |
To
work with any of these samples, you will need the
following: .........................................Microsoft .NET SDK .........................................Microsoft Visual Studio.NET Beta 2 or higher |
With early binding you import complete type information about a COM class at compile time. The compiler takes this information from the metadata assembly that is generated for the COM class.
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 QuoteClient, so the main source is called QuoteClient.cpp.
2. Develop the QuoteClient.cpp File
Clients that use COM objects should import
metadata. In C++ you should use #using directive. The syntax is:
#using "filename"
where filename is the name of the metadata
assembly.
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
#using "QuoteServer.dll"
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.
QuoteClient.cpp | ||
|
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: QuoteClient, Configuration: Debug Win32 ------ Deleting intermediate files and output files for project 'QuoteClient', configuration 'Debug|Win32'. Compiling... stdafx.cpp Compiling... AssemblyInfo.cpp QuoteClient.cpp Generating Code... Linking... Build log was saved at "file://c:\MyProjects\Cornucopia\ClassicCOM\QuoteClient\Debug\BuildLog.htm" QuoteClient - 0 error(s), 0 warning(s) ---------------------- Done ---------------------- Rebuild All: 1 succeeded, 0 failed, 0 skipped |
5. Run the Client
Command Prompt |
C:\MyProjects\Cornucopia\ClassicCOM\QuoteClient\Debug>QuoteClient
Usage: QuoteClient <symbol> Assuming Stock symbol is iCommware and proceeding... The price of iCommware is : US $186.4 C:\MyProjects\Cornucopia\ClassicCOM\QuoteClient\Debug>QuoteClient MSFT The price of MSFT is : US $62.8 C:\MyProjects\Cornucopia\ClassicCOM\QuoteClient\Debug>QuoteClient SUNW The price of SUNW is : US $66.6 C:\MyProjects\Cornucopia\ClassicCOM\QuoteClient\Debug> |
|
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
to
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 (https://gsraj.tripod.com/) or mail him at gopalan@gmx.net. |
This site was developed and is maintained by Gopalan Suresh Raj This page has been visited
|
Last Updated : Dec 27, '01 |
Copyright (c) 1997-2001, Gopalan Suresh Raj - All rights reserved. Terms of use. |
All products and companies mentioned at this site are trademarks of their respective owners. |