protected override void WndProc(ref Message m) { switch (m.Msg) { case Win32.WM_COPYDATA: Win32.CopyDataStruct st = (Win32.CopyDataStruct)Marshal.PtrToStructure(m.LParam, typeof(Win32.CopyDataStruct)); string strData = Marshal.PtrToStringUni(st.lpData); OpenFile(strData); Activate(); break; default: // let the base class deal with it base.WndProc(ref m); break; } }
static void Main() { if (mutex.WaitOne(TimeSpan.Zero, true)) { #region standaard Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); #endregion MainForm frm = new MainForm(); MainWindowHandle = (int)frm.Handle; Application.Run(frm); MainWindowHandle = 0; mutex.ReleaseMutex(); } else { int hwnd = 0; while (hwnd == 0) { Thread.Sleep(5); hwnd = MainWindowHandle; } Win32.CopyDataStruct cds = new Win32.CopyDataStruct(); try { string data = GetFileName(); cds.cbData = (data.Length + 1) * 2; // number of bytes cds.lpData = Win32.LocalAlloc(0x40, cds.cbData); // known local-pointer in RAM Marshal.Copy(data.ToCharArray(), 0, cds.lpData, data.Length); // Copy data to preserved local-pointer cds.dwData = (IntPtr)1; Win32.SendMessage((IntPtr)hwnd, Win32.WM_COPYDATA, IntPtr.Zero, ref cds); } finally { cds.Dispose(); } } }