1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace Antalys
{
/// <summary>
/// Description of MircMSG.
/// </summary>
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);
}
}
} |