Possible to use COM IMessageFilter interface from VB?

Discussion in 'Microsoft VB .NET' started by Whitney Kew, Feb 6, 2004.

  1. Whitney Kew

    Whitney Kew Guest

    Hi there,

    I want to implement the COM interface IMessageFilter from my VB.NET
    application. I realize that it's inside ole32.dll, and that ole32.dll
    doesn't have a type library inside of it, so I'm unsure if this is
    even possible. I'm pretty green to VB.NET, and .NET in general, so
    would anyone be able to give me a little push? Specifically, I think
    what I want to do is to somehow "import" ole32.dll into my VB app, but
    I'm unsure of how to do this.

    My basic problem is that I'm automating Visual Studio .NET 2003 with
    the EnvDTE interface, and I'm occasionally getting an exception ("Call
    was rejected by callee") when I run builds with devenv.exe from the
    command line. I saw the following Google article...

    http://www.google.com/groups?q=call...=&rnum=6

    that pointed out that I need to implement an IMessageFilter, but the
    code it points to is in C#. Does anyone know how to do this in VB?

    Thanks so much in advance!
    Whitney Kew
    Software Engineer
    Rain Bird Corporation
     
    Whitney Kew, Feb 6, 2004
    #1
    1. Advertisements

  2. Whitney Kew

    Mitrix42

    Joined:
    Jun 20, 2007
    Messages:
    1
    Likes Received:
    0
    I am working with MSAccess and MS Project and have adapted my code from this example. And it works :dance:. Note all .NET languages are translatable* you can use http://www.developerfusion.com/tools/convert/csharp-to-vb/ or a number of others to adjust syntax.

    * (I believe some C++ functionality is not translatable to VB.NET)

    Code:
    Imports Autodesk.AutoCAD.Interop
    Imports System.Windows.Forms
    Imports System.Runtime.InteropServices
    Imports System.Reflection
    Imports LoadableComponent
    
    ' For more information on IMessageFilter:
    ' http://msdn.microsoft.com/en-us/library/ms693740(VS.85).aspx
    
    Namespace DrivingAutoCAD
        <ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000016-0000-0000-C000-000000000046")> _
        Public Interface IMessageFilter
            <PreserveSig> _
            Function HandleInComingCall(dwCallType As Integer, hTaskCaller As IntPtr, dwTickCount As Integer, lpInterfaceInfo As IntPtr) As Integer
            <PreserveSig> _
            Function RetryRejectedCall(hTaskCallee As IntPtr, dwTickCount As Integer, dwRejectType As Integer) As Integer
            <PreserveSig> _
            Function MessagePending(hTaskCallee As IntPtr, dwTickCount As Integer, dwPendingType As Integer) As Integer
        End Interface
    
        Public Partial Class Form1
            Inherits Form
            Implements IMessageFilter
            <DllImport("ole32.dll")> _
            Private Shared Function CoRegisterMessageFilter(lpMessageFilter As IMessageFilter, ByRef lplpMessageFilter As IMessageFilter) As Integer
            End Function
    
            Public Sub New()
                InitializeComponent()
                Dim oldFilter As IMessageFilter
                CoRegisterMessageFilter(Me, oldFilter)
            End Sub
    
            Private Sub button1_Click(sender As Object, e As EventArgs)
                Const  progID As String = "AutoCAD.Application.18"
    
                Dim acApp As AcadApplication = Nothing
                Try
                    acApp = DirectCast(Marshal.GetActiveObject(progID), AcadApplication)
                Catch
                    Try
                        Dim acType As Type = Type.GetTypeFromProgID(progID)
                        acApp = DirectCast(Activator.CreateInstance(acType, True), AcadApplication)
                    Catch
                        MessageBox.Show("Cannot create object of type """ & progID & """")
                    End Try
                End Try
                If acApp IsNot Nothing Then
                    Try
                        ' By the time this is reached AutoCAD is fully
                        ' functional and can be interacted with through code
    
                        acApp.Visible = True
    
                        Dim app As INumberAddition = DirectCast(acApp.GetInterfaceObject("LoadableComponent.Commands"), INumberAddition)
    
                        ' Now let's call our method
    
                        Dim res As String = app.AddNumbers(5, 6.3)
    
                        acApp.ZoomAll()
    
                        MessageBox.Show(Me, "AddNumbers returned: " & res)
                    Catch ex As Exception
                        MessageBox.Show(Me, "Problem executing component: " & ex.Message)
                    End Try
                End If
            End Sub
            #Region "IMessageFilter Members"
    
            Private Function IMessageFilter_HandleInComingCall(dwCallType As Integer, hTaskCaller As IntPtr, dwTickCount As Integer, lpInterfaceInfo As IntPtr) As Integer Implements IMessageFilter.HandleInComingCall
                Return 0
                ' SERVERCALL_ISHANDLED
            End Function
    
            Private Function IMessageFilter_RetryRejectedCall(hTaskCallee As IntPtr, dwTickCount As Integer, dwRejectType As Integer) As Integer Implements IMessageFilter.RetryRejectedCall
                Return 1000
                ' Retry in a second
            End Function
    
            Private Function IMessageFilter_MessagePending(hTaskCallee As IntPtr, dwTickCount As Integer, dwPendingType As Integer) As Integer Implements IMessageFilter.MessagePending
                Return 1
                ' PENDINGMSG_WAITNOPROCESS
            End Function
    
            #End Region
        End Class
    End Namespace
    
     
    Last edited: Feb 22, 2012
    Mitrix42, Feb 22, 2012
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Morse
    Replies:
    10
    Views:
    1,069
    Chris Morse
    Jan 16, 2004
  2. Neil Stevens

    IMessageFilter question

    Neil Stevens, Jun 23, 2004, in forum: Microsoft VB .NET
    Replies:
    1
    Views:
    257
    Claes Bergefall
    Jun 24, 2004
  3. learner
    Replies:
    0
    Views:
    211
    learner
    Oct 3, 2005
  4. Guest

    How To Implement IMessageFilter

    Guest, Nov 23, 2005, in forum: Microsoft VB .NET
    Replies:
    4
    Views:
    469
    Guest
    Nov 24, 2005
  5. Rob Perkins
    Replies:
    3
    Views:
    201
    Rob Perkins
    Mar 22, 2006
Loading...

Share This Page