gpt4 book ai didi

C# COM 互操作 : How to translate instructions from C++?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:36:23 24 4
gpt4 key购买 nike

我正在尝试将我的相机制造商为 C++ 提供的 COM Interop 指令翻译成 C#。

他们写道:

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 );

他们还给出了一个接口(interface)签名和一个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
}

来自 another source我首先得到了一个类似的 DirectShow 包装器来访问相机,包括一个 COM 导入的接口(interface) IBaseFilter。我现在如何翻译第一个示例?

我试过了

IManufacturersInterface control = sourceFilter as IManufacturersInterface; // sourceFilter is declared as IBaseFilter
control.SetShutterSpeed(eShutterSpeed.ssAuto1);

但控制在转换后为空。

抱歉,如果我含糊不清,我不知道我在这里做什么。这是我第一次不得不使用 COM Interop。它表明,嗯? =)

最佳答案

执行 com 互操作的最简单方法是让 Visual Studio 为您创建互操作 - 我将它用于许多不同的 com 对象,并且从未遇到过任何问题。首先,在您的 C# 项目中选择“添加引用”并选择“COM”选项卡,在列表中找到相机制造商的对象,您应该完成了。您现在可以像使用 native C# 一样使用 com 对象。

关于C# COM 互操作 : How to translate instructions from C++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3555434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com