• 仿迅雷客户端的浏览器自定义协议的小程序


    上家公司有这样的一个需求:

    在网页中点一个链接,启动我们自己的一个客户端程序,并接受链接传递过来的参数,在客户端做相应的处理

    1。自定义浏览器协议

         只要给注册表写进去一点信息就可以实现将自己定义的协议(如:mylink://)跟某客户端程序关联,google一下就可以了

    2.只能让这个客户端运行一个实例


     static void Main(string[] url)
            
    {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(
    false);

                
    if (url != null && url.Length > 0)
                
    {
                    
    if (CreateMutex())
                    
    {
                        form 
    = new MainForm(url[0]);
                        Application.Run(form);
                        ReleaseMutex(); 
                    }

                    
    else
                    
    {
                        
    int hd = FindWindow(null"自定义浏览器协议");
                        
    if (hd == 0)
                        
    {
                            
    throw new Exception("Could   not   find   Main   window!");
                        }

                        
    byte[] sarr = System.Text.Encoding.Default.GetBytes(url[0]);
                        
    int len = sarr.Length;
                        COPYDATASTRUCT cds;
                        cds.dwData 
    = (IntPtr)100;
                        cds.lpData 
    = url[0];
                        cds.cbData 
    = len + 1;
                        SendMessage(hd, 
    0x004A0ref cds);
                    }

                }

                
    else
                
    {
                    
    if (CreateMutex())
                    
    {
                        form 
    = new MainForm();
                        Application.Run(form);
                        ReleaseMutex();
                    }

                    
    else
                    
    {
                        MessageBox.Show(
    "该程序只能运行一个实例!");
                    }

                }

            }

            public static bool CreateMutex()
            {
                
    bool result = false;
                mutex 
    = new Mutex(true, Assembly.GetEntryAssembly().FullName, out result);
                
    return result; 
            }

            
    public static void ReleaseMutex()
            {
                
    if (mutex != null)
                {
                    mutex.Close();
                }
            }

    3.消息传递   用windows的消息机制


            /// <summary>
            
    /// 该结构用来定义用户封装的数据
            
    /// </summary> 

            public struct COPYDATASTRUCT
            
    {
                
    public IntPtr dwData;
                
    public int cbData;
                [MarshalAs(UnmanagedType.LPStr)]
                
    public string lpData;
            }


           

            /// <summary>
            
    /// 消息处理
            
    /// </summary>
            
    /// <param name="m">消息</param>

            protected override void DefWndProc(ref Message m)
            
    {
                
    switch (m.Msg)
                
    {
                    
    case 0x004A:
                        COPYDATASTRUCT mystr 
    = new COPYDATASTRUCT();
                        Type mytype 
    = mystr.GetType();
                        mystr 
    = (COPYDATASTRUCT)m.GetLParam(mytype);
                        WebBrowserNavigatingEventArgs e 
    = new WebBrowserNavigatingEventArgs(new Uri(mystr.lpData.ToString()), "");
                        OnNavigate(
    null, e);
                        
    break;
                    
    default:
                        
    base.DefWndProc(ref   m);
                        
    break;
                }

            }
  • 相关阅读:
    内存分配机制
    typedef struct 和struct的区别
    imshow
    #include<string.h>和#include<string>
    Internal Errors
    TStopWatch 基本知识
    string 新常量 Empty
    System 这四个单元多用用(近期)
    对象释放三种方法对比:Free --> FreeAndNil() --> DisposeOf()
    程序性能优化的3个级别
  • 原文地址:https://www.cnblogs.com/sunsoft/p/1965620.html
Copyright © 2020-2023  润新知