Difference between revisions of "Writing your own COM component using VB.NET"

(corrected a formatting issue)
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
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.
+
[[Category: COM integration]]
  
 
''Note: [[COM Integration|Calling a COM component]] requires [[Analytica 4.6|Analytica Enterprise release 4.6]] or higher.''
 
''Note: [[COM Integration|Calling a COM component]] requires [[Analytica 4.6|Analytica Enterprise release 4.6]] or higher.''
 +
 +
__TOC__
 +
 +
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
 
To follow this example, you will also need to have
Line 7: Line 11:
 
* .NET Framework 4.0 or higher (I am using .NET 4.5).
 
* .NET Framework 4.0 or higher (I am using .NET 4.5).
  
= Using the component =
+
== 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.
 
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 := <code>[[COMCreateObject]]("AnaVoice.Speaker")</code>
+
:<code>Variable Speaker := COMCreateObject("AnaVoice.Speaker")</code>
:Button RunIt
+
:<code>Button RunIt</code>
::[[OnClick]]: <code>''LongComputation'' ; Speaker->Say("The simulation has finished")</code>
+
::<code>OnClick: ''LongComputation'' ; Speaker->Say("The simulation has finished")</code>
  
= Creating the project =
+
== Creating the project ==
  
 
Steps:
 
Steps:
Line 21: Line 25:
 
#::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''.
 
#::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 '''New Project...'''
# Select '''Visual Basic''' + '''Windows''' + '''Class Library''', and name your project (I've named this one <code>AnaVoice</code>).
+
# Select '''Visual Basic''' + '''Windows''' + '''Class Library''', and name your project (We've named this one <code>AnaVoice</code>).
 
#:[[image:VB_COM_New_Project.png]]
 
#:[[image:VB_COM_New_Project.png]]
 
# From the menus, select '''Project''' / '''AnaVoice Properties''' / '''Compile'''.
 
# From the menus, select '''Project''' / '''AnaVoice Properties''' / '''Compile'''.
Line 27: Line 31:
 
#:[[image:VB_COM_Register_for_COM_interop.png]]
 
#:[[image:VB_COM_Register_for_COM_interop.png]]
 
# If you'll be using Analytica 64-bit, then from the menus select '''Build''' / '''Configuration Manager...'''.
 
# If you'll be using Analytica 64-bit, then from the menus select '''Build''' / '''Configuration Manager...'''.
#:In the table, change Any CPU to '''<New...>'''. New platform should say '''x64'''. Press [OK].
+
#:In the table, change Any CPU to '''<New...>'''. New platform should say '''x64'''. Press '''OK'''.
 
#:[[image:VB_COM_Configuration_Manager.png]]
 
#:[[image:VB_COM_Configuration_Manager.png]]
# In the '''Solution Explorer''' window, right click on the ''Class1.vb'' filename and change it to ''AnaVoice.vb''.
+
# In the '''Solution Explorer''' window, right click on the <code>Class1.vb</code> filename and change it to <code>AnaVoice.vb</code>.
 
#:[[image:VB_COM_Rename_File.png]]
 
#:[[image:VB_COM_Rename_File.png]]
  
= Defining and registering a class =
+
== 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:
+
With the project set up, you are ready to define the component interface. In the code window for <code>AnaVoice.vb</code>, enter this code:
  
 +
<code style="background:white; border:white; margin-left: 1em;">
 
  <ComClass(Speaker.ClassId, Speaker.InterfaceId, Speaker.EventsId)>
 
  <ComClass(Speaker.ClassId, Speaker.InterfaceId, Speaker.EventsId)>
 
  Public Class Speaker
 
  Public Class Speaker
Line 41: Line 46:
 
     Public Const InterfaceId As String = "<font color="green">D99D7C79-2BA7-4A33-B7BC-9B7F19FDF828</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 Const EventsId As String = "<font color="green">CA128AC4-580C-4112-9EAD-8D1599E3F37A</font>"
  &nbsp;
+
   
 
     Public Sub New()
 
     Public Sub New()
 
         MyBase.New()
 
         MyBase.New()
 
     End Sub
 
     End Sub
  &nbsp;
+
   
 
     Public Sub Say(ByVal phrase As String)
 
     Public Sub Say(ByVal phrase As String)
 
     End Sub
 
     End Sub
 
  End Class
 
  End Class
 +
</code>
 +
 +
::''Note: In C#, you'll need to add
 +
:::<code>using System.Runtime.InteropServices;</code>
 +
::at the top of your file.''
  
 
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.
 
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.
Line 58: Line 68:
  
 
The method that you'll be calling from your Analytica model is  
 
The method that you'll be calling from your Analytica model is  
    Public Sub Say(ByVal phrase As String)
+
:<code>    Public Sub Say(ByVal phrase As String)</code>
    End Sub
+
:<code>    End Sub</code>
  
Notice that the parameter is marked as <code>ByVal</code>. 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 <code>ByRef</code>.
+
Notice that the parameter is marked as <code>ByVal</code>. This is important -- Analytica's COM facility can only call methods that use <code>ByVal</code> parameters only, so do not declare any of your parameters as <code>ByRef</code>.
  
= Implementing the Method =
+
== Implementing the Method ==
  
 
Now we'll implement the <code>Say()</code> 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.
 
Now we'll implement the <code>Say()</code> 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.
Line 69: Line 79:
 
# On the menus, select '''Project''' / '''Add Reference...'''
 
# On the menus, select '''Project''' / '''Add Reference...'''
 
# On the left, select '''Assemblies''' / '''Framework'''
 
# On the left, select '''Assemblies''' / '''Framework'''
# In the main pane, scroll down and select '''System.Speech'''. Press [OK]
+
# In the main pane, scroll down and select '''System.Speech'''. Press '''OK'''
 
#:Note: If it isn't there, you don't have the .NET 4.0 or better Framework installed.
 
#:Note: If it isn't there, you don't have the .NET 4.0 or better Framework installed.
#:[[image:VB_COM_System_Speech.png]]
+
#:[[image:VB_COM_System_Speech.png|900px]]
 
# At the top of the ''AnaVoice.vb'' file, add these lines
 
# At the top of the ''AnaVoice.vb'' file, add these lines
Imports System.Speech
+
#:<code>Imports System.Speech</code>
Imports System.Speech.Synthesis
+
#:<code>Imports System.Speech.Synthesis</code>
 
# Enter the code for the body of the method
 
# Enter the code for the body of the method
 +
<pre style="background:white; border:white; margin-left: 1em;">
 
     Public Sub Speak(ByVal phrase As String)
 
     Public Sub Speak(ByVal phrase As String)
 
         Dim synth As SpeechSynthesizer = New SpeechSynthesizer()
 
         Dim synth As SpeechSynthesizer = New SpeechSynthesizer()
Line 81: Line 92:
 
         synth.Speak(prompt)
 
         synth.Speak(prompt)
 
     End Sub
 
     End Sub
 
+
</pre>
 
Your VB code is now complete. Compile it again ('''Build''' / '''Build Solution''').
 
Your VB code is now complete. Compile it again ('''Build''' / '''Build Solution''').
  
= Test it =
+
== Test it ==
  
 
# Turn on your computer's speakers
 
# Turn on your computer's speakers
Line 97: Line 108:
 
:[[image:VB_COM_Test_Model.png]]
 
:[[image:VB_COM_Test_Model.png]]
  
= See Also =
+
== See Also ==
  
 
* [[COM Integration]]
 
* [[COM Integration]]
 
* [[media:AnaVoice.zip|AnaVoice.zip]]: The Visual Studio project files for this project.
 
* [[media:AnaVoice.zip|AnaVoice.zip]]: The Visual Studio project files for this project.
 
* [[media:AnaVoice_test.ana|AnaVoice_test.ana]]: The test model that speaks a phrase that you enter from Analytica.
 
* [[media:AnaVoice_test.ana|AnaVoice_test.ana]]: The test model that speaks a phrase that you enter from Analytica.

Revision as of 16:37, 4 September 2019


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

Creating the project

Steps:

  1. 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.
  2. Select New Project...
  3. Select Visual Basic + Windows + Class Library, and name your project (We've named this one AnaVoice).
    VB COM New Project.png
  4. From the menus, select Project / AnaVoice Properties / Compile.
    Scroll down and check Register for COM interop
    VB COM Register for COM interop.png
  5. If you'll be using Analytica 64-bit, then from the menus select Build / Configuration Manager....
    In the table, change Any CPU to <New...>. New platform should say x64. Press OK.
    VB COM Configuration Manager.png
  6. In the Solution Explorer window, right click on the Class1.vb filename and change it to AnaVoice.vb.
    VB COM Rename File.png

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
using System.Runtime.InteropServices;
at the top of your file.

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.

  1. On the menus, select Project / Add Reference...
  2. On the left, select Assemblies / Framework
  3. In the main pane, scroll down and select System.Speech. Press OK
    Note: If it isn't there, you don't have the .NET 4.0 or better Framework installed.
    VB COM System Speech.png
  4. At the top of the AnaVoice.vb file, add these lines
    Imports System.Speech
    Imports System.Speech.Synthesis
  5. 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

  1. Turn on your computer's speakers
  2. Start Analytica, edit mode, bring diagram to front.
  3. Create a variable named Speaker and set its definition to
    COMCreateObject("AnaVoice.Speaker")
  4. Evaluate to test that it works. If it works (your component is properly registered) the result will be «COM Object»
  5. 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

VB COM Test Model.png

See Also

Comments


You are not allowed to post comments.