private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == NativeMethods.WM_COPYDATA) { NativeMethods.COPYDATASTRUCT copyData = (NativeMethods.COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(NativeMethods.COPYDATASTRUCT)); int dataType = (int)copyData.dwData; if (dataType == 2) { string filename = Marshal.PtrToStringAnsi(copyData.lpData); // Add the file name to the user control AddEmlFile(filename); } else { MessageBox.Show("Unrecognized data type = " + dataType); } } return(IntPtr.Zero); }
private void Application_Startup(object sender, StartupEventArgs e) { if (e.Args.Length != 1) { MessageBox.Show("File name not specified." + Environment.NewLine + "Usage <executable> <emlfile>"); Environment.Exit(1); } var file = e.Args[0]; if (!File.Exists(file)) { MessageBox.Show("File not found. " + Environment.NewLine + file); Environment.Exit(2); } bool isNewInstance; Mutex singleMutex = new Mutex(true, "MyAppMutex", out isNewInstance); if (isNewInstance) { // Start the form with the file name as a parameter var mainWindow = new MainWindow(file); mainWindow.ShowDialog(); mainWindow.Close(); } else { string windowTitle = "View Emls"; // Find the window with the name of the main form IntPtr ptrWnd = NativeMethods.FindWindow(null, windowTitle); if (ptrWnd == IntPtr.Zero) { MessageBox.Show("No window found with the title " + windowTitle); } else { IntPtr ptrCopyData = IntPtr.Zero; try { // Create the data structure and fill with data NativeMethods.COPYDATASTRUCT copyData = new NativeMethods.COPYDATASTRUCT(); copyData.dwData = new IntPtr(2); // Just a number to identify the data type copyData.cbData = file.Length + 1; // One extra byte for the \0 character copyData.lpData = Marshal.StringToHGlobalAnsi(file); // Allocate memory for the data and copy ptrCopyData = Marshal.AllocCoTaskMem(Marshal.SizeOf(copyData)); Marshal.StructureToPtr(copyData, ptrCopyData, false); // Send the message NativeMethods.SendMessage(ptrWnd, NativeMethods.WM_COPYDATA, IntPtr.Zero, ptrCopyData); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { // Free the allocated memory after the contol has been returned if (ptrCopyData != IntPtr.Zero) { Marshal.FreeCoTaskMem(ptrCopyData); } } } Environment.Exit(1); } }