SAPI 5.1 Speech Recognition

I'm using VB 2005, and I'm having problems with my code.

Here's the exception I get:
System.Runtime.InteropServices.COMException (0x80040111): Retrieving the COM class factory for component with CLSID {47206204-5ECA-11D2-960F-00C04F8EE628} failed due to the following error: 80040111.
at Voice_Control_VB.frmMain.frmMain_Load(Object sender, EventArgs e) in C:Documents and SettingsScottMy DocumentsVisual Studio 2005ProjectsVoice Control VBVoice Control VBfrmMain.vb:line 23

Line 23 is: RecoContext = New SpSharedRecoContext
RecoContext is defined globally: Private WithEvents RecoContext As SpSharedRecoContext

My code works fine in VB6 (minor differences), I can get text to speech to work in VB 2005, but I can't even initialize the recognition engine without running into a problem.

Any help would be greatly appreciated.
Thanks.

Comments

  • [b][red]This message was edited by DrMarten at 2006-6-29 14:8:43[/red][/b][hr]

    Put the line>>
    [code]
    Imports System.Runtime.InteropServices
    [/code]

    at the very top of your code window and see if that helps.

    Your line>>
    Private WithEvents RecoContext As SpSharedRecoContext

    defined globally in a new program gives SpSharedRecoContext
    as undefined.

    Additionaly the line>>

    Private RecoContext1 As New SpSharedRecoContext

    would be more correct, though SpSharedRecoContext
    is still undefined.

    Is SpSharedRecoContext a CLASS you need to add to your
    project that has a "NEW" sub within it?

    If so go to PROJECT>AddClass to add your CLASS code.

    See>>
    http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=39&MsgID=339662&Setting=A9999F0001

    for links that show pictures of how to do this, regardless of the originator's question, the CLASS code and method of how to use NEW
    in a project is demonstrated in code on the page.

    Otherwise post your full code in this forum and someone
    may be able to help.


    Regards,

    Dr M.

  • I didn't have the interopservices, but adding it didn't change anything.

    SpSharedRecoContext is part of SpeechLib, and to import SpeechLib you need to add a reference to SAPI.dll (Microsoft Speech Object Library).

    Here's the code. Most of it can be ignored for the purposes of this, as it's in the Form_Load sub that I encounter my problem.

    [code]
    Option Explicit On

    Imports SpeechLib
    Imports System
    Imports System.IO
    Imports System.Media
    Imports System.Reflection
    Imports System.Resources
    Imports System.Runtime.InteropServices
    Imports System.Windows.Forms.Application

    Public Class frmMain
    Private WithEvents RecoContext As SpSharedRecoContext
    Private Grammar As ISpeechRecoGrammar
    Private Voice As SpeechLib.SpVoice

    Private Const ID_SayComputer As Integer = 1
    Private Const ID_Program As Integer = 0

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    NotifyIcon.Icon = Me.Icon
    NotifyIcon.Visible = False

    Try
    RecoContext = New SpSharedRecoContext

    Grammar = RecoContext.CreateGrammar(0)
    Grammar.CmdLoadFromFile("vc.xml", SpeechLoadOption.SLOStatic)
    Grammar.DictationSetState(SpeechRuleState.SGDSInactive)
    Grammar.CmdSetRuleIdState(ID_SayComputer, SpeechRuleState.SGDSActive) '0 for all top level rules, ID for others

    Call SayText("Voice control software is loaded and active")
    Catch ex As Exception
    MessageBox.Show(ex.ToString(), "Error - initSAPI")
    TextBox1.Text = ex.ToString
    End Try

    End Sub

    Private Sub SayTime()
    Call SayText(Now.ToString)
    End Sub

    Private Sub SayText(ByVal strText As String)
    Voice = New SpVoice
    Voice.Speak(strText, SpeechVoiceSpeakFlags.SVSFlagsAsync)
    End Sub

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
    Me.Close()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    Me.Close()
    End Sub

    Private Sub ResetTimer()
    'reset timer
    tmrSayComputer.Enabled = False 'disable timer
    tmrSayComputer.Enabled = True 'enable timer
    End Sub

    Private Sub tmrComputerSay_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrSayComputer.Tick
    tmrSayComputer.Enabled = False 'disable timer
    Grammar.CmdSetRuleIdState(0, SpeechRuleState.SGDSInactive) 'deactivate all commands
    Grammar.CmdSetRuleIdState(ID_SayComputer, SpeechRuleState.SGDSActive) 'activate SayComputer command
    End Sub

    Private Sub LaunchProgram(ByVal strProgram As String)
    Dim strLocation As String = ""

    Select Case strProgram
    Case "internet explorer"
    strLocation = "C:Program Files (x86)Internet Exploreriexplore.exe"
    Case "outlook express"
    strLocation = "C:Program Files (x86)Outlook Expressmsimn.exe"
    Case "firefox"
    strLocation = "C:Program Files (x86)Mozilla Firefoxfirefox.exe"
    End Select

    If strLocation <> "" Then
    Call Shell(strLocation, AppWinStyle.NormalFocus)
    End If
    End Sub

    Private Sub RecoContext_Recognition(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal RecognitionType As SpeechLib.SpeechRecognitionType, ByVal Result As SpeechLib.ISpeechRecoResult) Handles RecoContext.Recognition
    txtCommand.Text = Result.PhraseInfo.GetText

    If Result.PhraseInfo.Rule.Name = "SayComputer" Then
    Grammar.CmdSetRuleIdState(0, SpeechRuleState.SGDSActive) 'activate all commands
    tmrSayComputer.Enabled = True 'allow commands to be given for 6 seconds
    Call PlayCompSound()
    ElseIf Result.PhraseInfo.Rule.Name = "LaunchProgram" Then
    Call LaunchProgram(Result.PhraseInfo.Properties.Item(ID_Program).Value)
    Call ResetTimer()
    ElseIf Result.PhraseInfo.Rule.Name = "WhatTime" Then
    Call SayTime()
    Call ResetTimer()
    End If
    End Sub

    Private Sub PlayCompSound()
    With New SoundPlayer(My.Resources.computer)
    .PlaySync()
    End With
    End Sub

    Private Sub MaximizeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MaximizeToolStripMenuItem.Click
    Me.Show()
    NotifyIcon.Visible = False
    End Sub

    Private Sub cmdTray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTray.Click
    Me.Hide()
    NotifyIcon.Visible = True
    End Sub

    End Class
    [/code]

  • Hi,

    Please CUT and PASTE the entire code from your code window so i have the design too. :-)

    This is a way to copy an entire program without you having to ZIP
    up your PROJECT's folder.


    Regards,

    Dr M.

  • [code]
    Declare Auto Function MBox Lib "user32.dll" _
    Alias "MessageBox" (ByVal hWnd As Integer, _
    ByVal txt As String, ByVal caption As String, _
    ByVal Typ As Integer) As Integer

    Const MB_ICONQUESTION = &H20L
    Const MB_YESNO = &H4
    Const IDYES = 6
    Const IDNO = 7

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'SetWindowRgn(100, 100, 100)

    Dim RetVal As Integer ' Stores the return value.
    RetVal = MBox(0, "Declare DLL Test", "Windows API MessageBox", _
    MB_ICONQUESTION Or MB_YESNO)
    ' Check the return value.
    If RetVal = IDYES Then
    MsgBox("You chose Yes")
    Else
    MsgBox("You chose No")
    End If
    End Sub
    [/code]
  • That was the entire code cut and pasted from the code window...if you want the code from frmMain.Designer.vb, it's:

    [code]
    _
    Partial Class frmMain
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing AndAlso components IsNot Nothing Then
    components.Dispose()
    End If
    MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    _
    Private Sub InitializeComponent()
    Me.components = New System.ComponentModel.Container
    Me.txtCommand = New System.Windows.Forms.TextBox
    Me.NotifyIcon = New System.Windows.Forms.NotifyIcon(Me.components)
    Me.TrayIconMenu = New System.Windows.Forms.ContextMenuStrip(Me.components)
    Me.MaximizeToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
    Me.ExitToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
    Me.cmdTray = New System.Windows.Forms.Button
    Me.cmdExit = New System.Windows.Forms.Button
    Me.tmrSayComputer = New System.Windows.Forms.Timer(Me.components)
    Me.TextBox1 = New System.Windows.Forms.TextBox
    Me.TrayIconMenu.SuspendLayout()
    Me.SuspendLayout()
    '
    'txtCommand
    '
    Me.txtCommand.Location = New System.Drawing.Point(12, 12)
    Me.txtCommand.Name = "txtCommand"
    Me.txtCommand.ReadOnly = True
    Me.txtCommand.Size = New System.Drawing.Size(268, 20)
    Me.txtCommand.TabIndex = 0
    '
    'NotifyIcon
    '
    Me.NotifyIcon.ContextMenuStrip = Me.TrayIconMenu
    Me.NotifyIcon.Text = "NotifyIcon"
    Me.NotifyIcon.Visible = True
    '
    'TrayIconMenu
    '
    Me.TrayIconMenu.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.MaximizeToolStripMenuItem, Me.ExitToolStripMenuItem})
    Me.TrayIconMenu.Name = "ContextMenuStrip1"
    Me.TrayIconMenu.ShowImageMargin = False
    Me.TrayIconMenu.Size = New System.Drawing.Size(104, 48)
    '
    'MaximizeToolStripMenuItem
    '
    Me.MaximizeToolStripMenuItem.Name = "MaximizeToolStripMenuItem"
    Me.MaximizeToolStripMenuItem.Size = New System.Drawing.Size(103, 22)
    Me.MaximizeToolStripMenuItem.Text = "&Maximize"
    '
    'ExitToolStripMenuItem
    '
    Me.ExitToolStripMenuItem.Name = "ExitToolStripMenuItem"
    Me.ExitToolStripMenuItem.Size = New System.Drawing.Size(103, 22)
    Me.ExitToolStripMenuItem.Text = "E&xit"
    '
    'cmdTray
    '
    Me.cmdTray.Location = New System.Drawing.Point(12, 38)
    Me.cmdTray.Name = "cmdTray"
    Me.cmdTray.Size = New System.Drawing.Size(95, 23)
    Me.cmdTray.TabIndex = 1
    Me.cmdTray.Text = "Send to Tray"
    Me.cmdTray.UseVisualStyleBackColor = True
    '
    'cmdExit
    '
    Me.cmdExit.Location = New System.Drawing.Point(185, 38)
    Me.cmdExit.Name = "cmdExit"
    Me.cmdExit.Size = New System.Drawing.Size(95, 23)
    Me.cmdExit.TabIndex = 2
    Me.cmdExit.Text = "Exit"
    Me.cmdExit.UseVisualStyleBackColor = True
    '
    'tmrSayComputer
    '
    Me.tmrSayComputer.Interval = 10000
    '
    'TextBox1
    '
    Me.TextBox1.Location = New System.Drawing.Point(12, 67)
    Me.TextBox1.Multiline = True
    Me.TextBox1.Name = "TextBox1"
    Me.TextBox1.Size = New System.Drawing.Size(268, 162)
    Me.TextBox1.TabIndex = 3
    '
    'frmMain
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.ClientSize = New System.Drawing.Size(292, 316)
    Me.Controls.Add(Me.TextBox1)
    Me.Controls.Add(Me.cmdExit)
    Me.Controls.Add(Me.cmdTray)
    Me.Controls.Add(Me.txtCommand)
    Me.MaximizeBox = False
    Me.MinimizeBox = False
    Me.Name = "frmMain"
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = "Voice Control"
    Me.TrayIconMenu.ResumeLayout(False)
    Me.ResumeLayout(False)
    Me.PerformLayout()

    End Sub
    Friend WithEvents txtCommand As System.Windows.Forms.TextBox
    Friend WithEvents NotifyIcon As System.Windows.Forms.NotifyIcon
    Friend WithEvents TrayIconMenu As System.Windows.Forms.ContextMenuStrip
    Friend WithEvents MaximizeToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents ExitToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents cmdTray As System.Windows.Forms.Button
    Friend WithEvents cmdExit As System.Windows.Forms.Button
    Friend WithEvents tmrSayComputer As System.Windows.Forms.Timer
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

    End Class
    [/code]
  • And this is for....?

    : [code]
    : Declare Auto Function MBox Lib "user32.dll" _
    : Alias "MessageBox" (ByVal hWnd As Integer, _
    : ByVal txt As String, ByVal caption As String, _
    : ByVal Typ As Integer) As Integer
    :
    : Const MB_ICONQUESTION = &H20L
    : Const MB_YESNO = &H4
    : Const IDYES = 6
    : Const IDNO = 7
    :
    : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    : 'SetWindowRgn(100, 100, 100)
    :
    : Dim RetVal As Integer ' Stores the return value.
    : RetVal = MBox(0, "Declare DLL Test", "Windows API MessageBox", _
    : MB_ICONQUESTION Or MB_YESNO)
    : ' Check the return value.
    : If RetVal = IDYES Then
    : MsgBox("You chose Yes")
    : Else
    : MsgBox("You chose No")
    : End If
    : End Sub
    : [/code]
    :

  • [code]
    Imports System.Runtime.InteropServices
    Public Class Form1

    Inherits System.Windows.Forms.Form

    #Region " Windows Form Designer generated code "

    Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
    If Not (components Is Nothing) Then
    components.Dispose()
    End If
    End If
    MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Private Sub InitializeComponent()
    Me.Button1 = New System.Windows.Forms.Button
    Me.SuspendLayout()
    '
    'Button1
    '
    Me.Button1.Location = New System.Drawing.Point(96, 120)
    Me.Button1.Name = "Button1"
    Me.Button1.TabIndex = 0
    Me.Button1.Text = "Button1"
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(292, 266)
    Me.Controls.Add(Me.Button1)
    Me.Name = "Form1"
    Me.Text = "Form1"
    Me.ResumeLayout(False)

    End Sub

    #End Region
    Declare Auto Function MBox Lib "user32.dll" _
    Alias "MessageBox" (ByVal hWnd As Integer, _
    ByVal txt As String, ByVal caption As String, _
    ByVal Typ As Integer) As Integer

    Const MB_ICONQUESTION = &H20L
    Const MB_YESNO = &H4
    Const IDYES = 6
    Const IDNO = 7

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'SetWindowRgn(100, 100, 100)

    Dim RetVal As Integer ' Stores the return value.
    RetVal = MBox(0, "Declare DLL Test", "Windows API MessageBox", _
    MB_ICONQUESTION Or MB_YESNO)
    ' Check the return value.
    If RetVal = IDYES Then
    MsgBox("You chose Yes")
    Else
    MsgBox("You chose No")
    End If
    End Sub


    End Class
    [/code]

  • You'll have to tell me where to do that...the form's design/layout code is in a completely separate file than my program's code.

    Otherwise, http://www.canadiancow.com/voice.zip has everything you need.
  • Hi,

    Use http://www.megaupload.com/

    or send as a ZIP file either way to me at>>

    noooojohn007@aol.com please.

    I'm getting too many squiggly underlines in pasting your code. :-(


    Regards,

    Dr M.

  • http://www.canadiancow.com/voice.zip

    : Hi,
    :
    : Use http://www.megaupload.com/
    :
    : or send as a ZIP file either way to me at>>
    :
    : noooojohn007@aol.com please.
    :
    : I'm getting too many squiggly underlines in pasting your code. :-(
    :
    :
    : Regards,
    :
    : Dr M.
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories