UsingCOMInAutomationDeskApplicationNote
UsingCOMInAutomationDeskApplicationNote
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
Technical Background 9
Component Object Model (COM)..................................................... .............. 9
AutomationDesk........................................................................................... 10
Use of COM Objects in AutomationDesk....................................................... 12
Implementation Structure.............................................................................. 14
Examples 19
Example Use Cases........................................................................................ 19
Implementation............................................................................................. 20
Conclusion 29
Comparison of the Implementations.............................................................. 29
3
May 2024 Using COM in AutomationDesk
Contents
4
Using COM in AutomationDesk May 2024
About This Document
Content This document introduces features that let you access other applications in
AutomationDesk sequences via Microsoft's Component Object Model (COM) .
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 COM 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 COM in AutomationDesk May 2024
Introduction
Introduction
7
May 2024 Using COM in AutomationDesk
Introduction
8
Using COM in AutomationDesk May 2024
Technical Background
Technical Background
Introduction This chapter explains the basic technical background of COM and
AutomationDesk, and introduces recommended implementation structures.
AutomationDesk..................................................................................... 10
Implementation Structure........................................................................ 14
9
May 2024 Using COM in AutomationDesk
Technical Background
Life time of COM objects The communication between the client(s) and the server is realized via the
Window Message Queue. Every COM object has to be connected to the
message queue. The COM objects have to be cleaned up before the concerning
Message Queue shuts down, otherwise it is possible to produce memory leaks
after every usage.
The server counts the objects which are associated with it. Theoretically the COM
server should know the order of cleaning up the COM objects. But in fact, it is
much more secure to manually clean up the COM objects in the inverse order of
the creation.
AutomationDesk
Threads in AutomationDesk The AutomationDesk process contains several threads. The main thread contains
the GUI. Each execution of AutomationDesk elements (Project, Folder, Sequence,
Serial and Blocks) runs in a separated thread. In case of the execution of a
parallel subsystem each subtree is started in an own thread.
Python namespace in AutomationDesk has one global namespace for the use of Python variables
AutomationDesk in Exec blocks. The namespace is not cleaned up until the end of the
AutomationDesk session. After one execution, all assignments made in Exec
blocks will remain in the namespace and can be accessed in the next executions.
Picture 2‑2 shows the global Exec block namespace. In the first execution the
Exec block (1) initializes a Python object, an integer variable. In the second
10
Using COM in AutomationDesk May 2024
AutomationDesk
execution the Exec block (2) uses the same namespace and can access the
integer variable.
AutomationDesk process
X=5
COM objects in In AutomationDesk each thread has its own message loop. In conclusion of
AutomationDesk threads this and the fact that a COM object has to be released before its thread is
terminated, all used COM objects have to be released manually by the user’s
code at the end of each execution (thread).
Picture 2‑3 shows the global Exec block namespace. In the first execution the
Exec block (1) initializes a COM object. In the second execution the Exec block
(2) is expected to use it. The integer object is indeed available. Also the COM
object is available, because of the global namespace. But its area of validity was
terminated with the previous execution (thread) and the usage of the same COM
object will raise an error.
11
May 2024 Using COM in AutomationDesk
Technical Background
Picture 2‑3: Namespace and the life time of COM objects in AutomationDesk
Execution Stop button in The Stop button terminates the execution before starting the next block. In this
AutomationDesk case, no error handling or cleanup block will be executed. It can be therefore
critical to use the Stop button in sequences using COM.
Assignment of COM objects COM objects must not be assigned to the usual AutomationDesk data objects
(_AD_.). Instead simple Python variables should be used:
§ Incorrect (do not use):
_AD_.ExcelAppl = win32com.client.Dispatch("Excel.Application")
§ Correct:
ExcelAppl = win32com.client.Dispatch("Excel.Application")
12
Using COM in AutomationDesk May 2024
Use of COM Objects in AutomationDesk
Tip
Cleanup of COM objects By reason of the global Exec block namespace, it is essential to release all
COM objects when the execution thread is terminated. This can be done by
an assignment to None or using the del statement. For the realization, the try-
finally error handling should be used. The following code structure shows a
secure way for the implementation of COM handling (see Code 2‑1). The Code
can be divided into three parts: initialization usage and cleanup of the COM
objects. The initialization is necessary, because an error can occur before a COM
object is created. In this case the del statement itself would raise an error and
the following code lines of the cleanup part would not be executed.
# Initialize the COM Objects
ComObj_1 = None
ComObj_2 = None
...
try:
# Creation of the first COM object, e.g. Excel - Application
# (see Code 3-2)
ComObj_1 = win32com.client.Dispatch(<...>)
...
# Creation of an additional COM object via the first COM object
# (ComObj_1)
ComObj_2 = ComObj_1.<...>(<...>)
...
finally:
# Releasing the COM objects
del ComObj_2
del ComObj_1
The COM server generally provides a lot of objects, but normally only few are
directly visible by the client. For example, Microsoft Excel provides an application
object. In the further hierarchy the object for a single Excel sheet is dependent
on the workbook object and the workbook object on the application object.
Theoretically the server knows the cleanup order of all objects and shuts down
after releasing the last COM object, independent of the hierarchy level of the
COM object. The server documentation should also explain the rules for the
shutdown.
13
May 2024 Using COM in AutomationDesk
Technical Background
If there are problems with the shutdown, one can try to cleanup the COM
objects in the inverse order to their hierarchy level or their creation order:
# Creation order
ExcelAppl = win32com.client.Dispatch('Excel.Application')
Workbook = ExcelAppl.Workbook.Item(<...>)
Sheet = Workbook.Sheet.Item(<...>)
...
# Clean-up order (inverse to the creation order)
del Sheet
del Workbook
del ExcelAppl
Implementation Structure
Error handling in Python (Exec The single-Exec-Block solution is interesting for tasks which are rarely used (in
blocks) relation to the test duration). Using COM, this is the case if the communication
between the server and the client is rare or if the client is able to bring the server
to a state where the client is able to disconnect from the server but the server
will stay open.
But this implementation is secure, because the error handling realizes a clean up
of the COM objects and the execution Stop button cannot interrupt the block
execution.
Error handling in The error handling can be realized with the TryFinally AutomationDesk block
AutomationDesk (Sequences) (Picture 2‑4) and the TestFramework elements TestSequence and Test (Picture
2‑5).
On the one hand, with this implementations the execution Stop button should
not be used and on the other hand the whole ‘TryFinally’ or rather the whole
‘Test’ can be executed.
Note
14
Using COM in AutomationDesk May 2024
Implementation Structure
Note
The TestInitialization phase of the Test block is used for dispatch call, the
TestStepsAndEvaluation phase is used for the COM usage and clean up of the
COM objects is performed in the TestCleanUp phase (Picture 2‑5).
15
May 2024 Using COM in AutomationDesk
Technical Background
COM Dispatch
COM Usage
COM Release
Both solutions have restrictions on the development and on the usage. It is not
allowed to execute one of the Sequences/TestSequences separately. Only the
folder which contains all Sequences/TestSequences with COM handling (“Com
Dispatch”, “Com Usage” and “Com Release”) may be executed. The Stop
button must not be pressed during the execution, because the “Com Release”-
16
Using COM in AutomationDesk May 2024
Implementation Structure
17
May 2024 Using COM in AutomationDesk
Technical Background
18
Using COM in AutomationDesk May 2024
Examples
Examples
Introduction This chapter shows the implementation structure for COM handling on the
different implementation levels in AutomationDesk. The first example shows a
remote control of Microsoft Outlook®. The implementation is on the Exec block
level. The whole COM handling is implemented in a single Exec block.
Four different implementations are presented for the second example, a remote
control of Microsoft Excel®. The COM handling is distributed over several Exec
blocks and over several TestSequences.
Implementation....................................................................................... 20
Example 1: Sending an email The AutomationDesk block ‘SendEMail’ realizes a remote control of Microsoft
Outlook. The task of the block is to send an email.
Example 2: Transfer The second example shows the remote control of Excel. An Excel sheet contains
Data between Excel and the parameterization of the real-time model and should get the results too.
AutomationDesk
19
May 2024 Using COM in AutomationDesk
Examples
Implementation
Introduction The first example can be implemented in a single Exec block. The only interaction
with the AutomationDesk project consists in the block parameters (data objects)
which can be used to set necessary information for sending an email, for
example, the recipient and the subject. In the second example more interaction
between AutomationDesk and Excel is necessary. Excel will be needed twice
during the project execution; at first at the start of the execution, for reading the
model parameterization, and at the end of the execution to store the result.
Implementation Example 1 Behavior of Microsoft Outlook during Remote Control The behavior
of Outlook using the remote control is as follows: If Outlook is closed when
creating the COM object, it will be opened, and the final COM object cleanup
correspondingly causes the termination of Outlook. Otherwise, the related COM
object connects to the open Outlook and a the clean up only closes the
connection between the COM object and Outlook.
The remote control of Outlook contains two COM objects, the application object
(Python variable name ‘Outlook’, line 7) and the object of the email (Python
variable name ‘EMail’, line 8). The email object depends on the application object
and should be released before the application object is released.
20
Using COM in AutomationDesk May 2024
Implementation
1 import win32com.client
2
3 Outlook = None
4 EMail = None
5
6 try:
7 Outlook = win32com.client.Dispatch("Outlook.Application")
8 EMail = Outlook.CreateItem(0)
9 EMail.To = _AD_.To
10 EMail.Subject = _AD_.Subject
11 EMail.Body = _AD_.Body
12 if _AD_.Attachment != '':
13 EMail.Attachments.Add(_AD_.Attachment, 1, 1)
14 EMail.Send()
15 finally:
16 del EMail
17 del Outlook
Implementation Example 2 Behaviour of Microsoft Excel during Remote Controlling The Excel
application will be used “very often” during the project execution. For this case 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.
Block Interface and Code The following parameters have to be set to the
current location of the example:
21
May 2024 Using COM in AutomationDesk
Examples
22
Using COM in AutomationDesk May 2024
Implementation
23
May 2024 Using COM in AutomationDesk
Examples
4 Sheet = None
5 Workbook = None
6 ExcelAppl = None
7
8 try:
9 # Creation of the COM object
10 # Open an Excel instance
11 ExcelAppl = win32com.client.Dispatch("Excel.Application")
12 # Fetch COM interface of the Excel file
13 Workbook = ExcelAppl.Workbooks.Item(_AD_.FileName)
14 # Fetch COM interface of the Excel sheet
15 Sheet = Workbook.Worksheets.Item(_AD_.SheetName)
16 # write the matrix back to the Excel sheet
17 Sheet.Cells.Range(_AD_.StartCell,_AD_.EndCell).Value =_AD_.Data
18 # save the Excel file
19 Workbook.Save()
20 finally:
21 # Release the COM objects
22 del Sheet
23 del Workbook
24 del ExcelAppl
Note
24
Using COM in AutomationDesk May 2024
Implementation
Block: “OpenExcelApplication”
1 # import COM Module
2 import win32com.client
3 # Creation of the COM Object
4 # Open an Excel-Instance
5 ExcelAppl = win32com.client.Dispatch("Excel.Application")
6 # make the Excel-Instance visible (not necessary)
7 ExcelAppl.Visible = 1
25
May 2024 Using COM in AutomationDesk
Examples
Implementation Version C
Note
The last implementation (Picture 3‑4) can be also implemented with the
TestSequence element and the Test element of the TestFramework library (Picture
3‑5).
The blocks are the same as in the TryFinally solution, but the Stop button
problem is not so critical. After pressing the Stop button the user is asked,
whether AutomationDesk should execute all clean up parts. In the case of COM
handling it is always necessary to run the clean up parts. The blocks are the same
like the previous implementations (Implementation Version B).
26
Using COM in AutomationDesk May 2024
Implementation
COM Dispatch
COM Usage
COM Release
27
May 2024 Using COM in AutomationDesk
Examples
28
Using COM in AutomationDesk May 2024
Conclusion
Conclusion
Implementation-dependent The following table shows the conclusion of COM handling with the different
behavior implementation solutions described in this document.
29
May 2024 Using COM in AutomationDesk
Conclusion
30
Using COM in AutomationDesk May 2024