Adding voice commands is fairly simple. An application using these would only have to make the following modifications:
CoCreateInstance (CLSID_VCmd, NULL, CLSCTX_LOCAL_SERVER, IID_IVoiceCmd, &pIVoiceCommand);
CoCreateInstance() instantiates a local copy of the Voice Commands Object.
pIVCmdNotifySink = new CMyIVCmdNotifySink;
This creates a instance of the CMyIVCmdNotifySink class that receives notifications when the user says a command. The functionality of the class is specific to the application.
pIVoiceCommand->Register ("", pIVCmdNotifySink, IID_IVCmdNotifySink, 0, NULL);
Calling Register informs the voice-commands object of the notification sink and also specifies where the audio is to come from.
sData.pData = a pointer to a list of VCMDCOMMAND structures;
sData.dwSize = size of the memory pointed to by sData.pData;
Next, the application must include a list of VCMDCOMMAND structures. Each structure contains a command ("Open a file"), description ("This opens a file from the disk"), command ID for identification, and a few other parameters. The commands are specific to the application.
strcpy (VCmdName.szApplication, "Application Name");
strcpy (VCmdName.szState, "Main");
pIVoiceCommand->MenuCreate (&VCmdName, NULL, VCMDMC_CREATE_TEMP, &pIVCmdMenu);
pIVCmdMenu->Add (number of commands, sData, NULL);
The code above creates an instance of a Voice menu object using the commands specified in sData. The menu generated in this example is temporary, but menus can be saved to a system storage, similar to the registry, so that an application doesn't have to reconstruct the VCMDCOMMAND structures every time.
pIVCmdMenu->Activate (hWnd, 0);
Calling Activate causes the speech-recognition engine to start "listening" for the specified commands whenever the window, hWnd, is active.
Once the application has called Activate, speech recognition will begin listening. When the user says one of the commands in the menu, the notification sink will be called with pIVCmdNotifySink->CommandRecognize (..., dwID, ...). dwID contains the ID command spoken, just like a Windows menu. Other notifications such as the VU-meter also come through, but most applications will only care about dWID.
When an application is no longer required to get recognitions for the menu anymore, it simply calls:
pIVCmdMenu->Deactivate ();
Finally, when the application shuts down, it releases the object:
pIVCmdMenu->Release();
pIVoiceCommand->Release();
Wait until pIVCmdNotifySink is released by engine.
That's a concise overview of how this high-level interface works.