ISpObjectToken::CreateInstance (SAPI 5.4)

Speech API 5.4
Microsoft Speech API 5.4


ISpObjectToken::CreateInstance creates an instance of an object.

HRESULT CreateInstance(
   IUnknown   *pUnkOuter,
   DWORD       dwClsContext,
   REFIID      riid,
   void      **ppvObject


[in] If the object is being created as part of an aggregate, this is a pointer to the controlling IUnknown interface of the aggregate. Otherwise, pUnkOuter must be NULL.
[in] Context in which the code that manages the newly created object will run. It should be one of the following values:
[in] Reference to the identifier of the interface used to communicate with the newly created object. If pUnkOuter is NULL, this parameter is frequently the IID of the initializing interface; if pUnkOuter is non-NULL, riid must be IID_IUnknown.
[out, iid_is(riid)] Address of pointer variable that receives the interface pointer requested in riid. Upon successful return, ppvObject contains the requested interface pointer. If the object does not support the interface specified in riid, the implementation must set ppvObject to NULL.

Return values

S_OKFunction completed successfully.
E_POINTERppvObject is invalid or bad.
E_INVALIDARGpUnkOuter is invalid or bad.
SPERR_UNINITIALIZEDEither the data key or the token delegator interface is not initialized.
SPERR_TOKEN_DELETEDKey has been deleted.
FAILED(hr)Appropriate error message.


This method is used to create the underlying object that the object token represents. This method looks at the CLSID value stored in the object token and creates a COM object from this CLSID.

For example, when this method is called on an object token from the audio input category, an audio object that implements ISpStreamFormat will be created and returned.

This method is not used to create speech recognition or text-to-speech engines. Instead, an SpRecognizer or SpVoice object is created and the engine is then created by passing an object token to the ISpRecognizer::SetRecognizer or ISpVoice::SetVoice methods.


The following code snippet creates an InProc server instance.

// Declare local identifiers:
HRESULT                        hr = S_OK;
CComPtr<ISpObjectWithToken>    cpObjectWithToken;
CComPtr<ISpObjectToken>        cpObjectToken;

hr = SpGetDefaultTokenFromCategoryId(SPCAT_VOICES, &cpObjectToken;);

if (SUCCEEDED (hr))
   hr = cpObjectToken->CreateInstance(NULL, CLSCTX_INPROC_SERVER, IID_ISpObjectWithToken, (void **)&cpObjectWithToken;);

if (SUCCEEDED(hr))
   // Do stuff here.