Walkthrough: Creating COM objects with Visual Basic. net

Source: Internet
Author: User

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 using the com class template

  1. Open a new windows application project by clickingNewOnFileMenu, and then clickingProject.New projectDialog box appears.
  2. WithVisual Basic ProjectsHighlighted inProject typesList, selectClass LibraryFromTemplatesList, and then clickOK. The new project is displayed.
  3. SelectAdd new itemFromProjectMenu.Add new itemDialog box is displayed.
  4. SelectCom classFromTemplatesList, and then clickOpen. Visual Basic. net adds a new class and configures the new project for com InterOP.
  5. Add code, such as properties, methods, and events to the com class.
  6. SelectBuild SolutionFromBuildMenu. Visual Basic. Net builds the Assembly and registers the COM object with the operating system.
Creating COM objects without the com class template

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 project to generate a COM Object

  1. Open a new windows application project by clickingNewOnFileMenu, and then clickingProject.New projectDialog box appears.
  2. WithVisual Basic ProjectsHighlighted inProject typesList, selectClass LibraryFromTemplatesList, and then clickOK. The new project is displayed.
  3. In the Solution Explorer, right-click your project, and then clickProperties.Property pagesDialog box is displayed.
  4. ClickConfiguration PropertiesFolder, and then clickBuildFolder.BuildProperties are displayed.
  5. CheckRegister for com InterOPCheck box.

To set up the code in your class to create a COM Object

  1. In the Solution Explorer, double-click class1.vb to display its code.
  2. Rename the class.
  3. AddComclassAttribute 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.InterfaceId, ComClass1.EventsId)> _Public Class ComClass1
  4. Supply the Globally Unique Identifier (guid) constants forComclassAttribute. To create guids for your COM Object, clickCreate guidOnToolsMenu, or launch guidgen.exe to start the guidgen utility. SelectRegistry formatFrom the list of formats provided by the guidgen application. ClickNew GuidButton to generate the guid and clickCopyButton 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 shoshould appear:"2C8B0AEE-02C9-486e-B809-C780A11530FE".
  5. ClickNew guidAndCopyButtons 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"
  6. Com classes must have a parameterlessPublic Sub New()Constructor, or the class will not register correctly. Add a parameterless constructor to the class:
    Public Sub New()   MyBase.New()End Sub
  7. Add properties, methods and events to the class, ending it withEnd ClassStatement. SelectBuild SolutionFromBuildMenu. Visual Basic. Net builds the Assembly and registers the COM object with the operating system.

    NoteThe 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.