/* * Created by SharpDevelop. * User: USECRETARIAT3 * Date: 1/04/2009 * Time: 13:53 * * To change this template use Tools | Options | Coding | Edit Standard Headers. */ using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; namespace Antalys { /// /// Description of MircMSG. /// public class MircMSG : IDisposable { IntPtr FileMapHandle = IntPtr.Zero; IntPtr FileMap = IntPtr.Zero; const int WM_USER = 0x400; const int WM_MCOMMAND = WM_USER+200; const int WM_MEVALUATE = WM_USER+201; [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern IntPtr CreateFileMapping( IntPtr hFile, IntPtr lpFileMappingAttributes, FileMapProtection flProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, [MarshalAs(UnmanagedType.LPTStr)] string lpName); [Flags] public enum FileMapProtection : uint { PageReadonly = 0x02, PageReadWrite = 0x04, PageWriteCopy = 0x08, PageExecuteRead = 0x20, PageExecuteReadWrite = 0x40, SectionCommit = 0x8000000, SectionImage = 0x1000000, SectionNoCache = 0x10000000, SectionReserve = 0x4000000, } [DllImport("kernel32.dll", SetLastError = true)] static extern IntPtr MapViewOfFile( IntPtr hFileMappingObject, FileMapAccess dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap); [Flags] public enum FileMapAccess : uint { FileMapCopy = 0x0001, FileMapWrite = 0x0002, FileMapRead = 0x0004, FileMapAllAccess = 0x001f, fileMapExecute = 0x0020, } [DllImport("kernel32.dll", SetLastError=true)] static extern bool UnmapViewOfFile(IntPtr lpBaseAddress); [DllImport("kernel32.dll", SetLastError=true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool CloseHandle(IntPtr hObject); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] static extern bool SendMessage(IntPtr hWnd, Int32 Msg, int wParam, int lParam); public IntPtr GetMircHandle(){ Process p = Process.GetProcessesByName("mirc")[0]; return p.Handle; } public MircMSG(){ FileMapHandle = CreateFileMapping(new IntPtr(-1),IntPtr.Zero,FileMapProtection.PageReadWrite,0,1024,"mirc"); FileMap = MapViewOfFile(FileMapHandle, FileMapAccess.FileMapWrite,0,0,1024); } public void Dispose(){ if (!UnmapViewOfFile(FileMap)) throw new Exception(Marshal.GetLastWin32Error().ToString()); if (!CloseHandle(FileMapHandle)) throw new Exception(Marshal.GetLastWin32Error().ToString()); } public bool SendMircMessage(string command){ Marshal.Copy(command.ToCharArray(),0,FileMap,command.Length); return SendMessage(GetMircHandle(),WM_MCOMMAND,1,0); } } }