Writing your own COM component using VB.NET
Note: Calling a COM component requires Analytica Enterprise release 4.6 or higher.
This article takes you through the steps of creating and writing your own COM component in VB.NET that can be called from Analytica. You can use your component to compute something and return the result, to interface with other applications, or to interact with the user.
To follow this example, you will also need to have
- Microsoft Visual Studio with VB (I am using Visual Studio 2013)
- .NET Framework 4.0 or higher (I am using .NET 4.5).
Using the component
In this example, we will create a component that accepts text and speaks it using a voice synthesizer. You would use this from your Analytica Model as follows.
Variable Speaker := COMCreateObject("AnaVoice.Speaker")
- Button RunIt
- OnClick:
LongComputation ; Speaker->Say("The simulation has finished")
- OnClick:
Creating the project
Steps:
- Start Visual Studio.
- To enable Visual Studio to register your component for you, you should launch it as Administrator. Right-click on the Visual Studio icon and select Run as administrator.
- Select New Project...
- Select Visual Basic + Windows + Class Library, and name your project (I've named this one
AnaVoice
). - From the menus, select Project / AnaVoice Properties / Compile.
- If you'll be using Analytica 64-bit, then from the menus select Build / Configuration Manager....
- In the Solution Explorer window, right click on the Class1.vb filename and change it to AnaVoice.vb.
Defining and registering a class
With the project set up, you are ready to define the component interface. In the code window for AnaVoice.vb, enter this code:
<ComClass(Speaker.ClassId, Speaker.InterfaceId, Speaker.EventsId)> Public Class Speaker Public Const ClassId As String = "<font color="green">3A42F85E-24C8-4BAA-91B5-AE56C4683C13</font>" Public Const InterfaceId As String = "<font color="green">D99D7C79-2BA7-4A33-B7BC-9B7F19FDF828</font>" Public Const EventsId As String = "<font color="green">CA128AC4-580C-4112-9EAD-8D1599E3F37A</font>" Public Sub New() MyBase.New() End Sub Public Sub Say(ByVal phrase As String) End Sub End Class
When creating your own component, be sure to replace the GUIDs that are in green above with unique GUIDs. To generate unique GUIDs, select Create GUID from the Tools menu, and use the Registry Format option. After copy and pasting, remove the curly braces so it looks like the code above.
You now have a complete project and API. Compile it by selecting Build / Rebuild Solution. This will build the project and register the component.
To validate that the component was actually registered, you can run RegEdit.exe and navigate to
HKEY_CLASSES_ROOT\AnaVoice.Speaker
The method that you'll be calling from your Analytica model is
Public Sub Say(ByVal phrase As String) End Sub
Notice that the parameter is marked as ByVal
. This is important -- Analytica's COM facility can only call methods that use ByVal parameters only, so do not declare any of your parameters as ByRef
.
Implementing the Method
Now we'll implement the Say()
method to actually pronounce the phrase. Of course, in your own custom classes, this in where you would be implementing the logic that you are interested in.
- On the menus, select Project / Add Reference...
- On the left, select Assemblies / Framework
- In the main pane, scroll down and select System.Speech. Press [OK]
- At the top of the AnaVoice.vb file, add these lines
Imports System.Speech Imports System.Speech.Synthesis
- Enter the code for the body of the method
Public Sub Speak(ByVal phrase As String) Dim synth As SpeechSynthesizer = New SpeechSynthesizer() Dim prompt As Prompt = New Prompt(phrase) synth.Speak(prompt) End Sub
Your VB code is now complete. Compile it again (Build / Build Solution).
Test it
- Turn on your computer's speakers
- Start Analytica, edit mode, bring diagram to front.
- Create a variable named
Speaker
and set its definition toCOMCreateObject("AnaVoice.Speaker")
- Evaluate to test that it works. If it works (your component is properly registered) the result will be «COM Object»
- In another variable Definition or Button OnClick, call your method using
Speaker->Say("Some text to say")
You can use my test model, "AnaVoice test.ana" shown here
See Also
- COM Integration
- AnaVoice.zip: The Visual Studio project files for this project.
- AnaVoice_test.ana: The test model that speaks a phrase that you enter from Analytica.
Enable comment auto-refresher