Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
programming4us
Windows 7

Visual Basic 2010 : Exposing .NET Objects to the COM World

7/15/2011 6:29:16 PM
Although in modern world applications this practice is less frequent than in the past, you can expose .NET objects to the COM world. For example, a VB 6 application can consume an object like this. To demonstrate how you accomplish this export, create a new class library and rename Class1.vb to Contact.vb. The first thing you need to do to make a class consumable from COM is enable the COM interoperability support. Now open My Project and then select the Compile tab. Flag the Register for COM Interop item at the bottom of the page, as shown in Figure 1.
Figure 1. Registering an assembly for COM Interoperability.

This operation tells Visual Studio that it needs to register the COM component on build and adds the following line of code in AssemblyInfo.vb so that it makes it visible to COM:

<Assembly: ComVisible(True)>

Class Requirements for COM Exposure

Any class that you want to expose to COM has the following requirements: It must have a public, empty, parameterless constructor, any member, including types, to be exposed must be Public (no other modifiers are allowed), and it cannot include abstract classes. (This is just because they cannot be consumed.)


The ComVisible attribute establishes the visibility level and granularity not only at assembly level, but also for classes and class members. At the moment implement the Contact class as follows:

Public Class COMContact
Public Property FirstName As String
Public Property LastName As String
Public Property Email As String
Public Property BirthDay As Date

Public Sub New()

End Sub
End Class

Now you can decide the visibility level for each member in the class by decorating the class and its members with the System.Runtime.InteropServices.ComVisible attribute. The following code demonstrates how to make COM-visible only some members from the Contact class:

Imports System.Runtime.InteropServices


<ComVisible(False)>
Public Class COMContact

<ComVisible(True)>
Public Property FirstName As String
<ComVisible(True)>
Public Property LastName As String
<ComVisible(True)>
Public Property Email As String

<ComVisible(False)>
Public Property BirthDay As Date

Public Sub New()

End Sub
End Class

The class is marked as ComVisible(False) simply because not all its members are COM-visible. Notice that a public, empty constructor is required for COM-visible objects.

The next step should be to register the COM component after the build process. Fortunately, on the development machine Visual Studio 2010 does the work for you. (This requires the IDE to be launched with elevated privileges.) Therefore, simply compile the project to have a class library that is consumable from the COM architecture.

Other -----------------
- Visual Basic 2010 : Importing and Using COM Objects
- Microsoft Visio 2010 : Applying Color and Fill Patterns
- Microsoft Visio 2010 : Using the Auto Align & Space Feature
- Managing the Windows 7 Environment : Configuring Remote Connections (part 3) - Configuring a VPN Connection
- Managing the Windows 7 Environment : Configuring Remote Connections (part 2) - Remote Desktop
- Managing the Windows 7 Environment : Configuring Remote Connections (part 1) - Remote Assistance
- Microsoft Visio 2010 : Aligning and Spacing Shapes
- Microsoft Visio 2010 : Using the Organization Chart Wizard with New Data
- Configuring the Windows 7 Operating System (part 3) - Understanding the System Icon & Using the Registry Editor
- Configuring the Windows 7 Operating System (part 2) - Configuring Windows Aero & Using Control Panel
 
 
Top 10 video Game
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 windows Phone 7 windows Phone 8
programming4us programming4us
 
Popular keywords
HOW TO Swimlane in Visio Visio sort key Pen and Touch Creating groups in Windows Server Raid in Windows Server Exchange 2010 maintenance Exchange server mail enabled groups Debugging Tools Collaborating
programming4us programming4us
PS4 game trailer XBox One game trailer
WiiU game trailer 3ds game trailer
Trailer game
 
programming4us
Natural Miscarriage
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Game Trailer