C# COM 互操作:如何翻译 C++ 指令?
我正在尝试将我的相机制造商提供的 C++ COM Interop 指令转换为 C#。
他们写道:
要获取接口,您可以使用普通的 COM 函数从捕获过滤器请求所需的特定接口。 例如:
IBaseFilter* pSourceFilter; ... CComQIPtr
pKs( pSourceFilter ); pKs->SetShutterSpeed( ssAuto1 );
他们还给出了接口签名和Guid。签名看起来像
interface IManufacturersInterface: IUnknown
{
// more stuff
HRESULT SetShutterSpeed( [in] eShutterSpeed lShutter );
// more stuff
}
我翻译成 C# 的
[ComImport]
[Guid("926ddb16-3c8e-476c-9068-eb4555a99231")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IManufacturersInterface
{
// more stuff
[PreserveSig]
int SetShutterSpeed([In] eShutterSpeed lShutter);
// more stuff
}
From 另一个来源 我首先得到了一个类似的 DirectShow 包装器来访问相机,其中包括一个 COM 导入的接口 IBaseFilter
。我现在该如何翻译第一个例子?
我尝试过
IManufacturersInterface control = sourceFilter as IManufacturersInterface; // sourceFilter is declared as IBaseFilter
control.SetShutterSpeed(eShutterSpeed.ssAuto1);
,但在转换后控制为空。
抱歉,如果我含糊其词,我不知道我在这里做什么。这是我第一次不得不使用 COM Interop。它表明,嗯? =)
I am trying to translate the COM Interop instructions given by my camera manufacturer for C++ to C#.
They write:
To obtain the interface, you use the normal COM functions to ask for the specific interface you need from the capture filter.
For example:IBaseFilter* pSourceFilter; ... CComQIPtr<IManufacturersInterface> pKs( pSourceFilter ); pKs->SetShutterSpeed( ssAuto1 );
They also give an interface signature and a Guid. The signature looks like
interface IManufacturersInterface: IUnknown
{
// more stuff
HRESULT SetShutterSpeed( [in] eShutterSpeed lShutter );
// more stuff
}
which I translated into C# as
[ComImport]
[Guid("926ddb16-3c8e-476c-9068-eb4555a99231")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IManufacturersInterface
{
// more stuff
[PreserveSig]
int SetShutterSpeed([In] eShutterSpeed lShutter);
// more stuff
}
From another source I got a similar DirectShow wrapper to access the camera in the first place, including an COM-imported interface IBaseFilter
. How would I now translate the first example?
I tried
IManufacturersInterface control = sourceFilter as IManufacturersInterface; // sourceFilter is declared as IBaseFilter
control.SetShutterSpeed(eShutterSpeed.ssAuto1);
but control is null after the cast.
Sorry if I am vague, I have no real clue what I am doing here. This is the first time I had to use COM Interop. It shows, hm? =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
进行 com 互操作的最简单方法是让 Visual Studio 为您创建互操作 - 我将它与许多不同的 com 对象一起使用,并且从未遇到过任何问题。首先,在您的 C# 项目中选择“添加引用”并选择“COM”选项卡,在列表中找到相机制造商的对象,然后您就应该完成了。您现在可以像使用本机 C# 一样使用 com 对象。
The easiest way to do com interop is to let Visual Studio create the interop for you - I used it with many different com objects and never had any issues with it. To get started, in your C# project select Add Reference and select the tab COM, find the camera manufacturer's object in the list and you should be done. You can now use the com objects as if they were native C#.