Exemple #1
0
        private static IntPtr HandleMessages(IntPtr handle, int message, IntPtr wParameter, IntPtr lParameter, ref Boolean handled)
        {
            if (handle != MainWindow.WindowHandle)
            {
                return(IntPtr.Zero);
            }

            var data = UnsafeNative.GetMessage(message, lParameter);

            if (data != null)
            {
                if (Application.Current.MainWindow == null)
                {
                    return(IntPtr.Zero);
                }

                if (Application.Current.MainWindow.WindowState == WindowState.Minimized)
                {
                    Application.Current.MainWindow.WindowState = WindowState.Normal;
                }

                UnsafeNative.SetForegroundWindow(new WindowInteropHelper(Application.Current.MainWindow).Handle);

                var args = data.Split(' ');
                HandleParameter(args);
                handled = true;
            }

            return(IntPtr.Zero);
        }
Exemple #2
0
        public static void SendMessage(IntPtr hwnd, String message)
        {
            var messageBytes = Encoding.Unicode.GetBytes(message); /* ANSII encoding */
            var data         = new UnsafeNative.COPYDATASTRUCT {
                dwData = IntPtr.Zero,
                lpData = message,
                cbData = messageBytes.Length + 1 /* +1 because of \0 string termination */
            };

            if (UnsafeNative.SendMessage(hwnd, WM_COPYDATA, IntPtr.Zero, ref data) != 0)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
Exemple #3
0
        public static void Main(String[] args)
        {
            var proc        = Process.GetCurrentProcess();
            var processName = proc.ProcessName.Replace(".vshost", "");
            // NOTE: poor performance !
            var runningProcess = Process.GetProcesses()
                                 .FirstOrDefault(x => (x.ProcessName == processName || x.ProcessName == proc.ProcessName || x.ProcessName == proc.ProcessName + ".vshost") && x.Id != proc.Id);

            if (runningProcess == null)
            {
                var app = new App();
                app.InitializeComponent();
                var window = new MainWindow();
                MainWindow.HandleParameter(args);
                app.Run(window);
                return; // In this case we just proceed on loading the program
            }

            if (args.Length > 0)
            {
                UnsafeNative.SendMessage(runningProcess.MainWindowHandle, String.Join(" ", args));
            }
        }