COM Interoperability in .NET: Part 3
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   UnCategorized
Current Lesson:
COM Interoperability in .NET: Part 3
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  COM Interoperability in .NET: Part 2 [next Lesson]  COM and Component Creation in C#
COM Interoperability in .NET: Part 3
  by: ggarung

COM Interoperability in .NET: Part 3

by: ggarung

Build a .NET Client that calls a COM Server

Introduction:

In this article I cover the area how to use a COM server in a .NET client. Existing COM components are precious resources to your managed applications. So now let us observe how you can build a .NET Client that uses a COM Server.

The steps involved in the build process are as follows:

  1. Write and compile the unmanaged code.
  2. Generate an assembly containing definitions of the COM types using the tlbimp.exe utility so that allow the .NET client to interact with the exposed types.
  3. Install the assembly in the global assembly cache. (Optional)
  4. Write and compile the .NET client code that reference the assembly containing the COM type definitions.
1. Write and compile the unmanaged code.

Open an ActiveX DLL project workspace in VB 6.0 and put the below code in code window and name the project as VBServer and Class to Add
Public Function Add (ByVal x As Integer, ByVal y As Integer) As Integer
         Add = x + y
End Function
Finally save your workspace and compile the VBServer to VBServer.dll

In the OLE/COM Object Viewer we can see the name of default custom interface(Add) as well as a number of other COM interfaces implemented by VB.

2. Importing the Type Library:

The .NET Framework needs metadata for individual COM types at both compile time and run time. Your choice for generating metadata is the Type Library Importer (TlbImp.exe) utility generates an assembly containing metadata. When you carry out this tool on a type library it generates a standard runtime callable wrapper, based on the contents of the type library.

tlbimp VBServer.dll /out:NetClient.dll
Now we create the NetClient.dll using the Type Library Importer utility.

If you open the NetClient.dll with the ILDasm.exe.

In that You notice the _Add interface and Add coclass are mapped as .NET equivalents.

3. Install the assembly in the global assembly cache. (Optional)

To make assembly to be shared among several applications, it must be installed in the global assembly cache (GAC). Use gacutil.exe to install an assembly in the GAC.
gacutil /i NetClient.dll
4. Write and compile the .NET client code with reference to the assembly

During compilation we have to reference the assembly using the compiler /r switch or we can add reference to the project directly from Visual Studio.NET development tool.

Even by using Reflection one can use the COM server (Late Binding).I planned to cover this topic in another article.
csc TestClient.cs /r: NetClient.dll
The .NET client:(Early Binding)
namespace Addvbserver
{
    using System;
    using NetClient;
    public class TestClient
    {
        public static int Main(string[] args)
        {
            Add c = new Add();
            Console.WriteLine(c.Add(2,2));
            return 0;
        }

    } 
} 
In the above code You notice that all members of a Add interface are accessed directly from an object instance. If you want to explicitly reference the underlying _Add interface you have to write the code as below.
namespace Addvbserver
{
    using System;
    using NetClient;
    public class Client
    {
        public static int Main(string[] args)
        {
            Add c = new Add();
            _Add gg =c;
            Console.WriteLine(gg.Add(2,2));
            return 0;
        }

    } 
} 
Conclusion:

I hope after reading the three parts regarding Interoperability issues one can gain some good knowledge over communication between managed and unmanaged world and vice versa.

About the Author: G.GNANA ARUN GANESH is the Administrator and the Founder of ARUN MICRO SYSTEMS (www.arunmicrosystems.netfirms.com). He has been programming in C++, Visual Basic, COM, Java and Microsoft Technologies for 3+ years. Arun's background includes Bachelor degree in ECE.He is one of the Top authors writing articles in C# and .NET in various websites. He is an Active person in the panel of Technical Reviewers in Prentice Hall Publishers and Sams Publication. In .NET the last book he reviewed is the "C# how to program" written by Harvey and Paul Deitel. You can contact him for technological support and queries through ggarung@rediffmail.com

1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  UnCategorized
Current Lesson:
COM Interoperability in .NET: Part 3
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  COM Interoperability in .NET: Part 2 [next Lesson]  COM and Component Creation in C#


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
C#fanatic 2
Al_Pennywo.. 2
helper 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center