如何将暴露给COM-interop的.NET对象标记为单线程? [英] How to mark .NET objects exposed to COM-interop as single threaded?

查看:156
本文介绍了如何将暴露给COM-interop的.NET对象标记为单线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中定义一个COM-visible类时,我可以在头文件( threading(single))中定义支持的线程模型:

  [
coclass,
default(IComInterface),
threading(single),
vi_progid Example.ComClass),
progid(Example.ComClass.1),
version(1.0),
uuid(72861DF5-4C77-43ec-A4DC-ED04396F0CCD)
]

在.NET中有类似的设置线程模型的方法属性)?我目前定义我的COM类为:

  [Guid(67155A91-2948-43f5-B07F-5C55CDD240E5)] 
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IComInterface
{
...
}


[Guid(DC5E6955-BB29-44c8-9FC0-6AADEEB2AFFB)]
[ClassInterface(ClassInterfaceType.None)]
[ProgId(Example.ComClass)]
public class ComClass:IComInterface
{
...
}

- 编辑:



标记答案上的评论是非常重要的。看起来像告诉RegAsm设置不同的ThreadingModel的唯一方法是写一个标记有 [ComRegisterFunction] 属性的自定义注册方法。

解决方案

这真的很模糊,我从来没有见过MIDL的线程属性。也没有 MSDN库作者。 / p>

COM coclass使用 HKCR\CLSID\ {guid} \InProcServer32 键。 ThreadingModel值声明它需要的公寓。如果它缺失或设置为公寓,则宣布它需要STA。 CoCreateObject()在创建对象时使用此值。如果当前线程不是STA,它将创建一个代理。



在.NET中编写自定义注册表值有点尴尬,你必须使用[ComRegisterFunction]属性并接管Regasm.exe的职责。


When defining a COM-visible class in C++ I can define the supported threading model in the header file (the threading(single) line):

[
    coclass,
    default(IComInterface),
    threading(single),
    vi_progid("Example.ComClass"),
    progid("Example.ComClass.1"),
    version(1.0),
    uuid("72861DF5-4C77-43ec-A4DC-ED04396F0CCD")
]

Is there a comparable way of setting the threading model in .NET (for example an attribute)? I currently define my COM-class as such:

[Guid("67155A91-2948-43f5-B07F-5C55CDD240E5")]
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IComInterface
{
    ...
}


[Guid("DC5E6955-BB29-44c8-9FC0-6AADEEB2AFFB")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("Example.ComClass")]
public class ComClass : IComInterface
{
    ...
}

--edit:

The comments on the marked answer are the really important thing. It seems like the only way to tell RegAsm to set a different ThreadingModel is to write a custom registration method marked with the [ComRegisterFunction]attribute.

解决方案

That's really obscure, I've never seen the "threading" attribute in MIDL. Nor have the MSDN Library authors.

A COM coclass publishes its threading requirements in the registry, using the HKCR\CLSID\{guid}\InProcServer32 key. The ThreadingModel value declares the apartment it needs. If it is missing or is set to "Apartment" then is announces that it needs STA. CoCreateObject() uses this value when it creates the object. It will create a proxy if the current thread is not STA.

Writing custom registry values is a bit awkward in .NET, you'd have to use the [ComRegisterFunction] attribute and take over the duties of Regasm.exe.

这篇关于如何将暴露给COM-interop的.NET对象标记为单线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆