Exemple #1
0
        private static IpcParamEx LoadIpcInfoFile(int nId)
        {
            string strPath = GetIpcFilePath(nId);

            if (string.IsNullOrEmpty(strPath))
            {
                return(null);
            }

            IpcParamEx ipcParam = null;

            try {
                XmlSerializer xml = new XmlSerializer(typeof(IpcParamEx));

                using (var fs = new FileStream(strPath, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                    ipcParam = (IpcParamEx)xml.Deserialize(fs);
                }
            } catch (Exception ex) {
                Logger.LogWrite("IpcFake.LoadIpcInfoFile(): " + ex.Message);
            }

            RemoveIpcInfoFile(nId);

            return(ipcParam);
        }
Exemple #2
0
        private static void ProcessMessage(IpcMessage msg)
        {
            try {
                if (fEnforcer == null)
                {
                    return;
                }

                if (msg.Message == AppMessage.RestoreWindow)
                {
                    MessageEventArgs eArgs = new MessageEventArgs("restore");
                    fEnforcer.OnMessageReceived(eArgs);
                }
                else if (msg.Message == AppMessage.IpcByFile)
                {
                    IpcParamEx ipcMsg = LoadIpcInfoFile(msg.LParam);
                    if (ipcMsg != null && ipcMsg.Message == CmdSendArgs)
                    {
                        string[] vArgs = SafeDeserialize(ipcMsg.Params);

                        if (vArgs != null)
                        {
                            MessageEventArgs eArgs = new MessageEventArgs(vArgs);
                            fEnforcer.OnMessageReceived(eArgs);
                        }
                    }
                }
            } catch (Exception ex) {
                Logger.LogWrite("IpcFake.ProcessMessage(): " + ex.Message);
            }
        }
Exemple #3
0
        public static void SendMessage(IpcParamEx ipcMsg)
        {
            if (ipcMsg == null)
            {
                throw new ArgumentNullException("ipcMsg");
            }

            try {
                Random rnd = new Random();
                int    nId = rnd.Next() & 0x7FFFFFFF;

                if (!WriteIpcInfoFile(nId, ipcMsg))
                {
                    return;
                }

                Send(AppMessage.IpcByFile, nId, true);

                string strIpcFile = GetIpcFilePath(nId);
                for (int r = 0; r < 50; ++r)
                {
                    if (!File.Exists(strIpcFile))
                    {
                        break;
                    }

                    Thread.Sleep(20);
                }

                RemoveIpcInfoFile(nId);
            } catch (Exception ex) {
                Logger.LogWrite("IpcFake.SendMessage(): " + ex.Message);
            }
        }
Exemple #4
0
        private static bool WriteIpcInfoFile(int nId, IpcParamEx ipcMsg)
        {
            string strPath = GetIpcFilePath(nId);

            if (string.IsNullOrEmpty(strPath))
            {
                return(false);
            }

            try
            {
                XmlSerializer xml = new XmlSerializer(typeof(IpcParamEx));

                using (var fs = new FileStream(strPath, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    xml.Serialize(fs, ipcMsg);
                }

                return(true);
            }
            catch (Exception ex)
            {
                Logger.LogWrite("IpcFake.WriteIpcInfoFile(): " + ex.Message);
            }

            return(false);
        }
Exemple #5
0
        public void Test_IpcParamEx()
        {
            var ipcParam = new IpcParamEx();

            Assert.IsNotNull(ipcParam);
            Assert.AreEqual("", ipcParam.Message);
            Assert.AreEqual("", ipcParam.Params);

            ipcParam = new IpcParamEx("message", "params");
            Assert.IsNotNull(ipcParam);
            Assert.AreEqual("message", ipcParam.Message);
            Assert.AreEqual("params", ipcParam.Params);
        }
Exemple #6
0
        /// <summary>
        /// Sends a message to the first instance of the application.
        /// </summary>
        /// <param name="message">The message to send to the first instance of the application. The message must be serializable.</param>
        /// <exception cref="System.InvalidOperationException">The object was constructed with the SingleInstanceTracker(string name) constructor overload, or with the SingleInstanceTracker(string name, SingleInstanceEnforcerRetriever enforcerRetriever) cosntructor overload, with enforcerRetriever set to null.</exception>
        /// <exception cref="SingleInstancing.SingleInstancingException">The SingleInstanceInteractor has failed to send the message to the first application instance. The first instance might have terminated.</exception>
        public void SendMessageToFirstInstance(object message)
        {
            if (fDisposed)
            {
                throw new ObjectDisposedException("The SingleInstanceTracker object has already been disposed.");
            }

            #if IPC_SUPPORTS
            if (fIpcChannel == null)
            {
                throw new InvalidOperationException("The object was constructed with the SingleInstanceTracker(string name) constructor overload, or with the SingleInstanceTracker(string name, SingleInstanceEnforcerRetriever enforcerRetriever) constructor overload, with enforcerRetriever set to null, thus you cannot send messages to the first instance.");
            }

            try
            {
                fProxy.Enforcer.OnMessageReceived(new MessageEventArgs(message));
            }
            catch (Exception ex)
            {
                throw new SingleInstancingException("Failed to send message to the first instance of the application. The first instance might have terminated.", ex);
            }
            #else
            try
            {
                string[] args = message as string[];
                if (args.Length == 0 || string.IsNullOrEmpty(args[0]))
                {
                    IpcFake.Send(AppMessage.RestoreWindow, 0, false);
                }
                else
                {
                    IpcParamEx ipcMsg = new IpcParamEx(IpcFake.CmdSendArgs, IpcFake.SafeSerialize(args));
                    IpcFake.SendGlobalMessage(ipcMsg);
                }
            }
            catch (Exception ex)
            {
                Logger.LogWrite("SingleInstanceTracker.SendMessageToFirstInstance.2(): " + ex.Message);
            }
            #endif
        }
Exemple #7
0
        public static void SendMessage(string cmd, string[] args)
        {
            IpcParamEx ipcMsg = new IpcParamEx(cmd, IpcFake.SafeSerialize(args));

            IpcFake.SendMessage(ipcMsg);
        }