You can use LabVIEW as an ActiveX client to access the objects, properties, methods, and events associated with other ActiveX-enabled applications. LabVIEW also can act as an ActiveX server, so other applications can access LabVIEW objects, properties, and methods. Refer to ActiveX and LabVIEW at ni.com for more information about using ActiveX with LabVIEW.
ActiveX-enabled applications include objects that have exposed properties and methods that other applications can access. Objects can be visible to the users, such as buttons, windows, pictures, documents, and dialog boxes, or invisible to the user, such as application objects. You access an application by accessing an object associated with that application and setting a property or invoking a method of that object.
Events are the actions taken on an object, such as clicking a mouse, pressing a key, or receiving notifications about things such as running out of memory or tasks completing. Whenever these actions occur to the object, the object sends an event to alert the ActiveX container along with the event-specific data. The ActiveX object defines the events available for an object.
LabVIEW converts the data types of some ActiveX properties and methods into LabVIEW data types so LabVIEW can read and interpret the data. The following table lists the ActiveX data types and the corresponding converted LabVIEW data types.
|ActiveX Data Type||LabVIEW Data Type|
Use the following VIs, functions, controls, and indicators to access the objects, properties, methods, and events associated with other ActiveX-enabled applications:
When LabVIEW accesses the objects associated with another ActiveX-enabled application, it is acting as an ActiveX client. You can use LabVIEW as an ActiveX client in the following ways:
LabVIEW accesses an ActiveX object with the automation refnum control or the ActiveX container, both of which are front panel objects. Use the automation refnum control to select an ActiveX object. Use the ActiveX container to select a displayable ActiveX object, such as a button or document and place it on the front panel. Both objects appear as automation refnum controls on the block diagram.
The implementation, or the object you select, is necessary when COM creates the implementation. However, COM can only return interfaces. When LabVIEW creates an implementation, LabVIEW matches the implementation to the correct interface. A single implementation can implement multiple interfaces. In that case, LabVIEW picks the default interface on the object and returns that interface. The Automation Open function returns the default interface and you can use Variant To Data to convert to a different interface. You cannot return the implementation, only the interface.
|Note The block diagram displays the default interface wired to the Automation Open, not the implementation class. If you open the Select Object From Type Library dialog box, LabVIEW selects the implementation class by default.|
To access an ActiveX-enabled application, use the automation refnum control on the block diagram to create a reference to an application. Wire the control to the Automation Open function, which opens the calling application. Use the Property Node to select and access the properties associated with the object. Use the Invoke Node to select and access the methods associated with the object. Close the reference to the object using the Close Reference function. Closing the reference removes the object from memory.
For example, you can build a VI that opens Microsoft Excel so it appears on the user's screen, creates a workbook, creates a spreadsheet, creates a table in LabVIEW, and writes that table to the Excel spreadsheet.
Refer to the Write Table To XL VI in the labview\examples\comm\ExcelExamples.llb for an example of using LabVIEW as an Excel client.
|Note Applications that include ActiveX custom interfaces appear with a icon. Click the icon to select an object for the custom interface.|
To insert an ActiveX object on the front panel, right-click the ActiveX container, select Insert ActiveX Object from the shortcut menu, and select the ActiveX control or document you want to insert. You can set the properties for an ActiveX object using the ActiveX Property Browser or property pages, or you can set the properties programmatically using the Property Node.
Use the Invoke Node to invoke the methods associated with the object.
For example, you can display a Web page on the front panel by using an ActiveX container to access the Microsoft Web Browser control, selecting the Navigate class of methods, selecting the URL method, and specifying the URL.
If you use the ActiveX container, you do not have to wire the automation refnum control on the block diagram to the Automation Open function or close the reference to the object using the Close Reference function. You can wire directly to the Invoke Node or Property Node because the ActiveX container embeds the calling application in LabVIEW. However, if the ActiveX container includes properties or methods that return other automation refnums, you must close these additional references.
Right-click an ActiveX container and select Advanced»Design Mode from the shortcut menu to display the container in design mode while you edit the VI. When you select the design mode option, the ActiveX container owns the references to the controls and is responsible for creating and releasing new controls. There are distinct design and run modes for controls. Each time you run a VI, LabVIEW removes the design mode control and replaces it with a new control in run mode. When the VI stops, LabVIEW removes the run mode control and creates a new design mode control. In a built application there is no design mode control. LabVIEW creates the run mode control when the VI starts and releases the control when the VI stops. In design mode, events are not generated and event procedures do not run. The default mode is run mode, where you interact with the object as a user would.
After you open an ActiveX server or insert an ActiveX control or document, you can set the properties associated with that control or document using the ActiveX Property Browser, property pages, and the Property Node.
Use the ActiveX Property Browser to view and set all the properties associated with an ActiveX control or document in an ActiveX container. To access the ActiveX Property Browser, right-click the control or document in the container on the front panel and select Property Browser from the shortcut menu. You also can select View»ActiveX Property Browser. The ActiveX Property Browser is an easy way to set the properties of an ActiveX object interactively, but you cannot use the browser to set properties programmatically, and you can use the ActiveX Property Browser only with ActiveX objects in a container. The ActiveX Property Browser is not available in run mode or while a VI runs.
Many ActiveX objects include property pages, which organize the properties associated with the object on separate tabs. To access ActiveX property pages, right-click the object in the container on the front panel and select the name of the object from the shortcut menu.
Like the ActiveX Property Browser, ActiveX property pages are an easy way to set the properties of an ActiveX object interactively, but you cannot use them to set properties programmatically, and you can use property pages only with ActiveX objects in a container. Property pages are not available for all ActiveX objects. Property pages also are not available in run mode or when the VI is running.
Use the Property Node to set ActiveX properties programmatically. For example, if you use an ActiveX object to warn a user when a temperature exceeds a limit, use the Property Node to set the Value property of the object to specify the limit.
The following block diagram shows an example that changes the Value property of the CWButton ActiveX control, which is part of the National Instruments Measurement Studio User Interface ActiveX Library, when the temperature reaches 85 degrees Fahrenheit or higher.
In this case, the CWButton control acts as an LED, changes colors, and displays Warning when the temperature reaches the limit, which is the on state of the CWButton control.
|Note In this example, you could use the ActiveX Property Browser or property pages to set the OffText, OffColor, OnText, and OnColor properties for the CWButton control because you do not need to set those properties programmatically.|
The LabVIEW application, VIs, and control properties and methods are available through ActiveX calls from other applications. Other ActiveX-enabled applications, such as Microsoft Excel, can request properties, methods, and individual VIs from LabVIEW, and LabVIEW acts as an ActiveX server.
For example, you can embed a VI graph in an Excel spreadsheet and, from the spreadsheet, enter data in the VI inputs and run the VI. When you run the VI, the data plot to the graph.
Refer to the labview\examples\comm\freqresp.xls for an example of using LabVIEW properties and methods in an Excel spreadsheet.
If you are writing an ActiveX client that accesses properties and methods from an ActiveX server using LabVIEW, you can access custom interfaces exposed by the server. You do not need to use IDispatch to do so. However, the developer of the ActiveX server must make sure the parameters of the properties and methods in these custom interfaces have Automation (IDispatch) data types. The developer of the server must do so to expose multiple interfaces from one object, rather than through multiple objects. You still can use the interfaces in the LabVIEW environment. Refer to your server development programming environment documentation for more information about accessing custom interfaces.
Some parameters in ActiveX nodes take a discrete list of valid values. Select the descriptive name in the ring constant to set these parameter values. To access the ring constant when building an ActiveX VI, right-click the parameter of the node that accepts data values and select Create»Constant from the shortcut menu. The selections available in the ring constant depend on the refnum passed to the node. The following block diagrams use ring and numeric constants to set parameter values.
Parameters that accept data values have a small arrow to the left of the parameter name. To view the corresponding numeric data value, right-click the ring constant and select Visible Items»Digital Display from the shortcut menu.
The two previous block diagrams access the Microsoft Excel application and execute a method. The Index parameter of the ActivateMicrosoftApp method has the following options: MicrosoftWord, MicrosoftPowerPoint, MicrosoftMail, MicrosoftAccess, MicrosoftFoxPro, MicrosoftProject, and MicrosoftSchedulePlus.
To identify the numeric value of the Index parameter that corresponds to the MicrosoftAccess option in the first block diagram, select the MicrosoftAccess option from the pull-down menu in the ring constant. The numeric value of the currently selected option appears in a box next to the ring constant. Instead of using a ring constant, you can enter the numeric value of an option into a numeric constant, as shown in the second block diagram.
To use ActiveX events in an application, you must register for the event and handle the event when it occurs. ActiveX event registration is similar to dynamic event registration. However, the architecture of an ActiveX event VI is different than the architecture of an event-handling VI. The following components make up a typical ActiveX event VI:
You can generate and handle events on ActiveX objects in a container or on ActiveX objects you specify by using an automation refnum. For example, you can call a Windows-based tree control from an ActiveX container and specify that you want to generate a Double Click event for the items displayed in the tree control.
When you wire an ActiveX object reference to the Register Event Callback function and specify the event you want to generate for that object, you are registering the ActiveX object for that event. After you register for the event, create a callback VI that contains the code you write to handle the event. Different events may have different event data formats so changing the event after you create a callback VI might break wires on the block diagram. Select the event before you create the callback VI.
National Instruments recommends that you unregister for events when you no longer need to handle them using the Unregister For Events function. If you do not unregister for events, LabVIEW continues to generate and queue the events as long as the VI runs, even if no Event structure is waiting to handle them, which consumes memory and can hang the VI if you enable front panel locking for the events.
When you load a VI with a change in an ActiveX Type Library's major version, LabVIEW launches a warning dialog box, informing you of the change. When you load a VI with a change in an ActiveX Type Library's minor version or time stamp, the VI, once loaded, includes an asterisk in its title bar and in the list of open VIs displayed in the Window menu. When you save the VI, the asterisk disappears until you make a new change.