Partager via


ISpRecoContext::CreateGrammar (SAPI 5.4)

Microsoft Speech API 5.4

ISpRecoContext::CreateGrammar

ISpRecoContext::CreateGrammar creates an SpRecoGrammar object.

  
    HRESULT CreateGrammar(
   ULONGLONG          ullGrammarId,
ISpRecoGrammar   **ppGrammar
);

Parameters

  • ullGrammarId
    [in] Specifies the grammar identifier. The identifier is used by the application and is not required. This identifier is associated with all result objects from the grammar (see SPPHRASE.ullGrammarID).
  • ppGrammar
    [out] Address of a pointer which receives the ISpRecoGrammar object. The application must call IUnknown::Release on the object when finished using it.

Return values

Value
S_OK
E_POINTER
E_OUTOFMEMORY
SPERR_SR_ENGINE_EXCEPTION
FAILED(hr)

Example

The following code snippet illustrates the use of ISpRecoContext::CreateGrammar.

  
// Declare local identifiers:
HRESULT	                   hr = S_OK;
CComPtr<ISpRecoContext>    cpRecoContext;
CComPtr<ISpRecoResult>     cpRecoResult;
CComPtr<ISpRecoGrammar>    cpRecoGrammar;
ULONGLONG                  ullGramId = 1;
SPPHRASE                   *pPhrase;
const WCHAR                *MY_CFG_FILENAME = L"Foo.cfg";    // Dummy file name.

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

if (SUCCEEDED(hr))
{
   // Load a cfg from a file (constant points to dummy file name).
   hr = cpRecoGrammar->LoadCmdFromFile(MY_CFG_FILENAME, SPLO_STATIC);
}

if (SUCCEEDED(hr))
{
   // Activate the top-level rules.
   hr = cpRecoGrammar->SetRuleState(NULL, NULL, SPRS_ACTIVE);
}

if (SUCCEEDED(hr))
{
   // Get a recognition.
   // ...
}

// Get the recognized phrase from the recognition result object.
hr = cpRecoResult->GetPhrase(&pPhrase;);

if (SUCCEEDED(hr))
{
   // Check the grammar id of the recognition result.
   _ASSERT(GRAM_ID == pPhrase->ullGrammarID);

   // Release system resources.
   ::CoTaskMemFree(&pPhrase;);

}