Beispiel #1
0
        /// <summary>
        /// Sends the specified message string to a window. The SendMessage function calls the window
        /// procedure for the specified window and does not return until the window procedure has
        /// processed the message.
        /// </summary>
        /// <param name="hwnd">A handle to the window whose window procedure will receive the message.</param>
        /// <param name="message">The message to be sent to the window</param>
        /// <exception cref="Win32Exception">Win32 error occures</exception>
        /// <exception cref="ArgumentException">HWND is <see cref="IntPtr.Zero"/></exception>
        /// <exception cref="ArgumentNullException"><paramref name="message"/> is null</exception>
        /// <exception cref="ArgumentException"><paramref name="message"/> is empty</exception>
        public static void SendMessage(IntPtr hwnd, string message)
        {
            if (hwnd == IntPtr.Zero)
            {
                throw new ArgumentException("HWND cannot be IntPtr.Zero");
            }

            if (message == null)
            {
                throw new ArgumentNullException("The message cannot be null");
            }

            if (message == "")
            {
                throw new ArgumentException("The message cannot be empty");
            }

            var messageBytes = Encoding.Default.GetBytes(message); /* ANSII encoding */
            var data         = new UnsafeNative.COPYDATASTRUCT
            {
                dwData = (IntPtr)100,
                lpData = message,
                cbData = messageBytes.Length + 1 /* +1 because of 0 termination */
            };

            if (UnsafeNative.SendMessage(hwnd, WM_COPYDATA, IntPtr.Zero, ref data) != 0)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
Beispiel #2
0
        /// <summary>
        /// Configures the parameter passing
        /// </summary>
        /// <param name="args">The arguments passed to the application.</param>
        /// <param name="config">A delegate used to configure the parameter passing.</param>
        public static void Configure(string[] args, Action <ParameterPassingConfig> config)
        {
            var processes = Processes;

            @params = new ParameterPassingConfig(processes);
            config(@params);

            if (@params.Instances.Length == 0)
            {
                @params.ParameterPassedCallback(args);
                return; // In this case we just proceed on loading the program
            }

            // Single instance true will overrule everything else
            if (@params.IsSingleInstance)
            {
                if (args.Length > 0)
                {
                    UnsafeNative.SendMessage(processes[0].MainWindowHandle, string.Join(@params.DataSeparator.ToString(), args));
                }

                @params.ExitDelegate();
                return;
            }

            // You may want to have some parameters passed and some not
            if (@params.ValidationDelegate != null && [email protected](args))
            {
                return;
            }

            // Down here every empty args array will cause the to load regardless the config
            if (args.Length == 0)
            {
                @params.ParameterPassedCallback(args);
                return;
            }

            // If the process to prefer property is not null then we will only send the params to that certain process
            if (@params.ProcessToPrefer != null)
            {
                UnsafeNative.SendMessage(@params.ProcessToPrefer.MainWindowHandle, string.Join(@params.DataSeparator.ToString(), args));
                @params.ExitDelegate();
                return;
            }

            if (@params.Instances.Length == 1)
            {
                UnsafeNative.SendMessage(@params.Instances[0].MainWindowHandle, string.Join(@params.DataSeparator.ToString(), args));
                @params.ExitDelegate();
                return;
            }
            else if (@params.PassToAllInstances)
            {
                for (int i = 0; i < @params.Instances.Length; i++)
                {
                    UnsafeNative.SendMessage(@params.Instances[i].MainWindowHandle, string.Join(@params.DataSeparator.ToString(), args));
                }

                @params.ExitDelegate();
                return;
            }
            else if (@params.RandomSelectInstance)
            {
                UnsafeNative.SendMessage(Randomizer.Next(@params.Instances).MainWindowHandle, string.Join(@params.DataSeparator.ToString(), args));
                @params.ExitDelegate();
                return;
            }
        }