Company Events Academic Community Support Solutions Products & Services Contact NI MyNI

Using ActiveX with LabVIEW

LabVIEW 2013 Help

Edition Date: June 2013

Part Number: 371361K-01

»View Product Info

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 Objects, Properties, Methods, and Events

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.

Data Type Mapping

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
char ,
short ,
long ,
hyper ,
float ,
double
BSTR ,

ActiveX VIs, Functions, Controls, and Indicators

Use the following VIs, functions, controls, and indicators to access the objects, properties, methods, and events associated with other ActiveX-enabled applications:

  • Use the automation refnum control to create a reference to an ActiveX object. Right-click this control on the front panel to select an object from the type library you want to access.
    Note  If you have an automation refnum from a VI which does not run in the user interface thread, and you try to release that automation refnum in a VI which does run in the user interface thread, it will hang. When you release an automation refnum which does not run in the user interface thread, always release the automation refnum from a VI that also does not run in the user interface thread.
  • Use the Automation Open function to open an ActiveX object.
  • Use the ActiveX container to access and display an ActiveX object on the front panel. Right-click the container, select Insert ActiveX Object from the shortcut menu, and select the object you want to access.
  • Use the Property Node to get (read) and set (write) the properties associated with an ActiveX object.
  • Use the Invoke Node to invoke the methods associated with an ActiveX object.
  • Use the Register Event Callback function to handle events that occur on an ActiveX object.
  • Use the variant control and indicator to pass data to or from ActiveX controls.

LabVIEW as an ActiveX Client

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:

  • Open an application, such as Microsoft Excel, create a document, and add data to that document.
  • Embed a document, such as a Microsoft Word document or an Excel spreadsheet, on the front panel in a container.
  • Place a button or other object from another application, such as a Help button that calls the other application help file, on the front panel.
  • Link to an ActiveX control you built with another application.

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.

Accessing an ActiveX-Enabled Application

The following steps describe the process you use to access an ActiveX-enabled application in LabVIEW.

  1. Add an automation refnum control to the front panel of a VI.
  2. Select an ActiveX class for the automation refnum control. This class specifies what kind of ActiveX object you want to access.
  3. On the block diagram, use the Automation Open function to open the reference to the ActiveX object. When you open the reference, you enable LabVIEW to communicate with the referenced ActiveX object.
  4. Pass the reference to a Property Node or an Invoke Node to access properties or invoke methods on the object. When you click one of these nodes, LabVIEW displays all of the properties or methods available for the object. The available properties and methods are determined by the Component Object Model (COM) interface that the object implements.
    Note  If LabVIEW does not display the properties or methods that you expect for a particular ActiveX object, the object might implement multiple COM interfaces. LabVIEW can display the properties or methods from only one COM interface implemented by an object at a time. You can force LabVIEW to display the properties and methods from a different COM interface that the object implements by using the Variant To Data function to cast the object to the other COM interface.
  5. Use the Close Reference function to remove the object from memory.

For example, Microsoft Excel is an ActiveX-enabled application. Therefore, you can use the previous steps to open a reference to Excel and call properties and methods that open Excel so it appears on the user's screen, create a workbook, create a spreadsheet, and write a table from LabVIEW to the Excel spreadsheet.

Refer to the Excel - Write Table VI in the labview\examples\Connectivity\Excel directory 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.

Inserting an ActiveX Object on the Front Panel

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

Design Mode for ActiveX Objects

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.

Setting ActiveX Properties

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 Control Property Browser, property pages, and the Property Node.

ActiveX Control Property Browser

Use the ActiveX Control Property Browser to view and set all the properties associated with an ActiveX control or document in an ActiveX container. To access the ActiveX Control 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 Control Property Browser. The ActiveX Control 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 Control Property Browser only with ActiveX objects in a container. The ActiveX Control Property Browser is not available in run mode or while a VI runs.

ActiveX Property Pages

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

Property Nodes

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

LabVIEW as an ActiveX Server

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.

Support for Custom ActiveX Automation Interfaces

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.

Using Constants to Set Parameters in ActiveX VIs

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.

ActiveX Events

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:

  • ActiveX object for which you want to generate an event.
  • Register Event Callback function to specify and register for the type of event you want to generate.
  • Callback VI that contains the code you write to handle the event you specify.

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.

The Register Event Callback function is a growable node capable of handling multiple events, similar to the Register For Events function.

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.

Loading VIs with an Updated ActiveX Type Library

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.


 

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
Add Comments 1 2 3 4 5 submit