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

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
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
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server