我正在尝试编写DirectSound Wrapper。所以我必须将dsound.h文件的Cominterfaces导入到c#中。我目前正在研究IDirectSoundBuffer。这是这样声明的:
DEFINE_GUID(IID_IDirectSoundBuffer, 0x279AFA85, 0x4981, 0x11CE, 0xA5, 0x21, 0x00, 0x20, 0xAF, 0x0B, 0xE5, 0x60);
#undef INTERFACE
#define INTERFACE IDirectSoundBuffer
DECLARE_INTERFACE_(IDirectSoundBuffer, IUnknown)
{
// IUnknown methods
STDMETHOD(QueryInterface) (THIS_ __in REFIID, __deref_out LPVOID*) PURE;
STDMETHOD_(ULONG,AddRef) (THIS) PURE;
STDMETHOD_(ULONG,Release) (THIS) PURE;
// IDirectSoundBuffer methods
STDMETHOD(GetCaps) (THIS_ __out LPDSBCAPS pDSBufferCaps) PURE;
STDMETHOD(GetCurrentPosition) (THIS_ __out_opt LPDWORD pdwCurrentPlayCursor, __out_opt LPDWORD pdwCurrentWriteCursor) PURE;
STDMETHOD(GetFormat) (THIS_ __out_bcount_opt(dwSizeAllocated) LPWAVEFORMATEX pwfxFormat, DWORD dwSizeAllocated, __out_opt LPDWORD pdwSizeWritten) PURE;
STDMETHOD(GetVolume) (THIS_ __out LPLONG plVolume) PURE;
STDMETHOD(GetPan) (THIS_ __out LPLONG plPan) PURE;
STDMETHOD(GetFrequency) (THIS_ __out LPDWORD pdwFrequency) PURE;
STDMETHOD(GetStatus) (THIS_ __out LPDWORD pdwStatus) PURE;
STDMETHOD(Initialize) (THIS_ __in LPDIRECTSOUND pDirectSound, __in LPCDSBUFFERDESC pcDSBufferDesc) PURE;
STDMETHOD(Lock) (THIS_ DWORD dwOffset, DWORD dwBytes,
__deref_out_bcount(*pdwAudioBytes1) LPVOID *ppvAudioPtr1, __out LPDWORD pdwAudioBytes1,
__deref_opt_out_bcount(*pdwAudioBytes2) LPVOID *ppvAudioPtr2, __out_opt LPDWORD pdwAudioBytes2, DWORD dwFlags) PURE;
STDMETHOD(Play) (THIS_ DWORD dwReserved1, DWORD dwPriority, DWORD dwFlags) PURE;
STDMETHOD(SetCurrentPosition) (THIS_ DWORD dwNewPosition) PURE;
STDMETHOD(SetFormat) (THIS_ __in LPCWAVEFORMATEX pcfxFormat) PURE;
STDMETHOD(SetVolume) (THIS_ LONG lVolume) PURE;
STDMETHOD(SetPan) (THIS_ LONG lPan) PURE;
STDMETHOD(SetFrequency) (THIS_ DWORD dwFrequency) PURE;
STDMETHOD(Stop) (THIS) PURE;
STDMETHOD(Unlock) (THIS_ __in_bcount(dwAudioBytes1) LPVOID pvAudioPtr1, DWORD dwAudioBytes1,
__in_bcount_opt(dwAudioBytes2) LPVOID pvAudioPtr2, DWORD dwAudioBytes2) PURE;
STDMETHOD(Restore) (THIS) PURE;
};
所以我试着像这样翻译:
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("279AFA85-4981-11CE-A521-0020AF0BE560")]
public interface IDirectSoundBuffer : IUnknown
{
//int QueryInterface(ref Guid guid, out IntPtr comObject);
//int AddReference();
//int Release();
//IDirectSoundBuffer methods
void GetCaps([MarshalAs(UnmanagedType.LPStruct)] DSBufferCaps pDSBufferCaps); //TODO
void GetCurrentPosition([Out] out UInt32 pdwCurrentPlayCursor, [Out] out UInt32 pdwCurrentWriteCursor);
void GetFormat([Out, MarshalAs(UnmanagedType.CustomMarshaler, MarshalType = "AMEngine.Wave.WaveFormatMarshaler")] out AMEngine.Wave.WaveFormat pwfxFormat,
int dwSizeAllocated, [Out] out int pdwSizeWritten); //TODO implement
[return: MarshalAs(UnmanagedType.I4) /*See http://msdn.microsoft.com/en-us/library/aa288468%28v=vs.71%29.aspx */]
Int32 GetVolume();
[return: MarshalAs(UnmanagedType.I4)]
Int32 GetPan();
[return: MarshalAs(UnmanagedType.I4)]
Int32 GetFrequency();
[return: MarshalAs(UnmanagedType.I4)]
Int32 GetStatus();
void Initialize([In, MarshalAs(UnmanagedType.Interface)] IDirectSound pDirectSound, [In] DSBufferDescription pcDSBufferDesc);
void Lock(int dwOffset, int dwBytes,
[Out] out IntPtr ppvAudioPtr1, [Out] out int pdwAudioBytes1,
[Out] out IntPtr ppvAudioPtr2, [Out] out int pdwAudioBytes2,
DSBLock dwFlags );
void Play(int dwReserved1, int dwPriority, DSBPlayFlags dwFlags);
void SetCurrentPosition(int dwNewPosition);
void SetFormat([In] AMEngine.Wave.WaveFormat pcfxFormat);
void SetVolume(int lVolume);
void SetPan(int lPan);
void SetFrequency(int dwFrequency);
void Stop();
void Unlock([In] IntPtr pvAudioPtr1, int dwAudioBytes1,
[In] IntPtr pvAudioPtr2, int dwAudioBytes2);
void Restore();
}
IUnknown就是这个界面:
[Guid("00000000-0000-0000-C000-000000000046")]
public interface IUnknown
{
int QueryInterface(ref Guid gidd, out IntPtr ppvObject);
int AddRef();
int Release();
}
接口IDirectSoundBuffer本身有效。但对于像SetVolume这样的一些成员,我得到了一个异常E_NONINTERFACE,它告诉我必须实现QueryInterface。所以我创建了IUnknown。但我可以尝试任何我喜欢的东西。它永远不会奏效。当我尝试上面的解决方案时,我尝试播放主缓冲区时会出现异常。它告诉我,我必须为primarybufferdescritption设置其他标志。但是,如果我设置其他标志,我会在尝试创建缓冲区时遇到异常。 最后我得出的结果是接口声明的东西必须是错误的,因为如果我删除所有IUnknown的东西我可以播放音频但我只是不能设置音量,平移,...但是我可以播放它和它工作如此远。
我试图解决这个问题几个星期但我找不到任何解决方案。所以你是我最后的希望:(