Windows.Media.SpeechSynthesis.SpeechSynthesizer 构造函数中的 AccessViolationException

AccessViolationException in Windows.Media.SpeechSynthesis.SpeechSynthesizer constructor

我的应用程序在调用 Windows.Media.SpeechSynthesis.SpeechSynthesizer class 的默认构造函数时得到一个 AccessViolationException。这只发生在一台特定的机器上(至少到目前为止),它是 Surface Pro 3 运行 Windows 8.1。安装了该应用程序的所有其他机器都不会遇到此问题。

我的代码是这样的:

class Speech
{
    private Windows.Media.SpeechSynthesis.SpeechSynthesizer _Synthesizer;
    public Speech()
    {
        SetVoice();
    }

    private void SetVoice()
    {
        try
        {
            // Exception is raised here
            _Synthesizer = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();
        }
        catch (Exception e)
        {

        }
    }
}

事件日志中显示的堆栈跟踪如下所示:

Description: The process was terminated due to an unhandled exception
Exception Info: System.AccessViolationException
Stack:
    at Windows.Media.SpeechSynthesis.SpeechSynthesizer..ctor()
    at ... Speech.SetVoice()
    at ... Speech..ctor()

我使用 ProcDump 捕获了一个转储文件,并且可以在堆栈跟踪中看到它(它不显示 "my code" 只是它后面的代码):

sapi.dll!CSpObjectTokenEnumBuilder::CheckTokenSignature(struct ISpDataKey *) Unknown
sapi.dll!CSpObjectTokenEnumBuilder::AddTokensFromDataKey(struct ISpDataKey *,unsigned short const *,unsigned short const *) Unknown
sapi.dll!CSpObjectTokenCategory::InternalEnumTokens(unsigned short const *,unsigned short const *,struct IEnumSpObjectTokens * *,int) Unknown
sapi.dll!CSpObjectTokenCategory::EnumTokens(unsigned short const *,unsigned short const *,struct IEnumSpObjectTokens * *) Unknown
Windows.Media.SpeechSynthesis.dll!SpEnumTokens(unsigned short const *,unsigned short const *,unsigned short const *,struct IEnumSpObjectTokens * *) Unknown
Windows.Media.SpeechSynthesis.dll!Windows::Media::SpeechSynthesis::InstalledVoicesStatic::get_AllVoices(struct Windows::Foundation::Collections::IVectorView<class Windows::Media::SpeechSynthesis::VoiceInformation *> * *) Unknown
Windows.Media.SpeechSynthesis.dll!Windows::Media::SpeechSynthesis::InstalledVoicesStatic::get_DefaultVoiceWithRank(struct Windows::Media::SpeechSynthesis::IVoiceInformation * *,unsigned int *) Unknown
Windows.Media.SpeechSynthesis.dll!Windows::Media::SpeechSynthesis::CSpeechSynthesizer::GetDefaultVoice(struct Windows::Media::SpeechSynthesis::IVoiceInformation * *,unsigned int *) Unknown
Windows.Media.SpeechSynthesis.dll!Windows::Media::SpeechSynthesis::CSpeechSynthesizer::CreateSynthesizerObject(void) Unknown
Windows.Media.SpeechSynthesis.dll!Windows::Media::SpeechSynthesis::CSpeechSynthesizer::RuntimeClassInitialize(void) Unknown
Windows.Media.SpeechSynthesis.dll!Microsoft::WRL::Details::MakeAndInitialize<class Windows::Media::SpeechSynthesis::CSpeechSynthesizer,class Windows::Media::SpeechSynthesis::CSpeechSynthesizer>(class Windows::Media::SpeechSynthesis::CSpeechSynthesizer * *) Unknown
Windows.Media.SpeechSynthesis.dll!Windows::Media::SpeechSynthesis::InstalledVoicesStatic::ActivateInstance(struct IInspectable * *) Unknown

错误似乎是从 try/catch 块中逃逸出来的,这真的很奇怪。这仅在一个客户的机器上

知道发生了什么事吗?

我认为您的客户在注册表项 HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\Speech\Voices\Tokens 中有错误数据,这是 SAPI 存储有关系统上安装了哪些语音的信息的地方。

显然,存在一些键(假设代码试图从注册表项添加令牌),但值已损坏,导致本机代码中出现 AV。

其他要检查的区域(如果适用于您的系统)是:

  • HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\Speech\Voices\Tokens
  • HKEY_LOCAL_MACHINE\WOW6432Node\SOFTWARE\MICROSOFT\Speech\Voices\Tokens
  • HKEY_CURRENT_USER\WOW6432Node\SOFTWARE\MICROSOFT\Speech\Voices\Tokens