0

我正在尝试编写 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 东西我可以播放音频但我不能设置音量,平移,......但我可以播放它工作这么远。

几周以来我一直在尝试解决这个问题,但我找不到任何解决方案。所以你是我最后的希望:(

4

0 回答 0