UsingVirtualCOMInAutomationDeskApplicationNote
UsingVirtualCOMInAutomationDeskApplicationNote
Using VirtualCOM in
AutomationDesk
Release 2024-A – May 2024
How to Contact dSPACE
Mail: dSPACE GmbH
Rathenaustraße 26
33102 Paderborn
Germany
Tel.: +49 5251 1638-0
E-mail: [email protected]
Web: https://www.dspace.com
If possible, always provide the serial number of the hardware, the relevant dSPACE License
ID, or the serial number of the CmContainer in your support request.
Important Notice
This publication contains proprietary information that is protected by copyright. All rights
are reserved. The publication may be printed for personal or internal use provided all the
proprietary markings are retained on all printed copies. In all other cases, the publication
must not be copied, photocopied, reproduced, translated, or reduced to any electronic
medium or machine-readable form, in whole or in part, without the prior written consent
of dSPACE GmbH.
This publication and the contents hereof are subject to change without notice.
Contents
Introduction 7
Intention......................................................................................................... 7
Scope and Area of Validity............................................................................... 8
Technical Background 9
AutomationDesk............................................................................................. 9
Use of COM Objects in AutomationDesk....................................................... 11
Event Handling in AutomationDesk............................................................... 14
Examples 17
Example 1: Sending an Email......................................................................... 17
Example 2: Transfer Data between Excel and AutomationDesk...................... 19
Example 3: Using Excel Events in AutomationDesk......................................... 22
Conclusion 27
Comparison of the Assignment Methods.......................................... ............ 27
3
May 2024 Using VirtualCOM in AutomationDesk
Contents
4
Using VirtualCOM in AutomationDesk May 2024
About This Document
Content This document introduces features that let you access other applications in
AutomationDesk sequences via AutomationDesk's VirtualCOM.
Symbol Description
Indicates a hazardous situation that, if not avoided,
V DANGER
will result in death or serious injury.
Indicates a hazardous situation that, if not avoided,
V WARNING could result in death or serious injury.
Indicates a hazardous situation that, if not avoided,
V CAUTION could result in minor or moderate injury.
Indicates a hazard that, if not avoided, could result in
NOTICE
property damage.
Indicates important information that you should take
Note
into account to avoid malfunctions.
Indicates tips that can make your work easier.
Tip
Indicates a link that refers to a definition in the
glossary, which you can find at the end of the
document unless stated otherwise.
5
May 2024 Using VirtualCOM in AutomationDesk
About This Document
Symbol Description
Follows the document title in a link that refers to
another document.
Naming conventions dSPACE user documentation uses the following naming conventions:
Special Windows folders Windows‑based software products use the following special folders:
Accessing dSPACE Help and After you install and decrypt Windows‑based dSPACE software, the
PDF files documentation for the installed products is available in dSPACE Help and as PDF
files.
dSPACE Help (local) You can open your local installation of dSPACE Help:
§ On its home page via Windows Start Menu
§ On specific content using context-sensitive help via F1
PDF files You can access PDF files via the icon in dSPACE Help. The PDF
opens on the first page.
6
Using VirtualCOM in AutomationDesk May 2024
Introduction
Introduction
Intention................................................................................................... 7
Intention
Introduction This document is intended for AutomationDesk users who want to use features
of other applications in their AutomationDesk sequences. AutomationDesk's
open architecture lets users reimplement such features or remote-control other
applications.
The Distributed Component Object Model (DCOM) is based on the same COM
technology, except that communication takes place between different network
nodes. There are no differences between COM and DCOM with regard to the
remote-control interfaces. Other remote-control interfaces like ActiveX or OLE
are not discussed in this document.
7
May 2024 Using VirtualCOM in AutomationDesk
Introduction
phase. This document provides instructions and examples for using COM via
VirtualCOM.
Tip
Introduction The solutions presented in this document can be used from AutomationDesk 3.2
and upward.
8
Using VirtualCOM in AutomationDesk May 2024
Technical Background
Technical Background
Introduction This chapter explains the basic technical background of COM and VirtualCOM in
AutomationDesk, and introduces recommended implementation structures.
AutomationDesk....................................................................................... 9
AutomationDesk
Threads in AutomationDesk The AutomationDesk process contains several threads. The main thread contains
the user interface. Each execution of AutomationDesk elements (Project, Folder,
Sequence, Serial and Blocks) runs in a separate thread. If subsystems are
executed in parallel, each runs in its own thread.
Since AutomationDesk 3.2, a further thread, called the container thread, was
implemented to manage COM objects created by the VirtualCOM feature.
Python namespace in AutomationDesk has one global namespace for using Python variables in Exec
AutomationDesk blocks. The namespace is not cleaned up until the end of the AutomationDesk
session. After execution, all assignments made in Exec blocks remain in the
namespace and can be accessed in subsequent executions.
The figure below shows the global Exec block namespace. In the first execution,
Exec block (1) initializes a Python object, an integer variable. In the second
execution, Exec block (2) uses the same namespace and can access the integer
variable.
9
May 2024 Using VirtualCOM in AutomationDesk
Technical Background
AutomationDesk process
X=5
COM objects in In AutomationDesk each thread has its own message loop. An initialized COM
AutomationDesk threads object has to be released before its thread is terminated. With the normal COM
object handling, all COM objects have to be released manually by the user’s code
at the end of each execution thread. If VirtualCOM is used to initialize COM
objects, they are created in the container thread and are released either manually
by the user's code or automatically when AutomationDesk is closed. An object's
life time is independent of its execution time.
The figure below shows the global Exec block namespace. In the first execution,
Exec block (1) initializes a COM object using the VirtualCOM dispatch method. In
the second execution, Exec block (2) is expected to use it. Both the integer object
and the COM object are available in the global namespace. The COM object can
be used until it is released manually or the container thread is terminated.
10
Using VirtualCOM in AutomationDesk May 2024
Use of COM Objects in AutomationDesk
AutomationDesk process
X=5
COMObj reference
Introduction VirtualCOM makes it easy to use COM objects in AutomationDesk. Once a COM
object is initialized, it can be used until AutomationDesk is closed. A COM object
is not limited to the execution time of a single Exec block, but can be used
across blocks, sequences and even folders. The following sections describe how
to handle COM objects with VirtualCOM.
Possible methods to assign When using VirtualCOM, you can assign a COM object to a Variant data object
and clean up COM objects in your project structure or to a Python variable.
Note
Using project-specific Variant If you add a Variant data object to your project, you can access it via the _AD_
data objects alias.
11
May 2024 Using VirtualCOM in AutomationDesk
Technical Background
identified by the Variant's name. If COM objects are to be used by several blocks
or sequences, it is recommended to use project-specific Variant data objects.
Example: You have added a Variant data object named ExcelAppl to your
project. To initialize the COM object of the Microsoft Excel application, you can
use the following dispatch call.
_AD_.ExcelAppl = virtualcomobject.VirtualCOMObject.\
VC_Dispatch("Excel.Application")
Manual cleanup of COM objects using Variant data objects Although the
COM objects initialized via VirtualCOM do not have to be cleaned up manually,
it may be useful in some cases. They must be released in reverse order of
creation.
The Variant data object is initialized automatically with None during its creation.
After assignment it contains the COM object as its value. To clean up the COM
object, just set the Variant data object to None again.
_AD_.ExcelAppl = None
Note
Do not use the del command when using Variant data objects for COM
objects.
Using Python variables If you assign a Python variable, it is added to the global namespace and can be
accessed by its name.
12
Using VirtualCOM in AutomationDesk May 2024
Use of COM Objects in AutomationDesk
Using COM objects initialized COM objects are not limited by the implementation structure. They can be used
by VirtualCOM in a single Exec block, across several Exec blocks within a sequence, across
several sequences and even across different folders, as shown in the illustrations
below.
13
May 2024 Using VirtualCOM in AutomationDesk
Technical Background
Introduction With AutomationDesk 3.4, the VirtualCOM feature also provides COM-based
event handling.
Defining an application event Before AutomationDesk can react to events of the connected COM application,
class you have to define a class in AutomationDesk containing the relevant methods.
14
Using VirtualCOM in AutomationDesk May 2024
Event Handling in AutomationDesk
Refer to the COM API documentation of your COM application to get the names
of the available methods and parameters.
Example
class AppEvents:
# You can use any name for the class definition
def Method1(self, Parameter1):
print("*Event* APP -> Method1:", Parameter1)
def Method2(self, Parameter1, Parameter2):
print("*Event* APP -> Method2:", Parameter1, Parameter2)
Note
There might be methods that are only available in the main thread of the
COM object. Then, AutomationDesk cannot access such an event.
Connecting to the events After the application event class is defined and the application object is created,
you have to connect the application events to the application object by using the
VC_DispatchWithEvents method.
Example
_AD_.AppEvents = virtualcomobject.VirtualCOMObject.\
VC_DispatchWithEvents(_AD_.MyAppl, AppEvents)
Using the events While the application object and the application event class is connected,
AutomationDesk can react to the specified actions in the COM application.
Disconnecting from the Before you clear the application object, you should disconnect and clear the
events application event class. You do this by setting the application event class to
None.
Example
_AD_.AppEvents = None
Example project For an example project, refer to Example 3: Using Excel Events in
AutomationDesk on page 22.
15
May 2024 Using VirtualCOM in AutomationDesk
Technical Background
16
Using VirtualCOM in AutomationDesk May 2024
Examples
Examples
Introduction This chapter shows the implementation structure for COM handling using
the two types of COM object assignment. The first example shows a remote
control for Microsoft Outlook®. The implementation is based on Python variable
assignment, and all the COM handling is implemented in a single Exec block.
Data object assignment is described in the second example, a remote control for
Microsoft Excel®. The COM handling is distributed over several Exec blocks and
sequences.
Introduction This example is implemented in a single Exec block, and the COM objects are
assigned to Python variables.
It shows how you can implement a remote control for Microsoft Outlook® to
send an e-mail.
17
May 2024 Using VirtualCOM in AutomationDesk
Examples
Implementation of Example 1 Block interface and Python code The Python code contains the following
variables that are to be provided by project-specific data objects:
The Python code of the Exec block to remote-control Outlook contains two
COM objects, the application object (Python variable Outlook) and the object of
the email (Python variable EMail). The email object depends on the application
object and needs to be released before the application object is released.
import virtualcomobject
Outlook = None
EMail = None
try:
Outlook = virtualcomobject.VirtualCOMObject.\
VC_Dispatch("Outlook.Application")
if Outlook is not None:
EMail = Outlook.CreateItem(0)
if Email is not None:
EMail.To = _AD_.To
EMail.Subject = _AD_.Subject
EMail.Body = _AD_.Body
if _AD_.Attachment is not '':
EMail.Attachments.Add(_AD_.Attachment,1,1)
EMail.Send()
finally:
del EMail
del Outlook
18
Using VirtualCOM in AutomationDesk May 2024
Example 2: Transfer Data between Excel and AutomationDesk
Introduction This example is implemented in two sequences. The COM objects are assigned to
project-specific Variant data objects.
The aim is to remote control the data transfer between Microsoft Excel® and
AutomationDesk. In the first sequence, data is read from an Excel file, and in
the second sequence, data is written to the Excel file. Using VirtualCOM, the
initialized COM objects for reading data in the first sequence can also be used
for writing data in the second sequence. Because different Exec blocks have to
access the COM objects, they are made visible as project-specific Variant data
objects in the project structure.
Implementation of Example 2 Behavior of Microsoft Excel during remote control The Excel application
is used multiple times during project execution. Hence, it is better to open Excel
at the beginning of the execution and not to close it until the end of the
execution. The read and write access is realized in several separate Exec blocks
using the two sequences ReadDataFromExcel and WriteDataToExcel.
Block Interfaces
19
May 2024 Using VirtualCOM in AutomationDesk
Examples
_AD_.ExcelAppl = virtualcomobject.VirtualCOMObject.\
VC_Dispatch("Excel.Application")
_AD_.ExcelAppl.Visible = 1
20
Using VirtualCOM in AutomationDesk May 2024
Example 2: Transfer Data between Excel and AutomationDesk
are in the global namespace of AutomationDesk and can be used directly in this
Exec block without creating additional data objects.
Tip
If you execute the block, you get the data from the Excel sheet and store it in the
Data data object.
# Open the Excel file
_AD_.ExcelAppl.Workbooks.Open(_AD_.ExcelFile.GetPathName())
# Fetch the sheet from the Excel file. Return value is a tuple.
Data = _AD_.Sheet.Cells.\
Range(_AD_.StartCell,_AD_.EndCell).Value
21
May 2024 Using VirtualCOM in AutomationDesk
Examples
Introduction This example shows you how to access events from a remote-controlled
Microsoft Excel® application in AutomationDesk.
Implementation of Example 3 Project-specific data objects The following Variant data objects are used to
provide the created COM objects.
22
Using VirtualCOM in AutomationDesk May 2024
Example 3: Using Excel Events in AutomationDesk
23
May 2024 Using VirtualCOM in AutomationDesk
Examples
Using the application event class During the For loop, you can create a
new workbook and resize the Excel window. The creation of the workbook
triggers the specified event and you can see the message defined in the
application event class in the Output Viewer. The resize of the window also
triggers an event but it cannot be accessed by AutomationDesk. The defined
message is not displayed. While you want to work with the Excel user interface,
the application must be visible.
To change the visibility of Excel, you can use the Visible property:
_AD_.Excel.Visible = True # user interface is displayed
_AD_.Excel.Visible = False # user interface is hidden
24
Using VirtualCOM in AutomationDesk May 2024
Example 3: Using Excel Events in AutomationDesk
If you execute the blocks, the application event class is disconnected from the
application object and cleared, Excel is closed, and the Excel application object is
cleared.
25
May 2024 Using VirtualCOM in AutomationDesk
Examples
26
Using VirtualCOM in AutomationDesk May 2024
Conclusion
Conclusion
Introduction The descriptions and examples in this document demonstrate the functionality
of AutomationDesk's VirtualCOM. Its main advantage over the standard COM
handling is easier use of COM objects. There are no strict rules you have to
observe to prevent run-time errors, for example, when using the Stop Execution
command. VirtualCOM in AutomationDesk is a convenient, secure way to handle
COM objects for remote control.
Assignment methods The following table shows the recommended COM handling for the two
methods of COM object assignment.
27
May 2024 Using VirtualCOM in AutomationDesk
Conclusion
28
Using VirtualCOM in AutomationDesk May 2024