为什么仅当进程作为服务运行时,以下代码在 Speak() 之后失败,错误代码为 0x80045063 (SPERR_NOT_ACTIVE_SESSION)?
ISpVoicePtr pVoice;
CoCreateInstance(
CLSID_SpVoice,
0,
CLSCTX_INPROC_SERVER,
IID_ISpVoice, ( LPVOID * ) &pVoice
);
pVoice->SetOutput( 0, TRUE );
pVoice->Speak( L"Hello", 0, NULL );
我能想到的唯一资源是关于与桌面 session 交互的服务的安全更改。 .NET 中的以下内容将在服务中工作,所以我很好奇如何使用带或不带 SAPI 的纯 C++ 来完成它。
System::Speech::Synthesis::SpeechSynthesizer synth;
synth.SetOutputToDefaultAudioDevice();
synth.Speak( "Hello" );
最佳答案
SAPI 文档将 SPERR_NOT_ACTIVE_SESSION
列为
Neither audio output nor input is supported for non-active console sessions.
服务在后台 session 中运行。由于 Session 0 Isolation在 Vista 中引入,服务无法访问交互式资源,其中包括文本转语音。
谷歌搜索,我发现了一些支持该说法的评论:
http://www.ip-symcon.de/en/service/documentation/module-reference/text-to-speech/tts-speak/
This function does not work under Windows __ Vista/2003__ or newer. This is because services such as IP-Symcon, for security reasons, can not access interactive components in the system. The Text To Speech output is such an interactive component. The problem manifests itself with the error message: __ OLE error 80045063__ extract MSDN: SPERR_NOT_ACTIVE_SESSION.
Note that they broke ( deliberately I think ) this stuff beginning on Vista (and later). If you try to use SAPI's ISpMMSysAudio interface in a service (as they sketch in their bullet points in your link above), sooner or later you'll see a SPERR_NOT_ACTIVE_SESSION error.
至于 SpeechSynthesizer
,我的猜测是它在内部不使用 SAPI。 SpeechSynthesizer
的源代码可在此处获得:
关于c++ - 在 Windows 服务中使用 SAPI 进行简单的文本转语音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24293808/