markdown 如何从网络浏览器启动程序? (视窗)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 如何从网络浏览器启动程序? (视窗)相关的知识,希望对你有一定的参考价值。

# How to start programs from the web-browser? (Windows)

First off: 
*you **cannot** start programs via the regular `http` protocol, or any "default" protocol in that matter*.  
Think about the major security issues if some page you open could redirect you to, for example `C:\Windows\System32\shutdown.exe`. There's a good reason this does not work by default.  
**You will need access to the registry for this to work. Also beware of the security risks this might introduce!**

*"So, how can we execute programs from the browser?"* you might ask.  
In order to start executables we will write a little program which takes an executable path as an argument.  
In addition to that we will write our own URI scheme which will launch said program and hand over the string after the `://`.  
That way we can start executables using for example `launchprogram://path/to/my/executable.exe`.  


### 1. Declare the URI scheme.

To add an URI scheme we can either use `regedit`or write a registry file  (recognizable by `.reg` ending).
Here is an example of a registry file:
```
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\yourprotocolname]
@="URL:Launchprogram Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\yourprotocolname\DefaultIcon]
@="\"C:\\Path\\To\\The\\Executable\\filename.exe""

[HKEY_CLASSES_ROOT\yourprotocolname\shell]

[HKEY_CLASSES_ROOT\yourprotocolname\shell\open]

[HKEY_CLASSES_ROOT\yourprotocolname\shell\open\command]
@="\"C:\\Path\\To\\The\\Executable\\filename.exe \"%1\" "

```
in my case I went with the following registry file
<details><summary>[click to open]</summary>
<p>  


```
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\launchprogram]
@="URL:Launchprogram Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\launchprogram\DefaultIcon]
@="\"D:\\Program Files\\launchprogram\\launchprogram.exe\""

[HKEY_CLASSES_ROOT\launchprogram\shell]

[HKEY_CLASSES_ROOT\launchprogram\shell\open]

[HKEY_CLASSES_ROOT\launchprogram\shell\open\command]
@="\"D:\\Program Files\\launchprogram\\launchprogram.exe\" \"%1\" "
```
</p>
</details>

### 2. Check the data we get.
Before we proceed any further we should check if everything works.  
We can check what the executable receives by writing a quick console application like  
<details><summary>this</summary><p>

```c++
using System;
namespace launchprogram
{
    class Program
    {
        static void Main(string[] args)
        {
            if(args.Length > 0)
            {
                foreach(string e in args)
                {
                    Console.WriteLine(e);
                }
            }
            Console.ReadLine(); // Dont close app.
        }
    }
}
```

</p></details> 

When typing something like `launchprogram://C:\Program Files (x86)\Google\Chrome\Application\chrome.exe` into your web-browser a command window should appear with `launchprogram://C:%5CProgram%20Files%20(x86)%5CGoogle%5CChrome%5CApplication%5Cchrome.exe/` inside.  
Now we can see these weird segments like `%5C` or `%20`. These occur because we made use of URL-unsafe characters. Characters like `space` or `\` are not allowed inside an URL and are encoded accordingly.  
To decode the URL we can make use of `Uri.UnescapeDataString(string)` which should give us something like `launchprogram://C:\Program Files (x86)\Google\Chrome\Application\chrome.exe/`.


Now all we need to do is to remove the `launchprogram://` in the beginning as well as the `/` in the end.
`string.Replace("launchprogram://",String.Empty");` and `string.Remove(string.Length-1);` will do the trick.


Aaand.. that's pretty much it. Now you could add stuff like replacing `/` with `\` (`string.Replace("/","\\");`).

### 3. Launch the desired program.
Okay. Now we have the path - all we need to do now is to check if said executable exists, and if it does, run it.  
This can be done like this:
```c++
if (File.Exists(path))
{
	System.Diagnostics.Process.Start(path);
}
else
{
  Console.Write("Error: " + path + " does not point to a valid file.");
  Console.ReadKey();
}

```

### 4. Code example.

All together your little application might look similar to this:
```c++
using System;
using System.IO;
namespace launchprogram
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = String.Empty;
            if(args.Length > 0)
            {
                foreach(string e in args)
                {
                    path = path + e;
                }
                if(path.Length > 0)
                {
                    path = Environment.ExpandEnvironmentVariables(path);
                    path = Uri.UnescapeDataString(path);
                    path = path.Remove(path.Length - 1).Replace("launchprogram://", String.Empty).Replace("/", "\\");
                    if (File.Exists(path))
                    {
                        System.Diagnostics.Process.Start(path);
                    }
                    else
                    {
                        Console.Write("Error: " + path + " does not point to a valid file.");
                        Console.ReadKey();
                    }
                }
                else
                {
                    Console.Write("Error: Passed argument is empty.");
                    Console.ReadKey();
                }

            }
            else
            {
                Console.Write("Error: No argumets passed.");
                Console.ReadKey();
            }

            
        }
    }
}
```


以上是关于markdown 如何从网络浏览器启动程序? (视窗)的主要内容,如果未能解决你的问题,请参考以下文章

怎样在win10中设置开机启动项

markdown 从命令行界面创建可启动的macOS usb安装程序

如何将图像从安卓相机上传到网络服务器(不在应用程序中)

如何从网络/云启动应用程序

markdown 如何使用nginx代理网络应用程序?

markdown 如何使用nginx代理网络应用程序?