c# - 如何使 C# Com 对象成为 GlobalMultiUse?

标签 c# vba com-interop atl

我正在做一些 COM 互操作(VBA 客户端)并且有一个功能我可能认为是理所当然的,如果在 idl 中使用 idl 属性 appobject。这适用于 ATL/C++,因此我可以编写以下 IDL(ATL 项目)

[
    uuid(183343ca-6154-4c1e-9a6b-927e5b279332),
    version(1.0),
]
library GlobalCOMObjectLib
{
    importlib("stdole2.tlb");

    [
        odl,
        uuid(f1bd66ae-4fc9-45a4-8a2b-60df31d9bcfe),
        version(1.0),
        dual,
        oleautomation
    ]
    interface ISampleGlobalObject : IDispatch
    {
        HRESULT Line([in]BSTR sIn, [out, retval] BSTR* outRetVal);
    };

    [
        uuid(6a905a18-37d2-4ac1-a663-871f7dc99af7),
        version(1.0),
        appobject 
    ]
    coclass SampleGlobalObject
    {
        [default] interface ISampleGlobalObject;
    };
};

然后在 VBA 中我不需要新建 coclass 我可以编写以下内容

Sub TestGlobal3()
    Debug.Print SampleGlobalObject.Line("hello")
End Sub

如何在 C# 中做同样的事情,appobject 作为属性无法识别(波浪线)

namespace foo
{
    public interface ISampleGlobalObjectCSharp
    {
        string Line(string sLine);
    }

    [ClassInterface(ClassInterfaceType.None)]
    [ComDefaultInterface(typeof(ISampleGlobalObjectCSharp))]
    [appobject]
    public class SampleGlobalObject : ISampleGlobalObjectCSharp
    {
        string ISampleGlobalObjectCSharp.Line(string sLine)
        {
            return sLine;
        }
    }

谷歌搜索遇到在从 VB6 过渡到 C# 的时代编写的页面,他们认为这是不可能的。 VB6 术语是 GlobalMultiUse,如果它触发任何内存的话。

最佳答案

将评论复制到答案以保留它:

Those pages are correct. Ideally the type library exporter would pay attention to a [TypeLibType] attribute, but it doesn't. It is not like you can't get it, but it is error-prone and very hard to automate. Decompile the .tlb file with Oleview.exe, File > View TypeLib, copy/paste the decompiled idl and edit in the [appobject] attribute, rebuild the .tlb with midl.exe /tlb. The workaround in VBA is much easier. Fwiw, GlobalMultiUse is something completely different, affects only the REGCLS that an out-of-process server uses.

– Hans Passant 2018 年 5 月 4 日 13:05


OP 声明此评论是正确的。

关于c# - 如何使 C# Com 对象成为 GlobalMultiUse?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50174976/

相关文章:

VBA Excel,输入框作为整数不匹配

sql-server - Long,Excel中的一列列表,转换为SQL where in语句

visual-studio-2008 - VS 2008 中的 Wrapper Assembly Key File 设置在哪里?

c# - 尝试创建新的任务计划程序任务时抛出异常

c# - 有没有办法强制在 C# 中初始化静态字段?

c# - .Net PropertyGrid DropDownList - 返回值不同于显示值

c# - 从 ASP.NET Web 服务中删除了 WebMethod,方法仍然显示

excel - 如何让 VBA 函数存储单元格地址值而不是这些单元格地址处的值

windows - 是否有关于 IdentityUnmarshal 接口(interface)的文档?

c# - 当 viewmodel 包含 ICommand 时使用 Mock 进行单元测试