<?xml version="1.0" encoding="UTF-8"?>
#define SPCAT_VOICES L "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\Voices"
#define SPKEY_ATTRIBUTES L "Attributes"
#define SPVALUE_LANGUAGE L "Language"
BOOL CActionCenter :: InitializeSAPI ()
{
ReleaseSAPI ();
HRESULT hr ;
hr = m_cpVoice . CoCreateInstance ( CLSID_SpVoice );
if ( hr == S_OK )
{
ISpObjectToken * pToken ; // NOTE: Not a CComPtr! Be Careful.
CComPtr < IEnumSpObjectTokens > cpEnum ;
hr = SpEnumTokens ( SPCAT_VOICES , L "" , L "" , & cpEnum );
if ( hr == S_OK )
{
bool fSetDefault = false ;
while ( cpEnum -> Next (1, & pToken , NULL ) == S_OK )
{
//CSpDynamicString dstrDesc;
//hr = SpGetDescription(pToken, &dstrDesc);
//if (SUCCEEDED(hr))
{
m_vecISpToken . push_back ( pToken );
}
}
return ( m_vecISpToken . size () > 0);
}
}
return FALSE ;
}
void CActionCenter :: ReleaseSAPI ()
{
vector < ISpObjectToken *>:: iterator it ;
for ( it = m_vecISpToken . begin (); it != m_vecISpToken . end (); ++ it )
{
(* it )-> Release ();
}
m_vecISpToken . clear ();
}
void CActionCenter :: SetVoice ( LANGID lid )
{
vector < ISpObjectToken *>:: iterator it ;
WCHAR wstrLangID [32] = {0};
swprintf ( wstrLangID , L "%X" , lid );
for ( it = m_vecISpToken . begin (); it != m_vecISpToken . end (); ++ it )
{
ISpObjectToken * pToken = (* it );
ISpDataKey * pSpDataKeyAttributes = NULL ;
HRESULT hr = pToken -> OpenKey ( SPKEY_ATTRIBUTES , & pSpDataKeyAttributes );
if ( SUCCEEDED ( hr ))
{
WCHAR * wstrLanguageID = NULL ;
hr = pSpDataKeyAttributes -> GetStringValue ( SPVALUE_LANGUAGE , ( WCHAR **)& wstrLanguageID );
if ( SUCCEEDED ( hr ))
{
if ( wcsstr (( BSTR ) wstrLanguageID , ( BSTR ) wstrLangID ))
{
m_cpVoice -> SetVoice ( pToken );
break ;
}
}
pSpDataKeyAttributes -> Release ();
}
}
}