PC Review

Thread Tools Rating: Thread Rating: 3 votes, 3.67 average.

Possible to use COM IMessageFilter interface from VB?

Whitney Kew
Posts: n/a
      6th Feb 2004
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...


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
Reply With Quote
New Member
Join Date: Jun 2007
Posts: 1
      22nd Feb 2012
I am working with MSAccess and MS Project and have adapted my code from this example. And it works . Note all .NET languages are translatable* you can use http://www.developerfusion.com/tools.../csharp-to-vb/ or a number of others to adjust syntax.

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

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()
            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
                acApp = DirectCast(Marshal.GetActiveObject(progID), AcadApplication)
                    Dim acType As Type = Type.GetTypeFromProgID(progID)
                    acApp = DirectCast(Activator.CreateInstance(acType, True), AcadApplication)
                    MessageBox.Show("Cannot create object of type """ & progID & """")
                End Try
            End Try
            If acApp IsNot Nothing Then
                    ' 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)


                    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
        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
        End Function

        #End Region
    End Class
End Namespace

Last edited by Mitrix42; 22nd Feb 2012 at 07:50 AM..
Reply With Quote

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
IMEssageFilter =?Utf-8?B?SmFtZXM=?= Microsoft Dot NET Compact Framework 6 20th May 2005 07:53 AM
Need more help with OpenNETCF, IMessageFilter problem vbmark Microsoft Dot NET Compact Framework 7 8th Feb 2005 05:32 PM
What am I doing wrong with IMessageFilter? vbMark Microsoft Dot NET Compact Framework 7 3rd Feb 2005 03:47 PM
IMessageFilter.PreFilterMessage Method Tyler Microsoft Dot NET Framework Forms 1 6th Oct 2003 09:33 AM
MDI - Imessagefilter See Sharp Microsoft C# .NET 1 31st Jul 2003 04:01 PM




All times are GMT +1. The time now is 07:13 PM.