如何标注接触到的COM互操作的单线程.NET对象? [英] How to mark .NET objects exposed to COM-interop as single threaded?

查看:159
本文介绍了如何标注接触到的COM互操作的单线程.NET对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当定义在C语言的COM可见类++我可以定义在头文件中支持的线程模型(线程(单)行):

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")
]

有没有设置在.NET中的线程模型(例如一个属性)的可比的方法是什么?目前,我定义我的COM类如:

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
{
    ...
}

- 编辑:

在显着答案的意见是非常重要的事情。这似乎是唯一的方式告诉RegAsm设置不同的ThreadingModel是写标有 [ComRegisterFunction] 属性自定义的注册方法。

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.

推荐答案

这真是晦涩,我从来没有见过在MIDL的线程属性。也有的MSDN Library作者

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

一个COM coclass的发布在注册表的线程的要求,使用 HKCR \ CLSID \ {GUID} \ InProcServer32中键。 ThreadingModel对声明它需要的公寓。如果丢失或设置为公寓然后宣布,它需要STA。 CoCreateObject()使用此值时,它创建的对象。这将创建一个代理,如果当前线程不是STA。

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.

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

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互操作的单线程.NET对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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