Writing your own COM component using VB.NET
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")
OnClick: LongComputation ; Speaker->Say("The simulation has finished")
Creating the project
- 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 (We've named this one
- 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.vbfilename and change it to
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 = "3A42F85E-24C8-4BAA-91B5-AE56C4683C13" Public Const InterfaceId As String = "D99D7C79-2BA7-4A33-B7BC-9B7F19FDF828" Public Const EventsId As String = "CA128AC4-580C-4112-9EAD-8D1599E3F37A" Public Sub New() MyBase.New() End Sub Public Sub Say(ByVal phrase As String) End Sub End Class
- Note: In C#, you'll need to add
- at the top of your file.
- Note: In C#, you'll need to add
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
The method that you'll be calling from your Analytica model is
Public Sub Say(ByVal phrase As String)
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
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
- 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).
- Turn on your computer's speakers
- Start Analytica, edit mode, bring diagram to front.
- Create a variable named
Speakerand set its definition to
- 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