Ejemplo n.º 1
0
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == NativeMethods.WM_COPYDATA)
            {
                // Extract the file name
                NativeMethods.COPYDATASTRUCT copyData = (NativeMethods.COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(NativeMethods.COPYDATASTRUCT));
                int dataType = (int)copyData.dwData;
                if (dataType == 2)
                {
                    string fileName = Marshal.PtrToStringAnsi(copyData.lpData);

                    // Add the file name to the edit box
                    richTextBox1.AppendText(fileName);
                    richTextBox1.AppendText("\r\n");
                }
                else
                {
                    MessageBox.Show(String.Format("Unrecognized data type = {0}.", dataType), "SendMessageDemo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                base.WndProc(ref m);
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // Get the filename if it exists
            string fileName = "test 2";

            if (args.Length == 1)
            {
                fileName = args[0];
            }

            // If a mutex with the name below already exists,
            // one instance of the application is already running
            bool  isNewInstance;
            Mutex singleMutex = new Mutex(true, "MyAppMutex", out isNewInstance);

            if (isNewInstance)
            {
                // Start the form with the file name as a parameter
                Form1 frm = new Form1(fileName);
                Application.Run(frm);
            }
            else
            {
                string windowTitle = "SystemTrayApp";
                windowTitle = "SendMessage Demo";
                // Find the window with the name of the main form
                IntPtr ptrWnd = NativeMethods.FindWindow(null, windowTitle);
                if (ptrWnd == IntPtr.Zero)
                {
                    MessageBox.Show(String.Format("No window found with the title '{0}'.", windowTitle), "Program", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                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 = fileName.Length + 1; // One extra byte for the \0 character
                        copyData.lpData = Marshal.StringToHGlobalAnsi(fileName);

                        // 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(), "SendMessage Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    finally
                    {
                        // Free the allocated memory after the contol has been returned
                        if (ptrCopyData != IntPtr.Zero)
                        {
                            Marshal.FreeCoTaskMem(ptrCopyData);
                        }
                        singleMutex.ReleaseMutex();
                    }
                }
            }
        }