Share via


ISpeechRecoGrammar CmdLoadFromResource Method (SAPI 5.3)

Microsoft Speech API 5.3

Interface: ISpeechRecoGrammar

CmdLoadFromResource Method

The CmdLoadFromResource method loads a command and control grammar from a Win32 resource.

  
    ISpeechRecoGrammar.CmdLoadFromResource(
     hModule As Long,
     ResourceName As Variant,
     ResourceType As Variant,
     LanguageId As Long,
     [LoadOption As SpeechLoadOption = SLOStatic]
)

Parameters

  • hModule
    Specifies the hModule.
  • ResourceName
    Specifies the ResourceName.
  • ResourceType
    Specifies the ResourceType.
  • LanguageId
    Specifies the LanguageId.
  • LoadOption
    [Optional] Specifies whether the grammar is to be loaded for static or dynamic use. The default is static.

Return Value

None.

Remarks

The grammar resource must be a compiled SAPI 5 binary version of a context-free grammar (CFG).

Using this method will unload the currently loaded CFG or proprietary grammar.

Example

The following Visual Basic form code demonstrates the use of the CmdLoadFromResource method. Before attempting to run this code, you must:

  • Create a SpeechDocs.dll file as detailed in the Sample DLL Code Example, and
  • Make sure your microphone is connected and functional.

The Form_Load procedure creates a grammar object and loads it with the compiled Solitaire grammar contained in the DLL. There are no controls on this form; simply speak into your microphone. Phrases like "Move the red ten" or "Play the queen of hearts" will be recognized.

  
Option Explicit

Dim WithEvents MyRecoContext As SpSharedRecoContext
Dim MyGrammar As ISpeechRecoGrammar
Dim RsrcHandle As Long

' Change this constant to match the path
' of the SpeechDocs DLL on your machine!
Const Lib As String = "C:\SpeechDocs.dll"
Const RsrcType As String = "CFGGRAMMAR"
Const RsrcName  As Long = 101
Const LangID As Long = &H409;

Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" _
    (ByVal lpLibFileName As String) As Long

Private Sub Form_Load()
    On Error GoTo EH

    Set MyRecoContext = New SpSharedRecoContext
    Set MyGrammar = MyRecoContext.CreateGrammar

    ' Obtain a handle to the executable holding the grammar as a resource
    RsrcHandle = LoadLibrary(Lib)

    ' Load the grammar from the resource
    MyGrammar.CmdLoadFromResource RsrcHandle, RsrcName, RsrcType, LangID, SLOStatic

    ' Set all DefaultToActive rules active
    ' NOTE: The solitaire grammar happens to have DefaultToActive rules
    ' which is why we can activate them this way.
    MyGrammar.CmdSetRuleState "", SGDSActive

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub MyRecoContext_FalseRecognition _
   (ByVal StreamNumber As Long, _
    ByVal StreamPosition As Variant, _
    ByVal result As SpeechLib.ISpeechRecoResult)

    MsgBox "(not recognized!)"

End Sub

Private Sub MyRecoContext_Recognition _
   (ByVal StreamNumber As Long, _
    ByVal StreamPosition As Variant, _
    ByVal RecognitionType As SpeechLib.SpeechRecognitionType, _
    ByVal result As SpeechLib.ISpeechRecoResult)

    On Error GoTo EH
    MsgBox result.PhraseInfo.GetText

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub ShowErrMsg()

    ' Declare identifiers:
    Const NL = vbNewLine
    Dim T As String

    T = "Desc: " & Err.Description & NL
    T = T & "Err #: " & Err.Number
    MsgBox T, vbExclamation, "Run-Time Error"
    End

End Sub