如何發送一個指令給另外一個進程[進程通信2]

上一篇文章講的是如何用Windows API去控制其他進程的顯示,這次主要說的是一個應用程序如何如何獲得指令並執行一系列的內部操作。如最大化,最小化等。


爲什麼要對消息進行轉化處理?

因爲Windows API只支持string類型的參數傳遞,所以在應用程序中對string進行轉化,轉化成進程可以識別的類型。


Scenario: 

1. 用戶發送指令給另外一個進程

2. 另外一個應用程序接收指令並做相應處理


----------------------------------------------------- 消息轉化 開始----------------------------------------------------- 

using System;
 
//[assembly: System.Runtime.InteropServices.ComVisible(false)]
[assemblyCLSCompliant(false)]
namespace SDK
{
    public class CMDParameters
    {
        static UIArtifact Command { getset; }
 
        /// <summary>
        /// Parse command line arguments.
        /// </summary>
        /// <param name="args">command line arguments</param>
        /// <returns>True if parse ok, false means print help message only.</returns>
        public static UIArtifact ParseParameters(string[] args)
        {
            Command = new UIArtifact();
 
            #region parse command line parameters
            for (int i = 0; i < args.Length; i++)
            {
                string arg = args[i];
                string candidateArg = string.Empty;
                bool isSwitch = false;
                // normalize argument
                if (arg.StartsWith("/") || arg.StartsWith("-"))
                {
                    isSwitch = true;
                    int index = arg.IndexOf(":");
                    if (index != -1)
                    {
                        candidateArg = arg.Substring(index + 1);
                        arg = arg.Substring(0, index);
                    }
                }
                arg = arg.ToLower();
 
                // if it is a switch argument
                if (isSwitch)
                {   // parse argument by argument match
                    ParseArguments(arg, candidateArg);
                }
            }
 
            return Command;
            #endregion
        }
 
        /// <summary>
        /// Parse command line arguments.
        /// </summary>
        /// <param name="args">command line arguments</param>
        /// <returns>True if parse ok, false means print help message only.</returns>
        public static UIArtifact ParseParameters(string args)
        {
            #region parse command line parameters
            string[] newargs = args.Split(new Char[] { ' ' });
            return ParseParameters(newargs);
            #endregion
        }
 
        private static bool ArgumentMatch(string arg, string formal)
        {
            return ArgumentMatch(arg, formal, true);
        }
 
        /// <summary>
        /// Provides two match kinds:
        /// extra match: argu == formal.
        /// short-form match: argu is a char and equals to formal's first char.
        /// argu is striped from command line arg, without '/' or '-'
        /// </summary>
        /// <param name="arg">the command line argument which starts with '/' or '-'</param>
        /// <param name="formal">the expected argument string</param>
        /// <param name="exactMatch">true means exact match mode, false means short-form match</param>
        /// <returns>true if argument matches, else false.</returns>
        private static bool ArgumentMatch(string arg, string formal, bool exactMatch)
        {
            if ((arg[0] == '/') || (arg[0] == '-'))
            {
                arg = arg.Substring(1);
                if (arg == formal)
                {
                    return true;
                }
                else if (!exactMatch && arg.Length == 1)
                {
                    return (arg[0] == formal[0]);
                }
            }
            return false;
        }
 
 
        private static void WriteHelpMessage()
        {
            Console.WriteLine(String.Format("Help PTMC",
                    System.Reflection.Assembly.GetExecutingAssembly().GetName().Name));
        }
 
        private static void ParseArguments(string arg, string candidateArg)
        {
            if (ArgumentMatch(arg, "?") || ArgumentMatch(arg, "help"))
            {
                WriteHeader();
                WriteHelpMessage();
            }
            else if (ArgumentMatch(arg, "v") || ArgumentMatch(arg, "visible"))
            {
                if (!string.IsNullOrEmpty(candidateArg.Trim()))
                {
                    bool isSucess = false;
                    if (bool.TryParse(candidateArg, out isSucess))
                    {
                        Command.IsShowInWindow = isSucess;
                    }
                }
            }
            else if (ArgumentMatch(arg, "t") || ArgumentMatch(arg, "tablepage"))
            {
                if (!string.IsNullOrEmpty(candidateArg.Trim()))
                {
                    UIView.Tab tab;
                    bool isSucess = Enum.TryParse(candidateArg.Trim(), trueout  tab);
                    if (isSucess)
                    {
                        switch (tab)
                        {
                            case UIView.Tab.Review:
                                Command.TabPage = UIView.Tab.Review;
                                break;
 
                            case UIView.Tab.Scheduling:
                                Command.TabPage = UIView.Tab.Scheduling;
                                break;
 
                            case UIView.Tab.Reporting:
                                Command.TabPage = UIView.Tab.Reporting;
                                break;
 
                            case UIView.Tab.Quality:
                                Command.TabPage = UIView.Tab.Quality;
                                break;
 
                            case UIView.Tab.Administration:
                                Command.TabPage = UIView.Tab.Administration;
                                break;
 
                            case UIView.Tab.None:
                                Command.TabPage = UIView.Tab.None;
                                break;
                        }
                    }
                }
            }
            else if (ArgumentMatch(arg, "c") || ArgumentMatch(arg, "cluster"))
            {
                if (!string.IsNullOrEmpty(candidateArg.Trim()))
                {
                    int cluster = 0;
                    if (int.TryParse(candidateArg.Trim(), out cluster))
                    {
                        Command.Cluster = cluster;
                    }
                }
            }
            else if (ArgumentMatch(arg, "p") || ArgumentMatch(arg, "protocolname"))
            {
                if (!string.IsNullOrEmpty(candidateArg.Trim()))
                {
                    Command.ProtocolName = candidateArg.Trim();
                }
            }
            else if (ArgumentMatch(arg, "f") || ArgumentMatch(arg, "folder"))
            {
                if (!string.IsNullOrEmpty(candidateArg.Trim()))
                {
                    if (!string.IsNullOrEmpty(candidateArg.Trim()))
                    {
                        Command.FolderPath = candidateArg.Trim();
                    }
                }
            }
            else if (ArgumentMatch(arg, "fresh"))
            {
                bool isSucess = false;
                if (bool.TryParse(candidateArg, out isSucess))
                {
                    Command.Isfresh = isSucess;
                }
            }
            else if (ArgumentMatch(arg, "close"))
            {
                bool isSucess = false;
                if (bool.TryParse(candidateArg, out isSucess))
                {
                    Command.IsClose = isSucess;
                }
            }
            else if (ArgumentMatch(arg, "cmd") || ArgumentMatch(arg, "command"))
            {
                if (!string.IsNullOrEmpty(candidateArg.Trim()))
                {
                    UIView.Command command;
                    bool isSucess = Enum.TryParse(candidateArg.Trim(), trueout  command);
                    if (isSucess)
                    {
                        switch (command)
                        {
                            case UIView.Command.FileCommons:
                                Command.Command = UIView.Command.FileCommons;
                                break;
 
                            case UIView.Command.ReviewRequest:
                                Command.Command = UIView.Command.ReviewRequest;
                                break;
 
                            case UIView.Command.None:
                                Command.Command = UIView.Command.None;
                                break;
                        }
                    }
                }
            }
            else
            {
                throw new Exception("[ERROR] Invalid switch: " + arg);
            }
        }
    }
 
    public class UIArtifact
    {
        private PTMCHostBase windowInfo;
 
        public PTMCHostBase WindowInfo
        {
            get
            {
                if (windowInfo == null)
                {
                    windowInfo = new PTMCHostBase();
                }
 
                return windowInfo;
            }
            set
            {
                windowInfo = value;
            }
        }
 
        public bool? IsShowInWindow { getset; }
 
        public bool Isfresh { getset; }
 
        public bool? IsClose { getset; }
 
        public UIView.Tab TabPage { getset; }
 
        public UIView.Command Command { getset; }
 
        public int Cluster { getset; }
 
        public string ProtocolName { getset; }
 
        public string FolderPath { getset; }
    }
 
    public class UIView
    {
        public enum Tab
        {
            None = 0x00,
            Quality,
            Review,
            Scheduling,
            Reports,
            Reporting,
            Administration,
        }
 
        public enum Command
        {
            None = 0x00,
            ReviewRequest,
            FileCommons,
        }
 
        public enum AppWindowName
        {
 
        }
    }
 
    public class PTMCHostBase
    {
        public bool isShowInWindow { getset; }
 
        public bool isShowInTaskBar { getset; }
 
        public IntPtr IntPtr { getset; }
 
        public string WindowName { getset; }
 
        public string WindowClassName { getset; }
 
    }
}


----------------------------------------------------- 消息轉化 結束----------------------------------------------------- 


----------------------------------------------------- 初始化 必要接口----------------------------------------------------- 

/// 在程序初始化的時候進行第一遍的消息處理
private  void XXX(){

        string[] cmd = System.Environment.GetCommandLineArgs();

            UIArtifact command = CMDParameters.ParseParameters(cmd);

            // MessageBox.Show(command.isShowInWindow.ToString());

            ShowWindow(command);
	}

    	private void RibbonWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.InitialHook();
        }

        private void InitialHook()
        {
            (PresentationSource.FromVisual(thisas HwndSource).AddHook(new HwndSourceHook(this.WndProc));
        }
 
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
	    // 在其他進程需要的時候進行消息處理
            if (msg == NativeMethodsExtensibility.WM_COPYDATA)            {                NativeMethodsExtensibility.COPYDATASTRUCT cds = (NativeMethodsExtensibility.COPYDATASTRUCT)System.Runtime.InteropServices.Marshal.PtrToStructure(lParam, typeof(NativeMethodsExtensibility.COPYDATASTRUCT));                if (!string.IsNullOrEmpty(cds.lpData))                {                    UIArtifact command = CMDParameters.ParseParameters(cds.lpData);                    // MessageBox.Show(cds.lpData); //測試返回消息                    // ShowWindow(command);//對返回消息進行處理                }            }            return hwnd;        }

----------------------------------------------------結束初始化 必要接口----------------------------------------------------- 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章