Once you have a pointer to the IAgent interface, you can use the Load method to load a character and retrieve its IDispatch interface:
// Create a variant to store the full path of the character to load
VariantInit(&vPath);
vPath.vt = VT_BSTR;
vPath.bstrVal = SysAllocString(kpwszCharacter);
// Load the character
hRes = pAgent->Load(vPath, &lCharID, &lRequestID);
// Get its IDispatch interface
hRes = pAgent->GetCharacter(lCharID, &pdCharacter);
You can then use this information to request a pointer to the IAgentCharacter:
// Query for IAgentCharacter
hRes = pdCharacter->QueryInterface(IID_IAgentCharacter, (LPVOID *)&pCharacter);
// Release the IDispatch
pdCharacter->Release();
You can use this interface to access the character's methods:
// Show the character. The first parameter tells Microsoft
// Agent to show the character by playing an animation.
hRes = pCharacter->Show(FALSE, &lRequestID);
// Make the character speak
bszSpeak = SysAllocString(L"Hello World!");
hRes = pCharacter->Speak(bszSpeak, NULL, &lRequestID);
SysFreeString(bszSpeak);
When you no longer need Microsoft Agent services, such as when your client application shuts down, release its interfaces. Note that releasing the character interface does not unload the character. Call the Unload method to do this before releasing the IAgent interface:
// Clean up
if (pCharacter) {
// Release the character interface
pCharacter->Release();
// Unload the character. NOTE: releasing the character
// interface does NOT make the character go away. You must
// call Unload.
pAgent->Unload(lCharID);
}
// Release the Agent
pAgent->Release();
VariantClear(&vPath);