示例:一个开源的MPEG2解码器的接口源代码如下
在C#中的定义:
需要注意的是:ComInterfaceType需要指定为InterfaceIsIUnknown,如果指定为InterfaceIsDual,虽然也可以执行,但是接口之间的顺序为变为乱序,出现调用不正确.第二个地方是对于接口中定义的返回值不是HRESULT的函数,需要加上PreserveSig指定不进行转换.
interface IMpeg2DecFilter:
public IUnknown
{
STDMETHOD(SetDeinterlaceMethod) (THIS_ int iMethod) PURE;
STDMETHOD_(int, GetDeinterlaceMethod) (THIS) PURE;
// Brightness: -255.0 to 255.0, default 0.0
// Contrast: 0.0 to 10.0, default 1.0
// Hue: -180.0 to +180.0, default 0.0
// Saturation: 0.0 to 10.0, default 1.0
STDMETHOD(SetBrightness) (THIS_ double bright) PURE;
STDMETHOD(SetContrast) (THIS_ double cont) PURE;
STDMETHOD(SetHue) (THIS_ double hue) PURE;
STDMETHOD(SetSaturation) (THIS_ double sat) PURE;
STDMETHOD_(double, GetBrightness) (THIS) PURE;
STDMETHOD_(double, GetContrast) (THIS) PURE;
STDMETHOD_(double, GetHue) (THIS) PURE;
STDMETHOD_(double, GetSaturation) (THIS) PURE;
STDMETHOD(EnableForcedSubtitles) (THIS_ bool fEnable) PURE;
STDMETHOD_(bool, IsForcedSubtitlesEnabled) (THIS) PURE;
STDMETHOD(EnablePlanarYUV) (THIS_ bool fEnable) PURE;
STDMETHOD_(bool, IsPlanarYUVEnabled) (THIS) PURE;
};
public IUnknown
{
STDMETHOD(SetDeinterlaceMethod) (THIS_ int iMethod) PURE;
STDMETHOD_(int, GetDeinterlaceMethod) (THIS) PURE;
// Brightness: -255.0 to 255.0, default 0.0
// Contrast: 0.0 to 10.0, default 1.0
// Hue: -180.0 to +180.0, default 0.0
// Saturation: 0.0 to 10.0, default 1.0
STDMETHOD(SetBrightness) (THIS_ double bright) PURE;
STDMETHOD(SetContrast) (THIS_ double cont) PURE;
STDMETHOD(SetHue) (THIS_ double hue) PURE;
STDMETHOD(SetSaturation) (THIS_ double sat) PURE;
STDMETHOD_(double, GetBrightness) (THIS) PURE;
STDMETHOD_(double, GetContrast) (THIS) PURE;
STDMETHOD_(double, GetHue) (THIS) PURE;
STDMETHOD_(double, GetSaturation) (THIS) PURE;
STDMETHOD(EnableForcedSubtitles) (THIS_ bool fEnable) PURE;
STDMETHOD_(bool, IsForcedSubtitlesEnabled) (THIS) PURE;
STDMETHOD(EnablePlanarYUV) (THIS_ bool fEnable) PURE;
STDMETHOD_(bool, IsPlanarYUVEnabled) (THIS) PURE;
};
[ComImport,Guid("B31FB86F-46C4-48b0-BB2B-6935CF404E35"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
protected interface IMpeg2DecFilter
{
int SetDeinterlaceMethod(int iMethod);
[PreserveSig]
int GetDeinterlaceMethod();
int SetBrightness(double brit);
int SetContrast(double cont);
int SetHue(double sat);
int SetSaturation(double hue);
[PreserveSig]
double GetBrightness();
[PreserveSig]
double GetContrast();
[PreserveSig]
double GetHue();
[PreserveSig]
double GetSaturation();
int EnableForcedSubtitles(bool fEnable);
[PreserveSig]
bool IsForcedSubtitlesEnabled();
int EnablePlanarYUV(bool fEnable);
[PreserveSig]
bool IsPlanarYUVEnabled();
}
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
protected interface IMpeg2DecFilter
{
int SetDeinterlaceMethod(int iMethod);
[PreserveSig]
int GetDeinterlaceMethod();
int SetBrightness(double brit);
int SetContrast(double cont);
int SetHue(double sat);
int SetSaturation(double hue);
[PreserveSig]
double GetBrightness();
[PreserveSig]
double GetContrast();
[PreserveSig]
double GetHue();
[PreserveSig]
double GetSaturation();
int EnableForcedSubtitles(bool fEnable);
[PreserveSig]
bool IsForcedSubtitlesEnabled();
int EnablePlanarYUV(bool fEnable);
[PreserveSig]
bool IsPlanarYUVEnabled();
}