Bloqueada Failed to get call back from .Net to VB 6.0

  • miércoles, 11 de julio de 2012 23:30
     
     

    Hi

    I have main application written in Visual Basic 6.0, and the sub module in Visual Studio 2008(C#.NET). When I tried to call .NET form from VB 6.0 it works fine, and when I tried to raise an event in VB 6.0 from .NET then it just executes but then event doesn't raise. And it don't throw any exception too.

    I am completely frustrated with it from a week, kindly anyone can help me on this.

    Thanks in advance

    Rajesh

Todas las respuestas

  • viernes, 13 de julio de 2012 5:29
    Moderador
     
     

    Hi Rajesh,

    Welcome to the MSDN forum.

    I would refer to this reply by Riquel_Dong from http://social.msdn.microsoft.com/Forums/uk/vbinterop/thread/dac51bc1-ad48-48af-9342-93b8781feab5 . Basically, to fire an event from .NET,you can let VB.NET expose one public method, when we call this method from VB6. It will raises one VB.NET event. We need to add event handler in VB6 to handle this event.

    Please have a look at the following code snippet. I refer to book <<COM and .NET Interoperability>> in Page 609.

    VB.NET class library VbDotNetEventSender:
    Imports System.Runtime.InteropServices
    ' This is the name of the event interface to be generated.
    <InterfaceType(ComInterfaceType.InterfaceIsIDispatch), _
    Guid("D6BF881E-8548-4140-8569-583960F231DB")> _
    Public Interface _DEventInterface
        <DispId(1)> Sub TheEvent(ByVal msg As String)
    End Interface

    <ClassInterface(ClassInterfaceType.AutoDual), _
    Guid("BFA2B6BD-8FEA-47a2-B91A-5B77C7197293"), _
    ComSourceInterfaces(GetType(_DEventInterface))> _
    Public Class VbDotNetEventSender
        Public Event TheEvent(ByVal msg As String)
        Public Sub FireTheEvent()
            RaiseEvent TheEvent("Hello from the DotNetEventSender")
        End Sub
    End Class

    VB6 code snippet:

    Option Explicit
    Public WithEvents eventObj As VbDotNetEventSender.VbDotNetEventSender
    Private Sub Command1_Click()
    eventObj.FireTheEvent
    End Sub
    Private Sub eventObj_TheEvent(ByVal msg As String)
    MsgBox msg, , "Message from event object"
    End Sub
    Private Sub Form_Load()
    Set eventObj = New VbDotNetEventSender.VbDotNetEventSender
    End Sub

    I hope this will be helpful.

    Best regards,


    Shanks Zen
    MSDN Community Support | Feedback to us

  • viernes, 13 de julio de 2012 21:23
     
     

    Hi Shanks

    First and foremost thank you so much for your precious time. As far as coding is concerned everything looks just fine.

    But as I observed ApplicationEventRaisedEvent is returning null, is that fine or that might be causing the problem?

    If yes what would be the possible reason could you please tell me.

    Thanks

    Rajesh


    Rajesh

  • miércoles, 18 de julio de 2012 20:14
     
     Respondida
    Yes, that was causing problem. I was getting ApplicationEventRaisedEvent as null because in two of the place it was getting Initialized in the application.

    Rajesh