Passing Structs to Code Modules - C
- Updated2024-11-26
- 3 minute(s) read
Passing Structs to Code Modules - C
Purpose
This example demonstrates how to pass a TestStand container as a C-style struct to a function in a DLL. This example uses functions to pass the struct by both reference and value. For a DLL function to change the value of a TestStand container, you must pass the reference of the container to the function.
Example File
Location
<TestStand Public>\Examples\Fundamentals\Passing Structs to Code Modules\C\Passing Structs to Code Modules.seq
Highlighted Features
- Calling DLLs from TestStand
- C/C++ DLL Adapter
- Local variables
- Data types
Major API
None
Prerequisites
C compiler or DLL
How to Use This Example
The sequence specifies sPacked8Out and sPacked8In container local variables, which you can review on the Variables pane. The sequence uses a C DLL to copy the values from the sPacked8Out container to the sPacked8In container first by reference and then by value.
Complete the following steps to use this example.
- Select Execute»Test UUTs to run the sequence.
- Enter any serial number in the UUT Information dialog box and click OK. TestStand launches a Get Results dialog box that lists values loaded from the sPacked8In container, which specified default values before you executed the sequence.
- Click OK in the Get Results dialog box. TestStand launches another Get Results dialog box that lists values loaded from the sPacked8In container. The GetStructByRef2 step specifies a pre-expression that specifies a value of 0 for each variable, and then the GetStructByRef2 step specifies new values for each variable.
- Click OK when TestStand informs you that the sequence passed.
- Click Stop in the UUT Information dialog box to end the execution.
You can review the two Message Popup steps in the sequence to verify that the sPacked8In container is displaying the variable values. The functions the DLL is using are available in StructTest.cpp, located in the <TestStand Public>\Examples\Fundamentals\Passing Structs to Code Modules\C directory.
To pass a TestStand container as a struct to a DLL, you must define the container as a custom data type, and you must specify that objects of the data type can be passed as structs.
Complete the following steps to use the Types window to review the data type definition for a container.
- In the Sequence File window, right-click the sPacked8In container on the Variables pane and select View»Type Definition from the context menu to launch the Types window.
- Right-click the AllDataTypesPacked8 data type and select Properties from the context menu to launch the Type Properties dialog box. Because the sequence is using a DLL, you must enable C struct passing for the data type.
- Click the C Struct Passing tab. Notice that the Allow Objects of This Type to be Passed as Structs option is enabled. The Packing ring control specifies the 8 Byte Boundary option, which is the packing scheme the DLL uses.
- Click OK to close the Type Properties dialog box.
- In the Types window, expand the AllDataTypesPacked8 data type. You can right-click each property and select Properties from the context menu to launch the Type Properties dialog box for each property of the data type. You can review each property to see how it is configured.