Initializing an Application for Voice Text

To use voice text, you need to create a voice-text object and register your application with the object. You create a voice-text object by calling the CoCreateInstance function with the CLSID_VTxt class identifier and the IID_IVoiceText interface identifier. You must create a separate voice-text object for each site that your application needs to use.

CoCreateInstance returns a pointer to the IVoiceText interface for the voice-text object. Before an application can perform other voice-text tasks, it must register itself by calling the IVoiceText::Register member function. Register specifies the site that the object uses for audio output, and passes the address of the application's voice-text notification interface to the voice-text object.

The following example shows how to create a voice-text object and register the application with the object.

// BeginOLE - initializes OLE, creates a voice-text object, and

// registers the application with the object.

// Returns TRUE if successful or FALSE otherwise.

//

// Global variables:

// gpIVTxt - address of IVoiceText interface

// gpIVTxtAttr - address of IVTxtAttributes interface

// gpVTxtNotifySink - address of IVTxtNotifySink interface

BOOL BeginOLE()

{

HRESULT hRes;

// Initialize OLE.

if (FAILED(CoInitialize(NULL)))

return ReleaseInterfaces("CoInitialize() failed.");

// Create a voice-text object.

if (CoCreateInstance(CLSID_VTxt, NULL, CLSCTX_LOCAL_SERVER,

IID_IVoiceText, (LPVOID *) &gpIVTxt) != S_OK)

return ReleaseInterfaces(

"Error in CoCreateInstance for voice-text interface." );

// Get the address of the voice-text attributes interface.

hRes = gpIVTxt->QueryInterface(IID_IVTxtAttributes,

(LPVOID FAR *) &gpIVTxtAttr);

if (FAILED(hRes))

return ReleaseInterfaces(

"Failed to get voice-text attributes interface.");

// Create and register the voice-text notification sink.

gpVTxtNotifySink = new CIVTxtNotifySink;

if (gpVTxtNotifySink == NULL)

return ReleaseInterfaces(

"Out of memory for voice-text notification object.");

hRes = gpIVTxt->Register(NULL, "SRClock", gpVTxtNotifySink,

IID_IVTxtNotifySink, VTXTF_ALLMESSAGES, NULL);

if (FAILED(hRes))

return ReleaseInterfaces(

"Failed to register voice-text notification sink.");

return TRUE;

}

// ReleaseInterface - displays an error message and releases the

// voice-text object.

// Returns FALSE.

// szMsg - text of error message

//

// Global variable:

// gpIVTxt - address of voice-text interface

BOOL ReleaseInterfaces(const char *szMsg)

{

// If a message is specified, display it.

if (*szMsg)

MessageBox(NULL, szMsg, NULL, MB_OK);

if(gpIVTxt)

gpIVTxt->Release();

gpIVTxt = NULL;

return FALSE;

}