无需重新启动应用程序即可处理 URL 协议

发布于 2025-01-01 17:54:46 字数 1583 浏览 1 评论 0原文

我的问题都是关于 URL 协议的。

我已经注册了一个名为 mcm 的 URL 协议,但我注意到每次我从任何 Web 浏览器运行它时,都会创建一个新的实例该应用程序。有没有办法在已经运行的实例中处理协议请求?

例如,当 uTorrent 使用 torrent 协议时,它会立即处理请求,而无需再次运行应用程序。我真的找不到任何有趣的东西,所以我在这里问...

这是我用来注册协议的代码:

private static void RegisterUrlProtocol()
{
    UnregisterUrlProtocol();

    RegistryKey rKey = Registry.ClassesRoot.OpenSubKey(UrlProtocol, true);
    if (rKey == null)
    {
        rKey = Registry.ClassesRoot.CreateSubKey(UrlProtocol);
        rKey.SetValue("", "URL: MazCraft Protocol");
        rKey.SetValue("URL Protocol", "");

        rKey = rKey.CreateSubKey(@"shell\open\command");
        rKey.SetValue("", "\"" + Application.ExecutablePath + "\" %1");
    }

    if (rKey != null)
    {
        rKey.Close();
    }
}

以及读取参数的代码:

private static bool CheckForProtocolMessage()
{
    string[] arguments = Environment.GetCommandLineArgs();

    if (arguments.Length > 1)
    {
        string[] args = arguments[1].Split(':');
        args[1] = args[1].Replace("//", "");

        if (args[0].Trim().ToUpper() == "MCM" && args.Length > 1)
        {
            string[] actionDetail = args[1].Split('=');

            if (actionDetail[0].Trim().ToUpper() == "INSTALL" && actionDetail.Length > 1)
            {
                string id = actionDetail[1].Trim().Replace("/", "");

                Funcs.ID = id;

                return true;
            }
        }
    }

    return false;
}

任何帮助将不胜感激:) 问候。

My question is all about URL Protocols.

I have registered a URL Protocol called mcm, but I noticed that everytime I run it from any web browser, t creates a new instance of the application. Is there any way to handle the protocol request in an already running instance?

For example, when uTorrent is using the torrent protocol It handles the request immediately without running the app again. I couldn't really find anything interesting about it, so I am asking here...

Here is the code I use to register the protocol:

private static void RegisterUrlProtocol()
{
    UnregisterUrlProtocol();

    RegistryKey rKey = Registry.ClassesRoot.OpenSubKey(UrlProtocol, true);
    if (rKey == null)
    {
        rKey = Registry.ClassesRoot.CreateSubKey(UrlProtocol);
        rKey.SetValue("", "URL: MazCraft Protocol");
        rKey.SetValue("URL Protocol", "");

        rKey = rKey.CreateSubKey(@"shell\open\command");
        rKey.SetValue("", "\"" + Application.ExecutablePath + "\" %1");
    }

    if (rKey != null)
    {
        rKey.Close();
    }
}

And the code to read the arguments:

private static bool CheckForProtocolMessage()
{
    string[] arguments = Environment.GetCommandLineArgs();

    if (arguments.Length > 1)
    {
        string[] args = arguments[1].Split(':');
        args[1] = args[1].Replace("//", "");

        if (args[0].Trim().ToUpper() == "MCM" && args.Length > 1)
        {
            string[] actionDetail = args[1].Split('=');

            if (actionDetail[0].Trim().ToUpper() == "INSTALL" && actionDetail.Length > 1)
            {
                string id = actionDetail[1].Trim().Replace("/", "");

                Funcs.ID = id;

                return true;
            }
        }
    }

    return false;
}

Any help would be greatly appreciated :)
Greetings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹梦 2025-01-08 17:54:46

您可以使用互斥体来检测已在运行的应用程序实例,并通过命名管道将数据发送到现有实例。

希望以下示例有所帮助。
您可以将命名管道对象(在本例中为字符串)替换为您喜欢的任何可序列化对象。

NamedPipe.cs

    namespace SingleInstanceNP
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO.Pipes;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Threading;
    using System.IO;


    public class NamedPipe<T> : IDisposable
    {
        #region Attribute and Properties

        private string _pipeName;
        private NamedPipeServerStream _pipeServer;
        private bool _disposed;
        private Thread _thread;
        private bool _started;

        #endregion

        #region Constructors

        public NamedPipe(NameTypes pipeType)
        {
            _disposed = false;
            _started = false;
            _pipeName = pipeType.ToString();
            _thread = new Thread(Main);
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Name = "NamePipe: " + pipeType.ToString() + " Thread";
            _thread.IsBackground = true;
        }

        ~NamedPipe()
        {
            Dispose();
        }

        #endregion

        #region Events

        public delegate void Request(T t);
        public event Request OnRequest;

        #endregion

        #region Public Methods

        public static void Send(NameTypes pipeType, T t)
        {
            using (var npc = new NamedPipeClientStream(".", pipeType.ToString(), PipeDirection.Out))
            {
                var bf = new BinaryFormatter();
                npc.Connect();
                bf.Serialize(npc, t);
            }
        }

        public static T Recieve(NameTypes pipeType)
        {
            using (var nps = new NamedPipeServerStream(pipeType.ToString(), PipeDirection.In))
            {
                return Recieve(nps);
            }
        }

        public void Start()
        {
            if (!_disposed && !_started)
            {
                _started = true;
                _thread.Start();
            }
        }

        public void Stop()
        {
            _started = false;

            if (_pipeServer != null)
            {
                _pipeServer.Close();
                // disposing will occur on thread
            }
        }

        public void Dispose()
        {
            _disposed = true;
            Stop();

            if (OnRequest != null)
                OnRequest = null;
        }

        #endregion

        private void Main()
        {
            while (_started && !_disposed)
            {
                try
                {
                    using (_pipeServer = new NamedPipeServerStream(_pipeName))
                    {
                        T t = Recieve(_pipeServer);

                        if (OnRequest != null && _started)
                            OnRequest(t);
                    }
                }
                catch (ThreadAbortException)
                { }
                catch (System.IO.IOException iox)
                {
                    Console.WriteLine("ERROR: {0}", iox.Message);
                    Thread.Sleep(TimeSpan.FromSeconds(30));
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ERROR: {0}", ex.Message);
                    return;
                }
            }
        }

        private static T Recieve(NamedPipeServerStream nps)
        {
            var bf = new BinaryFormatter();

            try
            {
                nps.WaitForConnection();

                var obj = bf.Deserialize(nps);

                if (obj is T)
                    return (T)obj;
            }
            // Catch the IOException that is raised if the pipe is
            // broken or disconnected.
            catch (IOException e)
            {
                Console.WriteLine("ERROR: {0}", e.Message);
            }
            return default(T);
        }

        #region Enums

        public enum NameTypes
        {
            PipeType1
        }

        #endregion
    }
}

程序.cs
请将应用 GUID 注明为 在 C# 中使用全局互斥体的好模式是什么?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Threading;

namespace SingleInstanceNP
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // get application GUID as defined in AssemblyInfo.cs
            string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString();

            // unique id for global mutex - Global prefix means it is global to the machine
            string mutexId = string.Format("Global\\{{{0}}}", appGuid);

            using (var mutex = new Mutex(false, mutexId))
            {
                try
                {
                    if (!mutex.WaitOne(0, false))
                    {
                        //signal existing app via named pipes

                        NamedPipe<string>.Send(NamedPipe<string>.NameTypes.PipeType1, "test");

                        Environment.Exit(0);
                    }
                    else
                    {
                        // handle protocol with this instance   
                        Application.Run(new Form1());

                    }
                }
                finally
                {
                    mutex.ReleaseMutex();
                }
            }
        }
    }
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SingleInstanceNP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            // start listening for named pipe connections
            var namedPipeString = new NamedPipe<string>(NamedPipe<string>.NameTypes.PipeType1);
            namedPipeString.OnRequest += new NamedPipe<string>.Request(namedPipeString_OnRequest);
            namedPipeString.Start();
        }

        void namedPipeString_OnRequest(string t)
        {
            MessageBox.Show(t);
        }
    }
}

You could use a Mutex to detect an instance of the application that is already running and send the data over to the existing instance via Named Pipes.

Hope the following example helps.
you can swap out the named pipes object (in this case string) for whatever serializable object you like.

NamedPipe.cs

    namespace SingleInstanceNP
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO.Pipes;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Threading;
    using System.IO;


    public class NamedPipe<T> : IDisposable
    {
        #region Attribute and Properties

        private string _pipeName;
        private NamedPipeServerStream _pipeServer;
        private bool _disposed;
        private Thread _thread;
        private bool _started;

        #endregion

        #region Constructors

        public NamedPipe(NameTypes pipeType)
        {
            _disposed = false;
            _started = false;
            _pipeName = pipeType.ToString();
            _thread = new Thread(Main);
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Name = "NamePipe: " + pipeType.ToString() + " Thread";
            _thread.IsBackground = true;
        }

        ~NamedPipe()
        {
            Dispose();
        }

        #endregion

        #region Events

        public delegate void Request(T t);
        public event Request OnRequest;

        #endregion

        #region Public Methods

        public static void Send(NameTypes pipeType, T t)
        {
            using (var npc = new NamedPipeClientStream(".", pipeType.ToString(), PipeDirection.Out))
            {
                var bf = new BinaryFormatter();
                npc.Connect();
                bf.Serialize(npc, t);
            }
        }

        public static T Recieve(NameTypes pipeType)
        {
            using (var nps = new NamedPipeServerStream(pipeType.ToString(), PipeDirection.In))
            {
                return Recieve(nps);
            }
        }

        public void Start()
        {
            if (!_disposed && !_started)
            {
                _started = true;
                _thread.Start();
            }
        }

        public void Stop()
        {
            _started = false;

            if (_pipeServer != null)
            {
                _pipeServer.Close();
                // disposing will occur on thread
            }
        }

        public void Dispose()
        {
            _disposed = true;
            Stop();

            if (OnRequest != null)
                OnRequest = null;
        }

        #endregion

        private void Main()
        {
            while (_started && !_disposed)
            {
                try
                {
                    using (_pipeServer = new NamedPipeServerStream(_pipeName))
                    {
                        T t = Recieve(_pipeServer);

                        if (OnRequest != null && _started)
                            OnRequest(t);
                    }
                }
                catch (ThreadAbortException)
                { }
                catch (System.IO.IOException iox)
                {
                    Console.WriteLine("ERROR: {0}", iox.Message);
                    Thread.Sleep(TimeSpan.FromSeconds(30));
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ERROR: {0}", ex.Message);
                    return;
                }
            }
        }

        private static T Recieve(NamedPipeServerStream nps)
        {
            var bf = new BinaryFormatter();

            try
            {
                nps.WaitForConnection();

                var obj = bf.Deserialize(nps);

                if (obj is T)
                    return (T)obj;
            }
            // Catch the IOException that is raised if the pipe is
            // broken or disconnected.
            catch (IOException e)
            {
                Console.WriteLine("ERROR: {0}", e.Message);
            }
            return default(T);
        }

        #region Enums

        public enum NameTypes
        {
            PipeType1
        }

        #endregion
    }
}

Program.cs
Please give credit for the APP GUID to What is a good pattern for using a Global Mutex in C#?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Threading;

namespace SingleInstanceNP
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // get application GUID as defined in AssemblyInfo.cs
            string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString();

            // unique id for global mutex - Global prefix means it is global to the machine
            string mutexId = string.Format("Global\\{{{0}}}", appGuid);

            using (var mutex = new Mutex(false, mutexId))
            {
                try
                {
                    if (!mutex.WaitOne(0, false))
                    {
                        //signal existing app via named pipes

                        NamedPipe<string>.Send(NamedPipe<string>.NameTypes.PipeType1, "test");

                        Environment.Exit(0);
                    }
                    else
                    {
                        // handle protocol with this instance   
                        Application.Run(new Form1());

                    }
                }
                finally
                {
                    mutex.ReleaseMutex();
                }
            }
        }
    }
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SingleInstanceNP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            // start listening for named pipe connections
            var namedPipeString = new NamedPipe<string>(NamedPipe<string>.NameTypes.PipeType1);
            namedPipeString.OnRequest += new NamedPipe<string>.Request(namedPipeString_OnRequest);
            namedPipeString.Start();
        }

        void namedPipeString_OnRequest(string t)
        {
            MessageBox.Show(t);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文