using System; using System.Runtime.InteropServices; class SendToIM { private const int WM_COPYDATA = 0x004A; private const int MAGIC_MUMBER = 1351; [StructLayout(LayoutKind.Sequential)] private struct COPYDATASTRUCT { public int dwData; public int cbData; public IntPtr lpData; } [DllImport("user32.dll")] private extern static IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] private extern static int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, ref COPYDATASTRUCT lParam); public static void sendToIM(string applicationName, string title, string artist, int play1Stop0) { IntPtr im = FindWindow("MsnMsgrUIManager", null); if(im == IntPtr.Zero) return; string sendData = applicationName + @"\0Music\0" + play1Stop0.ToString() + @"\0{0} - {1}\0" + title + @"\0" + artist + @"\0\0"; COPYDATASTRUCT cds; cds.dwData = MAGIC_MUMBER; cds.cbData = (sendData.Length + 1) * 2; cds.lpData = Marshal.StringToHGlobalUni(sendData); try { SendMessage(im, WM_COPYDATA, IntPtr.Zero, ref cds); } finally { Marshal.FreeHGlobal(cds.lpData); } } }