private void WndProc(ref Message m)
        {
            //System.Diagnostics.Debug.WriteLine(String.Format("{0} - {1} - {2}", m.Msg, m.WParam, m.WParam));

            // close down if we were abandonded
            if (m.Msg == (int)MSG_STOP || m.Msg == (int)Win32.WM_CLOSE || m.Msg == (int)Win32.WM_QUIT)
            {
                Log("Close message received - stopping Growl Tray");
                Stop();
            }
            else if (GROWL && started && !GrowlConnector.IsGrowlRunningLocally())
            {
                Win32.PostMessage(this.hwnd.Handle, MSG_STOP, IntPtr.Zero, IntPtr.Zero);
            }
            else if (m.Msg == (int)Win32.WM_COPYDATA)
            {
                Win32.COPYDATASTRUCT cds = new Win32.COPYDATASTRUCT();
                cds = (Win32.COPYDATASTRUCT)m.GetLParam(typeof(Win32.COPYDATASTRUCT));
                try
                {
                    HandleCopyData(m.HWnd, m.WParam, cds);
                }
                catch (Exception ex)
                {
                    Log(ex.ToString());
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Invokes the default window procedure associated with this window.
        /// </summary>
        /// <param name="m">A <see cref="T:System.Windows.Forms.Message"></see> that is associated with the current Windows message.</param>
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == (int)Win32.WindowsMessage.WM_COPYDATA)
            {
                IrssLog.Info("Received WM_COPYDATA message");

                try
                {
                    Win32.COPYDATASTRUCT dataStructure = (Win32.COPYDATASTRUCT)m.GetLParam(typeof(Win32.COPYDATASTRUCT));

                    if (dataStructure.dwData != CopyDataID)
                    {
                        IrssLog.Warn("WM_COPYDATA ID ({0}) does not match expected ID ({1})", dataStructure.dwData, CopyDataID);
                        return;
                    }

                    byte[] dataBytes = new byte[dataStructure.cbData];
                    Marshal.Copy(dataStructure.lpData, dataBytes, 0, dataStructure.cbData);
                    string strData = Encoding.ASCII.GetString(dataBytes);

                    Program.ProcessCommand(strData, false);
                }
                catch (Exception ex)
                {
                    IrssLog.Error("Error processing WM_COPYDATA message: {0}", ex.ToString());
                }
            }

            base.WndProc(ref m);
        }
Exemplo n.º 3
0
 public static bool OpenExistingAppCallback(IntPtr hWnd, IntPtr lParam)
 {
     if (Win32.GetProp(hWnd, Game.programGuid) != IntPtr.Zero)
     {
         Win32.SetForegroundWindow(hWnd);
         IntPtr intPtr      = IntPtr.Zero;
         int    cbData      = 0;
         string mapArgument = EngineUtils.GetMapArgument();
         if (mapArgument != null)
         {
             intPtr = Marshal.StringToCoTaskMemUni(mapArgument);
             cbData = (mapArgument.Length + 1) * 2;
         }
         if (intPtr != IntPtr.Zero)
         {
             Win32.COPYDATASTRUCT cOPYDATASTRUCT = default(Win32.COPYDATASTRUCT);
             cOPYDATASTRUCT.dwData = IntPtr.Zero;
             cOPYDATASTRUCT.lpData = intPtr;
             cOPYDATASTRUCT.cbData = cbData;
             Win32.SendMessage(hWnd, 74, 0, ref cOPYDATASTRUCT);
         }
         Marshal.FreeCoTaskMem(intPtr);
         return(false);
     }
     return(true);
 }
Exemplo n.º 4
0
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == Win32.WM_COPYDATA)
            {
                Win32.COPYDATASTRUCT data = Marshal.PtrToStructure <Win32.COPYDATASTRUCT>(lParam);

                string str = Marshal.PtrToStringUni(data.lpData);

                WriteToMessages(str);
            }
            return(IntPtr.Zero);
        }
Exemplo n.º 5
0
        public static bool SendStringToWindow(string windowTitle, string data)
        {
            //wait & retry while initial window is spinning up
            var retries = retryMaxSeconds * 1000 / sleepMilliseconds;

            IntPtr ptrWnd = IntPtr.Zero;

            while ((ptrWnd = Win32.FindWindow(null, windowTitle)) == IntPtr.Zero && --retries > 0)
            {
                System.Threading.Thread.Sleep(sleepMilliseconds);
            }

            if (retries == 0)
            {
                MessageBox.Show($"Couldn't find window named: {windowTitle}", "SingleInstanceArgAggregator", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }

            var ptrCopyData = IntPtr.Zero;

            try {
                // Create the data structure and fill with data
                Win32.COPYDATASTRUCT copyData = new Win32.COPYDATASTRUCT {
                    dwData = new IntPtr(2),   // Just a number to identify the data type
                    cbData = data.Length + 1, // One extra byte for the \0 character
                    lpData = Marshal.StringToHGlobalAnsi(data)
                };

                // Allocate memory for the data and copy
                ptrCopyData = Marshal.AllocCoTaskMem(Marshal.SizeOf(copyData));
                Marshal.StructureToPtr(copyData, ptrCopyData, false);

                // Send the message
                Win32.SendMessage(ptrWnd, Win32.WM_COPYDATA, IntPtr.Zero, ptrCopyData);
            }
            catch (Exception ex) {
                MessageBox.Show(ex.ToString(), windowTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            finally {
                // Free the allocated memory after the contol has been returned
                if (ptrCopyData != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(ptrCopyData);
                }
            }

            return(true);
        }
Exemplo n.º 6
0
        //send a notification to the already existing instance that a new instance was started
        private bool NotifyPreviousInstance(object message)
        {
            //First, find the window of the previous instance
            IntPtr handle = Win32.FindWindow(null, _id);

            if (handle != IntPtr.Zero)
            {
                //create a GCHandle to hold the serialized object.
                GCHandle bufferHandle = new GCHandle();
                try
                {
                    byte[] buffer;
                    Win32.COPYDATASTRUCT data = new Win32.COPYDATASTRUCT();
                    if (message != null)
                    {
                        //serialize the object into a byte array
                        buffer = Serialize(message);
                        //pin the byte array in memory
                        bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

                        data.dwData = 0;
                        data.cbData = buffer.Length;
                        //get the address of the pinned buffer
                        data.lpData = bufferHandle.AddrOfPinnedObject();
                    }

                    GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
                    try
                    {
                        Win32.SendMessage(handle, Win32.WM_COPYDATA, IntPtr.Zero, dataHandle.AddrOfPinnedObject());
                        Win32.SetForegroundWindow(handle); // Give focus to first instance
                        return(true);
                    }
                    finally
                    {
                        dataHandle.Free();
                    }
                }
                finally
                {
                    if (bufferHandle.IsAllocated)
                    {
                        bufferHandle.Free();
                    }
                }
            }
            return(false);
        }
Exemplo n.º 7
0
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == Win32.WM_USER)
            {
                string msg = "";
                msg = (string)m.GetLParam(msg.GetType());
                WriteToMessages(msg);
            }
            else if (m.Msg == Win32.WM_COPYDATA)
            {
                Win32.COPYDATASTRUCT data = (Win32.COPYDATASTRUCT)
                                            m.GetLParam(typeof(Win32.COPYDATASTRUCT));

                string str = Marshal.PtrToStringUni(data.lpData);

                WriteToMessages(str);
            }
            base.WndProc(ref m);
        }
Exemplo n.º 8
0
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            switch (msg)
            {
            case Win32.WM_COPYDATA:
                //try
                //{
                //System.Windows.MessageBox.Show("copydata");
                Win32.COPYDATASTRUCT cd = (Win32.COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(Win32.COPYDATASTRUCT));
                this.PlayFile(cd.lpData);
                handled = true;
                //}
                //catch (Exception ex)
                //{
                //    MessageBox.Show(ex.Message);
                //}
                break;
            }

            return(IntPtr.Zero);
        }
        private unsafe static bool TryToSendOpenFileMessage(IntPtr hwnd, string filename)
        {
            char[] data = filename.ToCharArray();
            char * b    = stackalloc char[data.Length + 1];

            for (int i = 0; i < data.Length; i++)
            {
                b[i] = data[i];
            }
            b[data.Length] = '\0';

            Win32.COPYDATASTRUCT cddata = new Win32.COPYDATASTRUCT();
            cddata.dwData = Win32.PODEROSA_OPEN_FILE_REQUEST;
            cddata.cbData = (uint)(sizeof(char) * (data.Length + 1));
            cddata.lpData = b;

            int lresult = Win32.SendMessage(hwnd, Win32.WM_COPYDATA, IntPtr.Zero, new IntPtr(&cddata));

            Debug.WriteLine("TryToSend " + lresult);
            return(lresult == Win32.PODEROSA_OPEN_FILE_OK);
        }
Exemplo n.º 10
0
 //The window procedure that handles notifications from new application instances
 protected override void WndProc(ref Message m)
 {
     if (m.Msg == Win32.WM_COPYDATA)
     {
         //convert the message LParam to the WM_COPYDATA structure
         Win32.COPYDATASTRUCT data = (Win32.COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(Win32.COPYDATASTRUCT));
         object obj = null;
         if (data.cbData > 0 && data.lpData != IntPtr.Zero)
         {
             //copy the native byte array to a .net byte array
             byte[] buffer = new byte[data.cbData];
             Marshal.Copy(data.lpData, buffer, 0, buffer.Length);
             //deserialize the buffer to a new object
             obj = Deserialize(buffer);
         }
         _theInstance.OnNewInstanceMessage(obj);
     }
     else
     {
         base.WndProc(ref m);
     }
 }
Exemplo n.º 11
0
 protected override void WndProc(ref Message m)
 {
     if (m.Msg == Win32.WM_COPYDATA)
     {
         // Extract the file name
         Win32.COPYDATASTRUCT copyData = (Win32.COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(Win32.COPYDATASTRUCT));
         int dataType = (int)copyData.dwData;
         if (dataType == 2)
         {
             string fileName = Marshal.PtrToStringAnsi(copyData.lpData);
             timer.Interval = TimeoutMillisecs; //this is a timer "reset"
             OnNewValue(fileName);
             //MessageBox.Show($"received: {fileName}", "SingleInstanceArgAggregator", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
         else
         {
             MessageBox.Show(String.Format("Unrecognized data type = {0}.", dataType), "SingleInstanceArgAggregator", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
     else
     {
         base.WndProc(ref m);
     }
 }
Exemplo n.º 12
0
        private unsafe static bool TryToSendOpenFileMessage(IntPtr hwnd, string filename) {
            char[] data = filename.ToCharArray();
            char* b = stackalloc char[data.Length + 1];
            for (int i = 0; i < data.Length; i++)
                b[i] = data[i];
            b[data.Length] = '\0';

            Win32.COPYDATASTRUCT cddata = new Win32.COPYDATASTRUCT();
            cddata.dwData = Win32.PODEROSA_OPEN_FILE_REQUEST;
            cddata.cbData = (uint)(sizeof(char) * (data.Length + 1));
            cddata.lpData = b;

            int lresult = Win32.SendMessage(hwnd, Win32.WM_COPYDATA, IntPtr.Zero, new IntPtr(&cddata));
            Debug.WriteLine("TryToSend " + lresult);
            return lresult == Win32.PODEROSA_OPEN_FILE_OK;
        }
        private void HandleCopyData(IntPtr hWnd, IntPtr WParam, Win32.COPYDATASTRUCT cds)
        {
            string szTitle;
            string szText;

            Win32.BalloonFlags info;
            string             szClass = "NONE";
            string             szIcon  = "";
            uint   handle;
            uint   uID;
            UInt32 szPid;
            uint   CustomBalloonIconHandle;
            Image  image = null;

            CallbackContext callback = null;

            if (isXP)
            {
                Win32.TRAYINFO       ti   = (Win32.TRAYINFO)Marshal.PtrToStructure(cds.lpData, typeof(Win32.TRAYINFO));
                Win32.NOTIFYICONDATA data = ti.nid;
                if (!IsFlagSet((int)ti.nid.Flags, (int)Win32.IconDataMembers.Info))
                {
                    return;
                }
                if (data.szInfo == "")
                {
                    return;                    //ignore dummy notification
                }
                szTitle = data.szInfoTitle;
                szText  = data.szInfo;
                info    = data.dwInfoFlags;
                handle  = data.hWnd;
                uID     = data.uID;
                Win32.GetWindowThreadProcessId((IntPtr)data.hWnd, out szPid);
                CustomBalloonIconHandle = 0;



                // handle custom icons
                if (IsFlagSet((int)ti.nid.dwInfoFlags, (int)Win32.BalloonFlags.USER) && (IntPtr)ti.nid.hIcon != IntPtr.Zero)
                {
                    if ((IntPtr)ti.nid.hIcon != IntPtr.Zero)
                    {
                        System.Drawing.Icon icon = System.Drawing.Icon.FromHandle((IntPtr)ti.nid.hIcon);
                        using (icon)
                        {
                            image = icon.ToBitmap();
                        }
                    }
                }

                // handle callbacks - format: hwnd:msg:wparam:lparam
                if (ti.nid.uCallbackMsg > 0)
                {
                    string hwnd   = handle.ToString();
                    string msg    = ti.nid.uCallbackMsg.ToString();
                    string wparam = null;
                    string lparam = null;
                    if (ti.nid.uTimeoutOrVersion == 4)
                    {
                        wparam = MakeLParam(LoWord(0), HiWord(0)).ToString();
                        lparam = MakeLParam(LoWord(0x405), HiWord(Convert.ToInt32(ti.nid.uID))).ToString();
                    }
                    else
                    {
                        wparam = ti.nid.uID.ToString();
                        lparam = 0x405.ToString();
                    }
                    string d = String.Join(CALLBACK_DATA_SEPARATOR, new string[] { hwnd, msg, wparam, lparam });
                    callback = new CallbackContext(d, "balloonclick");
                }
            }
            else
            {
                Win32.TRAYINFO6       ti   = (Win32.TRAYINFO6)Marshal.PtrToStructure(cds.lpData, typeof(Win32.TRAYINFO6));
                Win32.NOTIFYICONDATA6 data = ti.nid;
                if (!IsFlagSet((int)ti.nid.Flags, (int)Win32.IconDataMembers.Info))
                {
                    return;
                }
                if (data.szInfo == "")
                {
                    return;                    //ignore dummy notification
                }
                szTitle = data.szInfoTitle;
                szText  = data.szInfo;
                info    = data.dwInfoFlags;
                handle  = data.hWnd;
                uID     = data.uID;
                Win32.GetWindowThreadProcessId((IntPtr)data.hWnd, out szPid);
                CustomBalloonIconHandle = data.CustomBalloonIconHandle;

                // handle custom icons
                //if (IsFlagSet((int)ti.nid.dwInfoFlags, (int)Win32.BalloonFlags.USER) && (IntPtr)ti.nid.CustomBalloonIconHandle != IntPtr.Zero)
                if ((IntPtr)ti.nid.CustomBalloonIconHandle != IntPtr.Zero)
                {
                    System.Drawing.Icon icon = System.Drawing.Icon.FromHandle((IntPtr)ti.nid.CustomBalloonIconHandle);
                    using (icon)
                    {
                        image = icon.ToBitmap();
                    }
                }

                // handle callbacks - format: hwnd:msg:wparam:lparam
                if (ti.nid.uCallbackMsg > 0)
                {
                    string hwnd   = handle.ToString();
                    string msg    = ti.nid.uCallbackMsg.ToString();
                    string wparam = null;
                    string lparam = null;
                    if (ti.nid.uTimeoutOrVersion == 4)
                    {
                        wparam = MakeLParam(LoWord(0), HiWord(0)).ToString();
                        lparam = MakeLParam(LoWord(0x405), HiWord(Convert.ToInt32(ti.nid.uID))).ToString();
                    }
                    else
                    {
                        wparam = ti.nid.uID.ToString();
                        lparam = 0x405.ToString();
                    }
                    string d = String.Join(CALLBACK_DATA_SEPARATOR, new string[] { hwnd, msg, wparam, lparam });
                    callback = new CallbackContext(d, "balloonclick");
                }
            }

            switch (info)
            {
            case Win32.BalloonFlags.INFO:
                image   = Properties.Resources.info;
                szClass = ntNameInfo;
                break;

            case Win32.BalloonFlags.WARN:
                image   = Properties.Resources.warning;
                szClass = ntNameWarning;
                break;

            case Win32.BalloonFlags.CRIT:
                image   = Properties.Resources.error;
                szClass = ntNameError;
                break;

            case Win32.BalloonFlags.USER:
                // image is already set from above
                szClass = ntNameOther;
                break;
            }

            // DEBUG INFO
            Process pGfxApp = new Process();

            pGfxApp = Process.GetProcessById((Int32)szPid);
            string          sGfxApp = pGfxApp.MainModule.FileName; // Full path to sending app EXE
            FileVersionInfo sGfxApv = pGfxApp.MainModule.FileVersionInfo;

            Log("");
            Log("[#] New notification from " + System.IO.Path.GetFileName(sGfxApp));
            Log("    [#] Title: " + szTitle);
            Log("    [#] Text: " + szText);
            Log("    [*] hWnd: " + handle
                + "; uID: " + uID
                //+ "; Flags: " + data.Flags.ToString()
                //+ "; uCallbackMsg: " + data.uCallbackMsg
                //+ "; hIcon: " + data.hIcon);
                //Log("        State: " + data.State
                //+ "; StateMask: " + data.StateMask
                //+ "; uTimeoutOrVersion: " + data.uTimeoutOrVersion
                //+ "; guidItem: " + data.guidItem
                );
            Log("    [i] Type: " + szClass);
            string szFilename = Path.GetFileNameWithoutExtension(sGfxApv.FileName);
            string szFilever  = sGfxApv.FileVersion.Replace(",", ".").Replace(" ", "");

            Log("    [#] Additional file version information:");
            Log("        Source File name: " + (szFilename != "" ? szFilename : "ERROR (please report this bug)"));
            Log("        Source File version: " + (szFilever != "" ? szFilever : "[N/A]"));
            if (szTitle == "")
            {
                szTitle = szFilename;                // Use program name if no balloon title was set
            }

            /*
             * // CUSTOM ICON CODE
             * if (CustomBalloonIconHandle == 0)
             * {
             *  Icon gfxSource = Icon.ExtractAssociatedIcon(sGfxApp);
             *  if (gfxSource != null)
             *  {
             *      Log("    [i] Using application icon...");
             *      szIcon = sGfxApp + ",-1";
             *  }
             * }
             * if ((CustomBalloonIconHandle != 0) || (info == Win32.BalloonFlags.USER))
             * {
             *  //szClass = ALERT_USER;    // not used
             *  szIcon = "%" + CustomBalloonIconHandle;
             *  Log("[i] Using requested USER icon...");
             * }
             * */

            Log("");

            // TODO: NOTIFY
            Notification n = new Notification(appName, ntNameOther, String.Empty, szTitle, szText);

            n.Icon = image;
            if (GROWL)
            {
                growl.Notify(n, callback);
            }
        }
        //send a notification to the already existing instance that a new instance was started
        private bool NotifyPreviousInstance(object message)
        {
            //First, find the window of the previous instance
            IntPtr handle = Win32.FindWindow(null, _id);
            if (handle != IntPtr.Zero)
            {
                //create a GCHandle to hold the serialized object. 
                GCHandle bufferHandle = new GCHandle();
                try
                {
                    byte[] buffer;
                    Win32.COPYDATASTRUCT data = new Win32.COPYDATASTRUCT();
                    if (message != null)
                    {
                        //serialize the object into a byte array
                        buffer = Serialize(message);
                        //pin the byte array in memory
                        bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

                        data.dwData = 0;
                        data.cbData = buffer.Length;
                        //get the address of the pinned buffer
                        data.lpData = bufferHandle.AddrOfPinnedObject();
                    }

                    GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
                    try
                    {
                        Win32.SendMessage(handle, Win32.WM_COPYDATA, IntPtr.Zero, dataHandle.AddrOfPinnedObject());
                        Win32.SetForegroundWindow(handle); // Give focus to first instance
                        return true;
                    }
                    finally
                    {
                        dataHandle.Free();
                    }
                }
                finally
                {
                    if (bufferHandle.IsAllocated) bufferHandle.Free();
                }
            }
            return false;
        }