草庐IT

c# - Regfree COM 事件从其他线程失败

coder 2024-06-03 原文

我有一个 COM 可见 .NET 类,它公开事件并从 VB6 中使用。在过去的几天里,我一直试图让它与 regfree COM 一起工作,但没有成功。

  • 当从原始线程触发事件时,VB6 事件以 regfree 模式运行。
  • VB6 事件在注册类型库时从另一个线程触发时运行。 (regasm/tlb/codebase 后跟 regasm/codebase/unregister,后者不会注销 tlb)

当在 regfree 模式下从另一个线程触发时,它会抛出异常,因此永远不会执行 VB6 事件代码。

System.Reflection.TargetException: Object does not match target type.

   at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
   at Example.Vb6RegFreeCom.IExampleClassEvents.TestEvent()
   at Example.Vb6RegFreeCom.ExampleClass.OnTestEvent(Action func) in ExampleClass.cs:line 78

我可以想到两种情况:1) list 缺少与 tlb 注册相关的内容,或者 2) 创建新线程时激活上下文丢失。不幸的是,我不知道如何找出是哪种情况,甚至可能是由其他原因引起的。

下面是一个显示我的问题的基本示例。

list (VB6 可执行文件)

<?xml version="1.0" encoding="utf-8"?>
<assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd" manifestVersion="1.0" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <assemblyIdentity name="VB6COM" version="1.0.0.0" type="win32" />
  <dependency xmlns="urn:schemas-microsoft-com:asm.v2">
    <dependentAssembly codebase="Example.Vb6RegFreeCom.tlb">
      <assemblyIdentity name="Example.Vb6RegFreeCom" version="1.0.0.0" publicKeyToken="B5630FCEE39CF455" language="neutral" processorArchitecture="x86" />
    </dependentAssembly>
  </dependency>
</assembly>

list (C# DLL)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity name="Example.Vb6RegFreeCom" version="1.0.0.0" publicKeyToken="b5630fcee39cf455" processorArchitecture="x86"></assemblyIdentity>
  <clrClass clsid="{8D51802D-0DAE-40F2-8559-7BF63C92E261}" progid="Example.Vb6RegFreeCom.ExampleClass" threadingModel="Both" name="Example.Vb6RegFreeCom.ExampleClass" runtimeVersion="v4.0.30319"></clrClass>
  <file name="Example.Vb6RegFreeCom.dll" hashalg="SHA1"></file>
  <!--
  <file name="Example.Vb6RegFreeCom.TLB">
    <typelib tlbid="{FABD4158-AFDB-4223-BB09-AB8B45E3816E}" version="1.0" flags="" helpdir="" />
  </file>
  -->
</assembly>

C#(平台目标:x86)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

using Timer = System.Threading.Timer;
using FormsTimer = System.Windows.Forms.Timer;

namespace Example.Vb6RegFreeCom {
    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    [Guid("467EB602-B7C4-4752-824A-B1BC164C7962")]
    public interface IExampleClass {
        [DispId(1)] int Test(int mode);
    }

    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    [Guid("2669EBDB-16D9-45C8-B0A3-ED2CEE26862C")]
    public interface IExampleClassEvents {
        [DispId(1)] void TestEvent();
    }

    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    [ComSourceInterfaces(typeof(IExampleClassEvents))]
    [Guid("8D51802D-0DAE-40F2-8559-7BF63C92E261")]
    public class ExampleClass: IExampleClass {
        public event Action TestEvent;

        public int Test(int mode) {
            var tempEvent = TestEvent;
            if (tempEvent == null) return -1;

            switch (mode) {
                case 0:
                    tempEvent();
                    break;
                case 1:
                    var staThread = new Thread(() => OnTestEvent(tempEvent) );

                    //if (!staThread.TrySetApartmentState(ApartmentState.STA)) MessageBox.Show("Failed to set STA thread.");

                    staThread.Start();
                    break;
                case 2:
                    var invoker = new Invoker();
                    var otherThread = new Thread(() => invoker.Invoke((Action)(() => OnTestEvent(tempEvent))));
                    otherThread.Start();
                    break;
                case 3:
                    var timer = new FormsTimer();
                    timer.Tick += (_1, _2) => { timer.Dispose(); OnTestEvent(tempEvent); };
                    timer.Interval = 100;
                    timer.Start();
                    break;
                default:
                    return -2;
            }

            return 1;
        }

        internal static void OnTestEvent(Action func) {
            try { func(); } catch (Exception err) { MessageBox.Show(err.ToString()); }
        }
    }

    internal class Invoker : Control {
        internal Invoker() {
            this.CreateHandle();
        }
    }
}

VB6

Option Explicit

Dim WithEvents DotNetObject As ExampleClass

Private Sub cmdImmediate_Click()
    CallDotNet 0
End Sub

Private Sub cmdOtherThread_Click()
    CallDotNet 1
End Sub

Private Sub cmdSameThread_Click()
    CallDotNet 2
End Sub

Private Sub Form_Load()
    Set DotNetObject = New ExampleClass
End Sub

Private Sub CallDotNet(TestMode As Long)
    Dim ReturnValue As Long
    ReturnValue = DotNetObject.Test(TestMode)

    If ReturnValue <> 1 Then MsgBox "Return value is " & ReturnValue
End Sub

Private Sub DotNetObject_TestEvent()
    MsgBox "Event was raised."
End Sub

最佳答案

对于多线程,调用必须被编码。这需要额外的信息,这些信息由 comInterfaceExternalProxyStubtypelib 元素提供。我已经尝试过这些,但直到现在才找到合适的组合。

list 更改(C# DLL)

  <file name="Example.Vb6RegFreeCom.dll" hashalg="SHA1">
    <typelib tlbid="{FABD4158-AFDB-4223-BB09-AB8B45E3816E}" version="1.0" 
             flags="hasdiskimage" helpdir="" />
  </file>

  <comInterfaceExternalProxyStub name="IExampleClassEvents"
    iid="{2669EBDB-16D9-45C8-B0A3-ED2CEE26862C}"
    tlbid="{FABD4158-AFDB-4223-BB09-AB8B45E3816E}"
    proxyStubClsid32="{00020420-0000-0000-C000-000000000046}">
  </comInterfaceExternalProxyStub>
  <comInterfaceExternalProxyStub name="IExampleClass"
    iid="{467EB602-B7C4-4752-824A-B1BC164C7962}"
    tlbid="{FABD4158-AFDB-4223-BB09-AB8B45E3816E}"
    proxyStubClsid32="{00020420-0000-0000-C000-000000000046}">
  </comInterfaceExternalProxyStub>

走上正轨后,我发现了几个指向正确方向的指针。我遇到的最好的描述如下。在我的示例中还使用了 IDispatch。

Excerpt from "Registration-Free Activation of COM Components: A Walkthrough" http://msdn.microsoft.com/en-us/library/ms973913.aspx

These elements provide information that would otherwise be present in the registry. The comInterfaceExternalProxyStub element provides enough information for type library marshalling to occur and it is appropriate for COM interfaces that derive from IDispatch (which includes all Automation interfaces). In these cases ole32.dll provides the external proxy-stub used (i.e., external to the files in the assembly). If your COM components implement only dispatch or dual interfaces then this is the element you should use.

关于c# - Regfree COM 事件从其他线程失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23341197/

有关c# - Regfree COM 事件从其他线程失败的更多相关文章

  1. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  2. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  3. ruby - RuntimeError(自动加载常量 Apps 多线程时检测到循环依赖 - 2

    我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("

  4. ruby - 即使失败也继续进行多主机测试 - 2

    我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r

  5. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  6. C# 到 Ruby sha1 base64 编码 - 2

    我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha

  7. ruby - 调用其他方法的 TDD 方法的正确方法 - 2

    我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent

  8. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  9. ruby-on-rails - 事件管理员日期过滤器日期格式自定义 - 2

    是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s

  10. 基于C#实现简易绘图工具【100010177】 - 2

    C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.

随机推荐