Exemplo 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 argsString = Marshal.PtrToStringAnsi(copyData.lpData);

                    string[] args = JsonConvert.DeserializeObject <string[]>(argsString);
                    if (args.Length > 0)
                    {
                        ProcessArguments(args);
                    }
                }
                else
                {
                    MessageBox.Show(String.Format("Unrecognized data type = {0}.",
                                                  dataType), "CmdQueue Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                base.WndProc(ref m);
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            PassedArguments = args;

            bool  isNewInstance;
            Mutex mutex = new Mutex(true, "com.gazchap.cmdqueue", out isNewInstance);

            if (isNewInstance)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new frmMain());

                mutex.ReleaseMutex();
            }
            else
            {
                string windowTitle = "CmdQueue";

                // 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),
                                    "CmdQueue Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    IntPtr ptrCopyData = IntPtr.Zero;
                    try {
                        string argsString = JsonConvert.SerializeObject(args);

                        // 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 = argsString.Length + 1; // One extra byte for the \0 character
                        copyData.lpData = Marshal.StringToHGlobalAnsi(argsString);

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