Contents
- Index
COM API - Initialization
Using Visual Basic
Using Visual C++
Excel
Note: If the Automation Tools option (/10) is not licensed you will get an "Accessed Denied" message when you try to create an instance of the class.
Visual Basic (Microsoft Visual Studio 2022)
1. Add a reference to the automation interface to your project
2. Create an instance of the class
3. Call the methods
Add the reference to your project:
1. Choose Project | Add Reference…
2. On the Add Reference dialog, click the "Browse..." button
3. Select C:\Program Files (x86)\SpectraPLUS_DT\Bin\SPAxnCOMwrapDT.dll
4. Click OK
Dimension and Instantiate an instance of the SpectraPLUS DT Automation Interface class object
Dim SP As SPAxnCOMwrapSC.IAnalyzerDT
SP = New SPAxnCOMwrapSC.IAnalyzerDT
SP.Init()
Call the methods in the interface as desired
SP.Run()
Return Values
The methods return HRESULTs, so Visual Basic will convert the return value to an exception if an error occurred. As such, you must handle exceptions in your code.
On Error GoTo Err_Trap
... call methods, etc.
Err_Trap:
Dim sMsg As String
sMsg = "The error returned: "
sMsg = sMsg & Err.Description
MsgBox sMsg, vbCritical, _
"Error" & Str$(Err.Number)
Err.Clear
Resume Next
Accessing Arrays
Below is a simple example of how to use the SAFEARRAYS with the GetSpectrum() call. Both freq and data are single precision floating point arrays.
Dim freq() As Single
Dim data() As Single
SP.GetSpectrum(0, freq, data)
Dim n As Variant
For Each n In freq
Value = n
Next n
For Each q In data
Value = q
Next q
Visual C++ (Visual Studio 2022)
1. Import the Type Library (TLB)
2. Create a smart pointer of that type
3. Call the methods
In the header file:
#import "C:\Program Files\SpectraPLUS_DT\bin\SPAxnCOMwrapDT.tlb" no_namespace
In the cpp file:
CoInitialize(NULL);
IAnalyzerDTPtr m_SP;
m_SP.CreateInstance(__uuidof(AnalyzerDT));
if ( ISP )
{
m_SP->Init();
ISP->Run();
}
… execute remainder of program …
ISP = 0;
The return values of the functions are HRESULTS. The Visual C++ implementation of smart pointers will intercept those return values and throw exceptions on errors. Thus, all code must be wrapped with try/catch blocks. The type of exception thrown is an _com_error.
try
{
ISP->Run();
}
catch ( _com_error &error )
{
MessageBox( error.ErrorMessage() );
}
Microsoft Excel
1. Enable the Developer tab in Excel
2. Add a reference to the automation interface
3. Create an instance of the class
4. Call the methods
Enable the Developer tab (if needed)
1. Click on the File tab
2. Click the Options command
3. Click the Customize Ribbon category
4. In the Main Tabs list on the right, put a check in the "Developer" box
Add a reference to the automation interface
1. Click on the Developer tab
2. Click the View Code button
3. Choose Tools | References
4. Scroll down and check the box next to the ".NET wrapper (32 &64 bit) for SpectraPLUS-DTautomationLib"
5. Click OK
Dimension and Instantiate an instance of the SpectraPLUS-DT Automation Interface class object
Dim SP As SPAxnCOMwrapDT.AnalyzerDT
Set SP = CreateObject("SPAxnCOMwrapDT.AnalyzerDT")
SP.Init
Call the methods
SP.Run
Below is a simple example of how to read an array of spectral data and write it to the spreadsheet
Dim channel As Integer
Dim startHz As Single
Dim stopHz As Single
' define the frequency span and channel
channel = 0
startHz = 500#
stopHz = 2000#
' Arrays to contain the spectral data
Dim freq() As Single
Dim data() As Single
' Read the data from the analyzer
SP.GetSpectrumInSpan channel, startHz, stopHz, freq, data
Dim n As Variant
Dim q As Variant
Dim col As Integer
Dim row As Integer
' Load the data into the cells
col = 1
row = 1
For Each n In freq
Sheet1.Cells(row, col).value = n
row = row + 1
Next n
col = 2
row = 1
For Each q In data
Sheet1.Cells(row, col).value = q
row = row + 1
Next q