C#调用Microsoft.DirectX.DirectSound问题记录及解决

在使用C#调用Microsoft.DirectX.DirectSound中,出现了一下问题,特此记录一下;以下问题是按照顺序出现的。

问题1:初始化结构体WaveFormat或其他变量时卡死

问题描述:

有关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。

************** 异常文本 **************
System.IO.FileLoadException: 混合模式程序集是针对“v1.1.4322”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。
   在 Altotech.Beta.Test.frmTest.btnSpeechTest_Click(Object sender, EventArgs e)
   在 System.Windows.Forms.Control.OnClick(EventArgs e)
   在 System.Windows.Forms.Button.OnClick(EventArgs e)
   在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
   在 System.Windows.Forms.Button.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** 已加载的程序集 **************
mscorlib
    程序集版本:4.0.0.0
    Win32 版本:4.6.1055.0 built by: NETFXREL2
    基本代码:file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
Altotech.Beta.Test
    程序集版本:1.0.0.0
    Win32 版本:1.0.0.0
    基本代码:file:///D:/Amon/Documents/Visual%20Studio%202015/Projects/Altotech.Beta/Altotech.Beta.Test/bin/Debug/Altotech.Beta.Test.exe
----------------------------------------
System.Windows.Forms
    程序集版本:4.0.0.0
    Win32 版本:4.6.1055.0 built by: NETFXREL2
    基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    程序集版本:4.0.0.0
    Win32 版本:4.6.1081.0 built by: NETFXREL3STAGE
    基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    程序集版本:4.0.0.0
    Win32 版本:4.6.1068.2 built by: NETFXREL3STAGE
    基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
    程序集版本:4.0.0.0
    Win32 版本:4.6.1055.0 built by: NETFXREL2
    基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Core
    程序集版本:4.0.0.0
    Win32 版本:4.6.1055.0 built by: NETFXREL2
    基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Xml
    程序集版本:4.0.0.0
    Win32 版本:4.6.1064.2 built by: NETFXREL3STAGE
    基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
mscorlib.resources
    程序集版本:4.0.0.0
    Win32 版本:4.6.1055.0 built by: NETFXREL2
    基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/mscorlib.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/mscorlib.resources.dll
----------------------------------------
System.Windows.Forms.resources
    程序集版本:4.0.0.0
    Win32 版本:4.6.1055.0 built by: NETFXREL2
    基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------

************** JIT 调试 **************
要启用实时(JIT)调试,
该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置
jitDebugging 值。
编译应用程序时还必须启用
调试。

例如:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>
启用 JIT 调试后,任何未经处理的异常
都将被发送到在此计算机上注册的 JIT 调试器,
而不是由此对话框处理。

解决办法:

修改App.config,修改方法如下:
原App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>

修改后的App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
    </startup>
</configuration>

详细描述请参考:
http://www.cnblogs.com/zcftech/archive/2013/03/22/2976385.html

问题2:其他信息: DLL“Microsoft.DirectX.DirectSound.dll”正尝试在 OS 加载程序锁内执行托管代码。

问题描述:

托管调试助手“LoaderLock”在“D:\Amon\Documents\Visual Studio 2015\Projects\Altotech.Beta\Altotech.Beta.Test\bin\x86\Debug\Altotech.Beta.Test.vshost.exe”中检测到问题。
其他信息: DLL“D:\Amon\Documents\Visual Studio 2015\Projects\Altotech.Beta\Altotech.Beta.Test\bin\x86\Debug\Microsoft.DirectX.DirectSound.dll”正尝试在 OS 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起。

解决方法:

把Visual Studio菜单的 调试->异常->Managed Debuggin Assistants->LoaderLock 的选中状态去掉(我没有找到异常菜单,但是使用快捷键Ctrl+Alt+E打开了异常对话框)。我认为这种做法不是解决办法,只是把异常给关掉了,但是暂时没有发现别的方法。

详细解决方法参考:
http://www.cnblogs.com/suguoqiang/archive/2012/07/17/2596529.html

问题3:System.TypeInitializationException

问题描述:

上周Win10系统升级,造成了很多问题,所以重新安装了一下Win10。安装好Visual Studio 2015后,运行程序,程序报出这个异常,见下图:
这里写图片描述

未处理System.TypeInitializationException
  HResult=-2146233036
  Message=“AltotechRobot.Msc”的类型初始值设定项引发异常。
  Source=AltotechRobot
  TypeName=AltotechRobot.Msc
  StackTrace:
       在 AltotechRobot.Msc.set_AutoTranslateUserword(String value)
       在 AltotechRobot.SRecognition..ctor(String[] fg) 位置 D:\User\Documents\Visual Studio 2015\Projects\AltotechRobot\AltotechRobot\SpRecognition.cs:行号 57
       在 AltotechRobot.frmMain.frmMain_Load(Object sender, EventArgs e) 位置 D:\User\Documents\Visual Studio 2015\Projects\AltotechRobot\AltotechRobot\frmMain.cs:行号 76
       在 System.Windows.Forms.Form.OnLoad(EventArgs e)
       在 System.Windows.Forms.Form.SetVisibleCore(Boolean value)
       在 System.Windows.Forms.Control.set_Visible(Boolean value)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 AltotechRobot.Program.Main() 位置 D:\User\Documents\Visual Studio 2015\Projects\AltotechRobot\AltotechRobot\Program.cs:行号 19
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
       HResult=-2146233036
       Message=“AltotechRobot.SoundRecorder”的类型初始值设定项引发异常。
       Source=AltotechRobot
       TypeName=AltotechRobot.SoundRecorder
       StackTrace:
            在 AltotechRobot.SoundRecorder.GetNewSoundRecoder()
            在 AltotechRobot.Msc..cctor() 位置 D:\User\Documents\Visual Studio 2015\Projects\AltotechRobot\AltotechRobot\Msc.cs:行号 25
       InnerException: 
            HResult=-2146232832
            Message=应用程序中的错误。
            Source=Microsoft.DirectX.DirectSound
            StackTrace:
                 在 Microsoft.DirectX.DirectSound.CaptureBuffer..ctor(CaptureBufferDescription desc, Capture parent)
                 在 AltotechRobot.SoundRecorder.CreateCaptureBuffer() 位置 D:\User\Documents\Visual Studio 2015\Projects\AltotechRobot\AltotechRobot\SoundRecorder.cs:行号 194
                 在 AltotechRobot.SoundRecorder..cctor() 位置 D:\User\Documents\Visual Studio 2015\Projects\AltotechRobot\AltotechRobot\SoundRecorder.cs:行号 82
            InnerException: 

解决方法:

该错误是因为系统没有检测到麦克风引起,所以解决方法是
- 在程序中增加异常处理机制,捕捉异常,及时处理并做相应提示。
- 插入麦克风,或者启用虚拟麦克风

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿蒙Amon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值