Walkthrough: Creating COM Objects with Visual Basic .NET

xiaoxiao2021-03-06  42

Although .NET assemblies are the best choice for new applications, you may at times need to employ COM objects instead This walkthrough demonstrates two ways to create COM objects with Visual Basic .NET:. With and without the COM class template.

The easiest way to create COM objects is by using the COM class template. The COM class template creates a new class, then configures your project to generate the class as a COM object and register it with the operating system.

To create a com Object useing the com class template

Open a new Windows Application project by clicking New on the File menu, and then clicking Project. The New Project dialog box appears. With Visual Basic Projects highlighted in the Project Types list, select Class Library from the Templates list, and then click OK. The new project is displayed. Select Add New Item from the Project menu. The Add New Item dialog box is displayed. Select COM class from the Templates list, and then click Open. Visual Basic .NET adds a new class and configures the new project for COM interop. Add code, such as properties, methods, and events to the COM class. Select Build Solution from the Build menu. Visual Basic .NET builds the assembly and registers the COM object with the operating system.

Creating COM Objects without The COM CLASSI

You can create a com class manually instead of using the com class template. This Procedure Can Be Helpful When Working from the Command Line or When You Want More Control Over How Com Objects Are Defined.

To set up your provject to generate a com object

Open a new Windows Application project by clicking New on the File menu, and then clicking Project. The New Project dialog box appears. With Visual Basic Projects highlighted in the Project Types list, select Class Library from the Templates list, and then click OK. The new project is displayed. In the Solution Explorer, right-click your project, and then click Properties. The Property Pages dialog box is displayed. Click the Configuration Properties folder, and then click the Build folder. The Build properties are displayed. Check The Register for Com Interop Check Box.to Set Up Up Up Your Class To Create a Com Object

.. In the Solution Explorer, double-click Class1.vb to display its code Rename the class Add the ComClass attribute to the class, specifying the guids for the Class ID, Interface ID, and events Events ID as in the following example: < Comclass (Comclass1.ClassID, Comclass1.Itersid, COMCLASS1.EVENTSID)> _

Public Class ComClass1 Supply the Globally Unique Identifier (GUID) constants for the ComClass attribute. To create GUIDs for your COM object, click Create GUID on the Tools menu, or launch guidgen.exe to start the guidgen utility. Select Registry Format from the list of formats provided by the guidgen application. Click the New GUID button to generate the GUID and click the Copy button to copy the GUID to the clipboard. Paste the GUID into the Visual Studio .Net code editor. Remove the leading and trailing braces from the . GUID provided For example, if the GUID provided by guidgen is "{2C8B0AEE-02C9-486e-B809-C780A11530FE}" then the GUID should appear as:. "2C8B0AEE-02C9-486e-B809-C780A11530FE" Click the New GUID and Copy buttons until you have generated GUIDS for the ClassId, InterfaceId, and EventsId for example:. Public Const ClassId As String = "832F34A5-5CF5-403f-B4A8-428C8351FD02" Public Const InterfaceId As String = "3D8B5BA4-FB8C-5ff8-8468 -11BF6BD5CF91

Public Const EventsId As String = "2B691787-6ED7-401e-90A4-B3B9C0360E31" COM classes must have a parameterless Public Sub New () constructor, or the class will not register correctly Add a parameterless constructor to the class:. Public Sub New ( )

Mybase.new ()

End Sub Add properties, methods and events to the class, ending it with an End Class statement. Select Build Solution from the Build menu. Visual Basic .NET builds the assembly and registers the COM object with the operating system.

Note The Com Objects You Generate with Visual Studio .NET Cannot Be Used by Other Visual Studio .NET Applications. Attempts To Add References To Such Com Objects Raise An Error.

转载请注明原文地址:https://www.9cbs.com/read-79817.html

New Post(0)