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
CloseHandle(intPtrFileHandle)
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))
Next
UnmapViewOfFile(intPtrMappingAddress)
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
objMemoryMappedFile.Open("mIRC")
objMemoryMappedFile.WriteString(Command)
SendMessage(Handle, WM_MCOMMAND, 1, 0)
objMemoryMappedFile.Close()
End Function
End Class
End Namespace