ISpRecoGrammar::SetRuleState (SAPI 5.3)

Speech API 5.3
Microsoft Speech API 5.3


ISpRecoGrammar::SetRuleState activates or deactivates a rule by its rule name.

HRESULT SetRuleState(
   LPCWSTR       pszName,
   void          *pReserved,
   SPRULESTATE    NewState


[in, string] Address of a null-terminated string containing the rule name. If NULL and the grammar is in SAPI 5 format, all rules with attribute SPRAF_TopLevel and SPRAF_Active and set (at rule creation time) are affected.
Reserved. Do not use; must be NULL.
[in] Flag of type SPRULESTATE indicating the new rule state. See Remarks section.

Return values

S_OKFunction completed successfully.
E_INVALIDARGpszName is invalid or bad. Alternatively, pReserved is non-NULL.
SP_STREAM_UNINITIALIZEDISpRecognizer::SetInput has not been called with the InProc recognizer
SPERR_UNINITIALIZEDThe object has not been properly initialized.
SPERR_UNSUPPORTED_FORMATAudio format is bad or is not recognized. Alternatively, the device driver may be busy by another application and cannot be accessed.
SPERR_NOT_TOPLEVEL_RULEThe rule pszName exists, but is not a top-level rule.
FAILED(hr)Appropriate error message.


The rule name is specified in the XML grammar (using the rule NAME tag), or when ISpGrammarBuilder::GetRule is called.

See also ISpSREngine::RuleNotify for information on the how SAPI notifies the SR engine.

An application can use the SPRS_ACTIVE_WITH_AUTO_PAUSE state to pause the engine after each CFG recognition is sent. The application must reactivate the SR engine (see ISpRecoContext::Resume) to prevent the loss of input audio data (see ISpSREngineSite::Read and SPERR_AUDIO_BUFFER_OVERFLOW).

By default, the recognizer state (SPRECOSTATE) is SPRST_ACTIVE, and the recognition will begin as soon as one or more rule are activated. Consequently, an application should not activate the rule state until it is prepared to receive recognitions. An application can also disable the SpRecoContext object (see ISpRecoContext::SetContextState) or SpRecoGrammar objects (see ISpRecoGrammar::SetGrammarState) to prevent recognitions from being fired for active rules. 

If the recognizer state is SPRST_ACTIVE, SAPI will first attempt to open the audio input stream when dictation (or a rule) is activated. Consequently, if the audio device is already in use by another application, or the stream fails to open, the failure code will be returned using ::SetRuleState. The application should handle this failure gracefully.

If an application uses an InProc recognizer, it must call ISpRecognizer::SetInput with a non-NULL setting before the recognizer will return recognitions, regardless of how many rules are active.


The following snippet loads a grammar, activate a single rule ("playcard") and then immediately deactivates it.

// Declare local identifiers:
HRESULT                      hr = S_OK;
CComPtr<ISpRecoContext>      cpRecoContext;
CComPtr<ISpRecoGrammar>      cpRecoGrammar;
ULONGLONG                    ullGramId = 1;

// Create a grammar object.
hr = cpRecoContext->CreateGrammar(ullGramId, &cpRecoGrammar;);

if (SUCCEEDED(hr))
   // Activate the rule.
   hr = cpRecoGrammar->SetRuleState(L"playcard", NULL, SPRS_ACTIVE);

if (SUCCEEDED(hr))
   // Deactivate the rule.
   hr = cpRecoGrammar->SetRuleState(L"playcard", NULL, SPRS_INACTIVE);

if (SUCCEEDED(hr))
   // Do stuff here.
© 2015 Microsoft