Click here to Skip to main content
15,898,222 members
Articles / Programming Languages / Visual Basic

INTER PROCESS COMMUNICATION USING REGISTERMESSAGE and POSTMESSAGE

Rate me:
Please Sign up or sign in to vote.
3.35/5 (11 votes)
22 Jun 2006 36.5K   1.3K   12  
IPC using custom windows messanging by win32 api
Public Class clsIPC
    Inherits System.Windows.Forms.NativeWindow
    Implements IDisposable

    Private Const GLOBAL_IDENTITY_ONE = "GLOBAL_IDENTITY_ONE"
    Private Const GLOBAL_IDENTITY_TWO = "GLOBAL_IDENTITY_TWO"

    Private Const MESSAGE_FOR_ONE = "MESSAGE_ONE"
    Private Const MESSAGE_FOR_TWO = "MESSAGE_TWO"


#Region "API Declaration"

    'API function to create custom system-wide Messages
    Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" _
        (ByVal lpString As String) As IntPtr

    'API function to find window based on WindowName and class
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
        (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr

    'API function to send async. message to target application
    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
        (ByVal hwnd As IntPtr, ByVal wMsg As IntPtr, ByVal wParam As Integer, _
        ByVal lParam As Integer) As IntPtr

#End Region

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case TWO_TO_ONE_MESSAGEID().ToInt32
                MessageBox.Show("GOT A MESSAGE FROM ONE--- CALLING METHOD A", "VBNETMessaging")
                Call A()
        End Select
        MyBase.WndProc(m)
    End Sub

    Private Sub A()
        MsgBox("Message from Method A")
    End Sub
    Public ReadOnly Property TWO_TO_ONE_MESSAGEID() As IntPtr
        Get
            Static objTWO_TO_ONE_MESSAGEID As IntPtr

            If IntPtr.Zero.Equals(objTWO_TO_ONE_MESSAGEID) Then
                objTWO_TO_ONE_MESSAGEID = RegisterWindowMessage(MESSAGE_FOR_TWO)
            End If
            Return objTWO_TO_ONE_MESSAGEID
        End Get
    End Property


#Region "dispose method and new()/constructor"

    Public Sub Dispose() Implements IDisposable.Dispose
        If Not Me.Handle.Equals(IntPtr.Zero) Then
            Me.ReleaseHandle()
        End If
    End Sub

    Public Sub New()
        '''''Creating Window based on globally known name, and create handle
        '''''so we can start listening to Window Messages.
        Dim Params As CreateParams = New CreateParams
        Params.Caption = GLOBAL_IDENTITY_TWO
        Me.CreateHandle(Params)
    End Sub
#End Region

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
India India
Kedar Vaijanapurkar
theghost_k8@yahoo.com

Comments and Discussions