4.6. Using ActiveX Components in a Project

If the ActiveX object you need to reference provides a type library and is registered on your computer, you can create a reference to the component using the References dialog. This includes the component's properties, methods, and events in the Intellisense drop-down list and provides statement completion for its properties, methods, and events. Furthermore, this enables you to early bind to the component's classes.

If the ActiveX object either doesn't provide a type library, or you don't know at design time which classes of a component you will need, you should use late binding.

4.6.1. Manually Registering and Unregistering ActiveX Components

There are occasions when you need to use an ActiveX component in your project, and the DLL is available on your machine, but it isn't registered. In this situation, you must manually register the component before you can use it. Fortunately, ActiveX components are designed to be self-registering: every ActiveX DLL contains information about itself that Windows can write to the registry using a program called RegSvr32.exe. For example, to register a DLL called myServer.DLL :

  1. Click the Windows Start button and select Run.

  2. Type RegSvr32 c:windowssystem32myserver.dll, or the precise path of the DLL. If you don't know where the DLL is stored, you can locate it using Explorer, type RegSvr32 in the Run dialog, then select the DLL and drag it to the text box in the Run dialog; the complete path (with no spelling mistakes!) is entered into the Run dialog for you.

  3. Click OK.

  4. A dialog box should then be displayed informing you of the success or failure of the component registration.

There are also occasions, particularly when you are developing remote server applications, when you need to unregister components on your local machine. The procedure is almost the same as that to register a component, the only difference being that you add the -U switch (for unregister). For example:

RegSvr32 -U c:windowssystem32myserver.dll

4.6.2. Early Binding

Early binding is the most efficient method of accessing an ActiveX component. This is because the reference to the Type Library for the component can be compiled into your project, giving your project an almost instant navigational path to the properties, methods, and events of the component, and allowing memory to be allocated based on the type library. To provide early binding for a component, you must use the References dialog to add a reference to the object to your project. You can then use either of two methods of instantiating the object:

Dim oObjVar As New myClass

or:

Dim oObjVar As myClass
Set oObjVar = New myClass

4.6.3. Late Binding

Late binding is flexible in terms of coding but is much less efficient at runtime because references to objects have to be resolved on the fly. To implement a late bound object, you should declare an object variable as an Object or a generic object, and then use the CreateObject function to assign a reference to the object variable:

Dim oObjVar As Object
Set oObjVar = CreateObject("myComponent.myClass")

or, in VB6:

Set oObjVar = CreateObject("myComponent.myClass", "DEVSVR1")

As you can see, late binding allows you to specify the name of the class (and in VB6, the name of the server where the component resides). However, late bound component properties, methods, and events are not available for statement completion, because your project knows nothing about the structure of the classes at design time.

For more information about using object references, see the entries for the CreateObject function, the Dim statement, the Friend statement, the GetObject function, the Private statement, the Public statement, and the Set statement in Chapter 7.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.22.26