C# COM 互操作:如何翻译 C++ 指令?

发布于 2024-09-15 09:25:54 字数 1346 浏览 3 评论 0原文

我正在尝试将我的相机制造商提供的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韬韬不绝 2024-09-22 09:25:54

进行 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#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文