1

我有一个 Visual Basic 6 使用的 .NET COM DLL。但是,CloseEvent 在 Windows 7 中不起作用,并且抛出了以下异常。VB6进程调用Init方法没有问题。只有 CloseEvent 不起作用。Init 和 CloseEvent 在我的 XP 中都可以正常工作。

System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: Object does not match target type. (Fault Detail is equal to An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
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 FMStation.VbComGateway.IVbComEventGateway.CloseEvent()
   at FMStation.VbComGateway.VbComGateway.TriggerCloseEvent()
   at FMStation.VbComGateway.VbComGateway.<.ctor>b__0(Object o, EventArgs e)
   at FMStation.VbComGateway.VbService.CloseApplication()
   at SyncInvokeCloseApplication(Object , Object[] , Object[] )...).

这是如下代码。这个 COM 对象有一个 Init 方法和一个 CloseEvent 事件。

。网

public interface IVbComGateway
{
    void Init(string namedPipieId);
}

[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[Guid("EA9C2EFC-7A13-4944-9901-29263F4F4B32")]
[ComVisible(true)]
public interface IVbComEventGateway
{
    [DispId(1)]
    void CloseEvent();
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(IVbComEventGateway))]  //Our event source is IMathEvents interface
[ComDefaultInterface(typeof(IVbComGateway))]
public class VbComGateway : IVbComGateway
{
    [ComVisible(false)]
    public delegate void MyEventHandler();

    private readonly VbService vbService;
    private ServiceHost host;

    public event MyEventHandler CloseEvent;

    public VbComGateway()
    {
        vbService = new VbService();
        vbService.ClosingApplicationSignalReceived += (o, e) => TriggerCloseEvent();
    }

    public void Init(string namedPipieId)
    {
        host = new ServiceHost(vbService, new[] { new Uri("net.pipe://localhost/" + namedPipieId) });

        host.AddServiceEndpoint(typeof(IVbService), new NetNamedPipeBinding(), "PipeReverse");

        host.Open();
    }

    private void TriggerCloseEvent()
    {
        if (CloseEvent != null)
            CloseEvent();
    }
}

VB6中,我使用 WithEvents 来连接此事件:

Dim WithEvents gateway As FmsVbComGateway.VbComGateway

Private Sub gateway_CloseEvent()

    CloseApplication

    Dim number As Integer
    For number = 0 To VB.Forms.Count - 1
        Unload VB.Forms(number)
    Next number
End Sub

希望有人可以提供帮助。谢谢!

4

2 回答 2

3

终于问题解决了。我只是在注册 dll,而不是在 Windows 7 机器上注册 tlb。

我正在使用 WIX 创建包。我现在已将 dll 和 tlb 片段添加到 wxs 脚本(由热生成)以解决此问题。

于 2013-06-12T17:05:42.393 回答
0

这似乎是因为在一般 COM 注册中只CoClass注册了接口和ComSourceInterfaces接口。

这只会在某些机器上发生,而在其他机器上完美运行。

到目前为止有效的解决方案是注册 TLB(通过使用regasm参数/tlb或在代码中使用 Win32 函数LoadTypeLibEx然后RegisterTypeLibRegisterTypeLibForUser

于 2021-10-11T23:14:50.340 回答