ISpRecoGrammar::SaveCmd (SAPI 5.3)

Speech API 5.3
Microsoft Speech API 5.3


ISpRecoGrammar::SaveCmd allows applications using dynamic grammars to save the current grammar state to a stream.

   IStream                                   *pSaveStream,
   [annotation ("__deref_opt_out")]LPWSTR   **ppCoMemErrorText


[in] The stream to save the compiler binary grammar into.
[out] Optional parameter of a null-terminated string containing error messages that occurred during the save operation.

Return values

S_OKFunction completed successfully.
E_INVALIDARGpSaveStream is invalid or bad.
SPERR_NOT_DYNAMIC_GRAMMARCommand was loaded but compiler is not available.
SPERR_UNINITIALIZEDCompiler is not available.
E_POINTERppCoMemErrorText is invalid or bad.
FAILED (hr)Appropriate error message.


Applications can use ::SaveCmd to serialize grammar changes that were made at run time for use at a later time. See also ISpRecoGrammar::LoadCmdFromMemory.


The following code snippet illustrates how to use ISpRecoGrammar::SaveCmd to serialize the CFG from one SpRecoGrammar object and deserialize it into another SpRecoGrammar object.

// Declare local identifiers:
HRESULT                      hr = S_OK;
CComPtr<ISpRecoGrammar>      cpRecoGrammar;
CComPtr<ISpRecoGrammar>      cpReloadedGrammar;
CComPtr<ISpRecoContext>      cpRecoContext;
CComPtr<IStream>             cpHStream;
HGLOBAL                      hGrammar;

// Build and use a SpRecoGrammar object...

// Create a Win32 global stream.
hr = ::CreateStreamOnHGlobal(NULL, true, &cpHStream;);

if (SUCCEEDED(hr))
   // Save the current grammar to the global stream.
   hr = cpRecoGrammar->SaveCmd(cpHStream, NULL);

if (SUCCEEDED(hr))
   // Create the second grammar to deserialize into.
   hr = cpRecoContext->CreateGrammar(0, &cpReloadedGrammar;);

if (SUCCEEDED(hr))
   // Get a handle to the stream with the serialized grammar.
   ::GetHGlobalFromStream(cpHStream, &hGrammar;);

// Deserialize the CFG into a new grammar object.
hr = cpReloadedGrammar->LoadCmdFromMemory((SPBINARYGRAMMAR *)::GlobalLock(hGrammar), SPLO_DYNAMIC);

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