Windows 7中的VB6的.NET COM问题:事件不起作用

时间:2013-06-11 16:54:24

标签: .net windows-7 com vb6-migration

我有一个.NET COM DLL,由Visual Basic 6使用。但是,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事件。

.NET

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

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

1 个答案:

答案 0 :(得分:1)

最后问题得到了解决。我只是在Windows 7机器上注册了dll,而不是tlb。

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