Jason Ching
Jason Ching

Reputation: 2239

.NET COM Issues with VB6 in Windows 7: Event doesn't work

I have a .NET COM DLL that is used by Visual Basic 6. However, the CloseEvent is not working in Windows 7 and the following exception is throw. The VB6 process has no problem to invoke the Init method. Only CloseEvent is not working. Both Init and CloseEvent are working fine in my 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[] )...).

Here is the code as follow. This COM object has a Init method and a CloseEvent event.

.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();
    }
}

In VB6, I am using WithEvents to hook up this event:

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

Hope someone can help. Thanks!

Upvotes: 1

Views: 987

Answers (2)

yoel halb
yoel halb

Reputation: 12711

This appears to be a result of the fact that in general COM registration only the CoClass is registered and the ComSourceInterfaces interface.

This will only happen on some machines while perfectly working on other machines.

The solution that works so far is to register the TLB (either by using regasm with the /tlb argument or in code by using the Win32 functions LoadTypeLibEx and then RegisterTypeLib or RegisterTypeLibForUser)

Upvotes: 0

Jason Ching
Jason Ching

Reputation: 2239

Finally the problem is resolved. I was just registering the dll, but not the tlb on the Windows 7 machine.

I am using WIX to create the package. I have now added both dll and tlb fragments to the wxs script (generated by heat) to resolve this.

Upvotes: 3

Related Questions