了解 COM 对象以及如何声明它们
Understanding COM Objects and how to declare them
假设我想为 IMMDeviceEnumerator 创建界面。
我在网上看到例子显示了定义:
[ComImport]
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IMMDeviceEnumerator
{
}
我的理解(可能): [ComImport]
属性指定它来自 dll。 [Guid]
属性是接口标识符。
不明白的地方:这个GUID值是怎么得到的? [InterfaceType]
属性有什么作用?我如何填写其余的功能?
我太迷茫了,想弄清楚这些东西,所有文档都非常不透明。
您自己创建 GUID。如果您不想自己分配发电机,可以在线购买。
所有接口类型都应派生自 IUnknown
。
更新:这是一个生成器。 https://www.guidgenerator.com/online-guid-generator.aspx
他们使用同一个,因为 IMMDeviceEnumerator
已经用那个特定的 GUID 定义了。如果您创建自己的界面,您将创建自己的 GUID。
你从 IUnknown
派生,因为
"Within that constraint, your custom interface can support almost any method or parameter, including asynchronous methods. You can also generate a type library for your custom interfaces so that clients can access information about your object's methods at run time. "
How was this GUID value obtained?
GUID 是作为 COM 接口定义的一部分创建的;因为你试图调用别人的对象 - 你需要使用他们的 GUID。你可以在mmdeviceapi.h the MMDevice docs指向的地方找到它。
Header file Mmdeviceapi.h defines the interfaces in the MMDevice API.
MIDL_INTERFACE("A95664D2-9614-4F35-A746-DE8DB63617E6")
IMMDeviceEnumerator : public IUnknown
执行此操作的正常方法是添加对 COM dll 或 运行 tlbimp.exe 的引用,这将为您生成一个 COM Class 包装器,其中包含所有神奇的 goo。
如果 COM 类型库不可用,那么您基本上必须通过 IDL 文件自己完成。
就像 p/invoke 签名一样,这会变得非常痛苦 - 所以 best to use an existing one if you can.
COM interop这个比较大的问题,基本上需要学一点COM,熟悉C#。如果可以的话general docs are out there, but usually if you're just trying to use a well known COM component you're best off using a library。
假设我想为 IMMDeviceEnumerator 创建界面。
我在网上看到例子显示了定义:
[ComImport]
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IMMDeviceEnumerator
{
}
我的理解(可能): [ComImport]
属性指定它来自 dll。 [Guid]
属性是接口标识符。
不明白的地方:这个GUID值是怎么得到的? [InterfaceType]
属性有什么作用?我如何填写其余的功能?
我太迷茫了,想弄清楚这些东西,所有文档都非常不透明。
您自己创建 GUID。如果您不想自己分配发电机,可以在线购买。
所有接口类型都应派生自 IUnknown
。
更新:这是一个生成器。 https://www.guidgenerator.com/online-guid-generator.aspx
他们使用同一个,因为 IMMDeviceEnumerator
已经用那个特定的 GUID 定义了。如果您创建自己的界面,您将创建自己的 GUID。
你从 IUnknown
派生,因为
"Within that constraint, your custom interface can support almost any method or parameter, including asynchronous methods. You can also generate a type library for your custom interfaces so that clients can access information about your object's methods at run time. "
How was this GUID value obtained?
GUID 是作为 COM 接口定义的一部分创建的;因为你试图调用别人的对象 - 你需要使用他们的 GUID。你可以在mmdeviceapi.h the MMDevice docs指向的地方找到它。
Header file Mmdeviceapi.h defines the interfaces in the MMDevice API.
MIDL_INTERFACE("A95664D2-9614-4F35-A746-DE8DB63617E6")
IMMDeviceEnumerator : public IUnknown
执行此操作的正常方法是添加对 COM dll 或 运行 tlbimp.exe 的引用,这将为您生成一个 COM Class 包装器,其中包含所有神奇的 goo。
如果 COM 类型库不可用,那么您基本上必须通过 IDL 文件自己完成。
就像 p/invoke 签名一样,这会变得非常痛苦 - 所以 best to use an existing one if you can.
COM interop这个比较大的问题,基本上需要学一点COM,熟悉C#。如果可以的话general docs are out there, but usually if you're just trying to use a well known COM component you're best off using a library。