vhost.exe c#에서 웨이브 작업을하려고 할 때 충돌합니다.
문제
C#에서 WaveOut을 사용하여 동시에 더 많은 WAV 파일을 재생하려고했습니다 (적어도 다른 파일). (SoundPlayer Object는 한 번에 하나만 재생하며 DirectSound 및 MediaPlayer Objects 또는이 간단한 목표를위한 기타 고급 기술을 사용하고 싶지 않습니다.)
작동하는 C ++ 코드를 C#에 포팅했는데 (마샬링하는 방법과 기본 Win32 DLL-S를 호출하는 방법과 관리되지 않는 메모리를 할당하고 잠그는 방법을 연구 한 후에도 vhost.exe 충돌을 일으키고 실마리가 없습니다. 왜 그렇게 하는가. (표준 Windows 오류 대화 상자가 나타나고 프로그램이 충돌하고 종료됩니다.)
아무도 아이디어가 있습니까?
다음은 해당 수업의 출처입니다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using XiVo.PlayThrough;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Timers;
namespace RTS
{
class WaveObject
{
protected IntPtr wo;
protected byte[] Adat=null;
protected WaveNative.WaveHdr woh;
protected int pgc;
public class NotAWaveformFileException : Exception
{
public NotAWaveformFileException(string str) : base(str) { }
public NotAWaveformFileException() { }
}
public class WaveFileIsCorruptedException : Exception
{
public WaveFileIsCorruptedException(string str) : base(str) { }
public WaveFileIsCorruptedException() { }
}
public class WaveMapperCouldNotBeOpenedException : Exception
{
public WaveMapperCouldNotBeOpenedException(string str) : base(str) { }
public WaveMapperCouldNotBeOpenedException() { }
}
public WaveObject() {}
public WaveObject(string Fn) : this() { Load(Fn); }
public void Load(string Fn)
{
IntPtr mmio;
NativeMMIO.mmckInfo Main, Sub;
WaveFormat wfx;
StringBuilder str=new StringBuilder(Fn);
int r;
mmio = NativeMMIO.mmioOpen(str,IntPtr.Zero,NativeMMIO.MMIO_READ);
if (mmio == IntPtr.Zero)
{
throw new FileNotFoundException(Fn + "not found!");
}
Main.fccType = NativeMMIO.mmioFOURCC('W', 'A', 'V', 'E');
if (NativeMMIO.mmioDescend(mmio, out Main, IntPtr.Zero, NativeMMIO.MMIO_FINDRIFF) != 0)
{
throw new NotAWaveformFileException();
}
Sub.ckid = NativeMMIO.mmioFOURCC('f', 'm', 't', ' ');
if (NativeMMIO.mmioDescend(mmio, out Sub, out Main, NativeMMIO.MMIO_FINDCHUNK) != 0)
{
throw new WaveFileIsCorruptedException("fmt chunk is not found!");
}
byte[] raw = new byte[Sub.cksize+2];
NativeMMIO.mmioRead(mmio, raw, (int)Sub.cksize);
GCHandle conv = GCHandle.Alloc(raw, GCHandleType.Pinned); // mapping a WaveFormat structure from the byte array
wfx = (WaveFormat)Marshal.PtrToStructure(conv.AddrOfPinnedObject(), typeof(WaveFormat));
conv.Free();
Sub.ckid = NativeMMIO.mmioFOURCC('d', 'a', 't', 'a');
if (NativeMMIO.mmioDescend(mmio, out Sub, out Main, NativeMMIO.MMIO_FINDCHUNK) != 0)
{
throw new WaveFileIsCorruptedException("data chunk is not found!");
}
Adat = new byte[Sub.cksize+2];
NativeMMIO.mmioRead(mmio, Adat, (int)Sub.cksize);
NativeMMIO.mmioClose(mmio, 0);
wfx.cbSize = (short)Marshal.SizeOf(wfx);
unchecked // WAVE_MAPPER is 0xFFFFFFFF and it does not let it convert to int otherwise
{
int res = WaveNative.waveOutOpen(out wo, (int)WaveNative.WAVE_MAPPER, wfx, null, 0, (int)WaveNative.CALLBACK_NULL);
if (res != WaveNative.MMSYSERR_NOERROR)
{
throw new WaveMapperCouldNotBeOpenedException();
}
}
woh.lpData = Marshal.AllocHGlobal((int)Sub.cksize); // alloc memory for the buffer
Marshal.Copy(Adat, 0, woh.lpData, (int)Sub.cksize);
woh.dwBufferLength = (int)Sub.cksize;
woh.dwBytesRecorded = 0;
woh.dwUser = IntPtr.Zero;
woh.dwFlags = 0;
woh.dwLoops = 1000000;
woh.reserved = 0;
woh.lpNext = IntPtr.Zero;
r = WaveNative.waveOutPrepareHeader(wo, ref woh, Marshal.SizeOf(woh));
pgc = System.Environment.TickCount;
}
public void Play()
{
if (System.Environment.TickCount - pgc > 50)
{
if (wo == null) throw new Exception("wo somehow became null.");
int res = WaveNative.waveOutReset(wo);
if (res != WaveNative.MMSYSERR_NOERROR) throw new Exception(string.Format("waveOutReset {0}",res));
res=WaveNative.waveOutWrite(wo, ref woh, Marshal.SizeOf(woh));
if ((res != WaveNative.MMSYSERR_NOERROR) && (res!=33)) throw new Exception(string.Format("waveOutWrite {0}",res));
}
}
~WaveObject()
{
Marshal.FreeHGlobal(woh.lpData); // release memory
}
}
}
해결책
많은 인터넷 검색 후에 나는 버그가 winmm.dll 자체에 있다는 것을 깨달았습니다. 어떤 상황에서는 도우미 스레드가 충돌하여 호스트 응용 프로그램도 중단합니다.
제휴하지 않습니다 StackOverflow