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 2008)

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c
2. On the Add Reference dialog, choose the COM tab
3. Select the SpectraPLUS-SC Automation Typelib
4. Click OK

Dimension and Instantiate an instance of the SpectraPLUS-SC SC Automation Interface class object

Dim SP As SpectraPLUS_SCAutomationLib.ISpectraPLUS_SC

SP = New SpectraPLUS_SCAutomationLib.SpectraPLUS_SCClass

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 2008)

1.  Import the Type Library (TLB)
2.  Create a smart pointer of that type
3.  Call the methods

#import "C:\Program Files\SpectraPLUS_SC\bin\ SPAxnIface.tlb" no_namespace

CoInitialize(NULL);

ISpectraPLUS_SCPtr ISP(__uuidof(SpectraPLUS_SCClass));

if ( ISP )
{
  ISP->Run();
}

c execute remainder of program c

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

Excel 2007
1. Click on the Office button
2. Click on Excel Options
3.
Click Popular
4. Check the "Show Developer tab in the Ribbon" box

Excel 2010
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 on View Code
3.
Choose Tools | References
4. Scroll down and check the box next to the "SpectraPLUS-SC Automation Typelib"
5. Click OK

Dimension and Instantiate an instance of the SpectraPLUS-SC Automation Interface class object

Dim SP As SpectraPLUS_SCClass

Set SP = CreateObject("SpectraPLUS_SC.Axn")

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