mIRC Homepage

VB .NET SendMessage .. Updated Code

Posted By: bradtm

VB .NET SendMessage .. Updated Code - 07/02/05 10:21 PM

With argv0's C code I have converted a lot of the code to VB .NET. I am getting 0 returned by SendMessage though and can't find a solution to the problem. Anyone have any insight?

The New Win32Exception(Marshal.GetLastWin32Error()).Message I get after I execute SendMessage is: "The specified procedure could not be found"

I am getting a return of 0 from SendMessage and can't figure out why. How can I find out why it's getting an error or does anyone know what's wrong?

    Private Const WM_MCOMMAND As Integer = &H102 + 200
    Private Const FILE_MAP_ALL_ACCESS As Integer = &H1 Or &H2 Or &H4 Or &H8 Or &H10 Or &HF0000
    Private Const PAGE_READWRITE As Integer = &H4
    Private Const INVALID_HANDLE_VALUE As Integer = -1

    Declare Auto Function SendMessage Lib "user32" ( _
        ByVal hWnd As IntPtr, _
        ByVal Msg As Integer, _
        ByVal wParam As Integer, _
        ByVal lParam As Integer) As Integer

    Private Declare Auto Function CreateFileMapping Lib "kernel32" ( _
        ByVal hFile As Integer, _
        ByVal lpFileMappingAttributes As Integer, _
        ByVal flProtect As Integer, _
        ByVal dwMaximumSizeHigh As Integer, _
        ByVal dwMaximumSizeLow As Integer, _
        ByVal lpName As String) As IntPtr

    Declare Function MapViewOfFile Lib "kernel32" Alias "MapViewOfFile" ( _
        ByVal hFileMappingObject As IntPtr, _
        ByVal dwDesiredAccess As Integer, _
        ByVal dwFileOffsetHigh As Integer, _
        ByVal dwFileOffsetLow As Integer, _
        ByVal dwNumberOfBytesToMap As UIntPtr) As IntPtr

    Private Declare Auto Function wsprintf Lib "user32" ( _
        ByVal lpOut As IntPtr, _
        ByVal lpFmt As String) As Integer

    Private Declare Auto Function UnmapViewOfFile Lib "kernel32" ( _
        ByVal lpBaseAddress As IntPtr) As Boolean

    Public Declare Auto Function CloseHandle Lib "kernel32.dll" ( _
        ByVal hObject As IntPtr) As Boolean

    Function SendCommand(ByVal hwnd As IntPtr, ByVal command As String) As Boolean
        Dim hMapFile As IntPtr
        Dim mData As IntPtr
        Dim NumberOfBytes As UIntPtr

        Dim intWsprintf As Integer
        Dim intSendmessage As Integer

        ' hwnd = 459282
        ' command = "/msg XBN`grind test"

        ' Returns: 1684
        hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, 4096, "mIRC")

        ' Returns: 72548352
        mData = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, NumberOfBytes)

        ' Returns: 18
        intWsprintf = wsprintf(mData, command)

        ' Returns: 0
        intSendmessage = (SendMessage(hwnd, WM_MCOMMAND, 1, 0))

        ' Returns: False

        ' Returns: False

        Return True
    End Function
Posted By: bradtm

Re: VB .NET SendMessage .. Updated Code - 08/02/05 07:20 PM

Ok, I got SendMessage to return 1, and all the functions behave properly. Am I supposed to SendMessage() to the mIRC_Channel class or the Edit class of the mIRC_Channel class?

I assume the mIRC_Channel class since SendMessage returns 1. But the message I send for example, /server irc.enterthegame.com does not show up as being entered in mIRC.
Posted By: trbz

Re: VB .NET SendMessage .. Updated Code - 06/03/05 11:15 PM

You need to SendMessage to mIRC's main window. Get the handle to it by searching for the window with the API FindWindow(.,.).
Posted By: trbz

Re: VB .NET SendMessage .. Updated Code - 09/03/05 09:15 PM

Hello again. Since so many have asked for a working VB.NET sendmessage example I've decided to write one. Here you go.

Put the code in a class file.
Imports System
Imports System.Runtime.InteropServices
Imports System.Text
Namespace mIRC
    Public Class MemoryMappedFile
#Region " Declarations "
        <DllImport("Kernel32")> _
        Private Shared Function CloseHandle( _
            ByVal intPtrFileHandle As IntPtr) As Boolean
        End Function
        <DllImport("Kernel32", EntryPoint:="CreateFileMappingA")> _
        Private Shared Function CreateFileMapping( _
            ByVal hFile As IntPtr, _
            ByRef lpFileMappigAttributes As SECURITY_ATTRIBUTES, _
            ByVal flProtect As Int32, _
            ByVal dwMaximumSizeHigh As Int32, _
            ByVal dwMaximumSizeLow As Int32, _
            ByVal lpname As String) As IntPtr
        End Function
        <DllImport("Kernel32")> _
        Private Shared Function MapViewOfFile( _
            ByVal hFileMappingObject As IntPtr, _
            ByVal dwDesiredAccess As Int32, _
            ByVal dwFileOffsetHigh As Int32, _
            ByVal dwFileOffsetLow As Int32, _
            ByVal dwNumberOfBytesToMap As Int32) As IntPtr
        End Function
        <DllImport("Kernel32")> _
        Private Shared Function UnmapViewOfFile( _
            ByVal lpBaseAddress As IntPtr) As Int32
        End Function
        Private Const PAGE_READWRITE = 4
        Private Const FILE_MAP_ALL_ACCESS As Integer = &H1 Or &H2 Or &H4 Or &H8 Or &H10 Or &HF0000
        Private Structure SECURITY_ATTRIBUTES
            Const nLength As Int32 = 12
            Public lpSecurityDescriptor As Int32
            Public bInheritHandle As Int32
        End Structure
        Private intPtrFileHandle As New IntPtr(0)
#End Region
        Public Sub Open(ByVal Filename As String)
            If intPtrFileHandle.ToInt32 <> 0 Then
                MessageBox.Show("The object is already assigned a file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If
            Dim Security As New SECURITY_ATTRIBUTES
            intPtrFileHandle = CreateFileMapping(New IntPtr(-1), Security, PAGE_READWRITE, 0, 1024, Filename)
        End Sub
        Public Sub Close()
            If intPtrFileHandle.ToInt32 <> 0 Then
                intPtrFileHandle = New IntPtr(0)
            End If
        End Sub
        Public Sub WriteString(ByVal Value As String)
            Dim intPtrMappingAddress As New IntPtr(0)
            Dim intLoop As Integer = 0
            If intPtrFileHandle.ToInt32 = 0 Then
                MessageBox.Show("No file to write to", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If
            intPtrMappingAddress = MapViewOfFile(intPtrFileHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0)
            If intPtrFileHandle.ToInt32 = 0 Then
                MessageBox.Show("Cannot write to the file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If
            Dim ByteArray() As Byte = Encoding.Default.GetBytes(Value & ControlChars.NullChar)
            For intLoop = 0 To ByteArray.Length - 1
                Marshal.WriteByte(intPtrMappingAddress, intLoop, ByteArray(intLoop))
        End Sub
    End Class
    Public Class Messaging
#Region " Declarations "
        <DllImport("User32.dll")> _
        Private Shared Function SendMessage( _
            ByVal Handle As Int32, _
            ByVal wMsg As Int32, _
            ByVal wParam As Int32, _
            ByVal lParam As Int32) As Int32
        End Function
        Public Enum EvaluationMethods
            Evaluate = 1
            Typed = 2
            TypedWithFloodProtection = 4
        End Enum
        Private Const WM_USER = &H400
        Private Const WM_MCOMMAND = WM_USER + 200
#End Region
        Public Function SendMessage(ByVal Handle As Integer, ByVal Command As String, Optional ByVal Method As EvaluationMethods = EvaluationMethods.Evaluate) As Boolean
            Dim objMemoryMappedFile As New mIRC.MemoryMappedFile
            SendMessage(Handle, WM_MCOMMAND, 1, 0)
        End Function
    End Class
End Namespace

Sample useage:
        Dim mIRCObject As New mIRC.Messaging
        mIRCObject.SendMessage(708, "/echo -a Hello World", mIRC.Messaging.EvaluationMethods.Evaluate)

You need to find mIRC's main window handle and use that or the code will fail (change 708 to a new value). A simple approach to that could be using the API FindWindow(). Look that up in the MSDN.
Posted By: Anyday901

Re: VB .NET SendMessage .. Updated Code - 23/06/17 04:01 PM

I see this is a pretty old post, hope you guys are still around lol. I've been using this code and it works great for sending strings to a mirc message window. I'm trying to figure out if there is a way to send a key using this code? Like just sending the ESCAPE key.

mIRCObject.SendMessage(708, WOULDLIKEESCAPEKEYHERE, mIRC.Messaging.EvaluationMethods.Evaluate)

any help would be great!
© 2020 mIRC Discussion Forums