This is the 9th day of my participation in the More text Challenge. For details, see more text Challenge

To see what it looks like in action, let’s type ping so.com into the browser

Try opening your calculator and starting Steam

To achieve this effect, we actually use the browser custom protocol, we can open the wechat, QQ, Emali application through the custom protocol. For example, web customer service systems often use the Tencent :// custom protocol to provide a QQ chat button that the browser launches external applications when clicked.

After we write the custom protocol into the registry, the browser will start our pilot application according to the protocol. In Tencent :// protocol, a timwp.exe is started. Timwp.exe will start the corresponding service after parsing the URL parameters.

From this we can register a CMD protocol ourselves to launch our application. We import the following urlcmd. reg registry file into the HKEY_CLASSES_ROOT entry of the registry, or manually add entries and strings to the registry. We tell the registry that I want to register a URL Protocol with the directory structure HKEY_CLASSES_ROOT\ CMD \shell\open\command.

Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\ CMD] @="URL: cmd protocol" "URL Protocol"="" [HKEY_CLASSES_ROOT\cmd\shell] [HKEY_CLASSES_ROOT\cmd\shell\open] [HKEY_CLASSES_ROOT\cmd\shell\open\command] @="C:\\Windows\\system32\\urlCmd.exe \"%1\""Copy the code

After the import, it looks like this. The pilot application we want to launch is C:\Windows\system32\ urlcmd.exe

Next we need to write an urLcmd.exe to implement the functionality we need. In this case, we write it with go. We parse the passed URL into the form
and then call CMD to execute. Note that the parameters are URL-encoded. In urlcmd. go below we only decoded the Spaces.

//urlCmd.go

package main

import (
	"fmt"
	"log"
	"os"
	"os/exec"
	"strings"
	"time"
)

func main(a) {
	var cmd_args, cmd_com string                        // With parameter but without parameter
	cmd_all := os.Args[1:]                              // [cmd://%20
      
       ] [cmd://
       /]
      
	cmd_cmd := strings.Split(string(cmd_all[0]), "/ /")  // [cmd: %20
      
       ] [cmd: 
       /]
      
	cmd_pre := strings.Split(string(cmd_cmd[1]), "% 20") // [
       ] [
       /]
      

	if len(cmd_pre) == 1 {
		cmd_args = "" // The parameter is empty
	} else {
		cmd_args = strings.Replace(cmd_pre[1]."/"."".1)
	}
	cmd_com = strings.Replace(cmd_pre[0]."/"."".1)

	fmt.Printf("\ncommand: %v\nargs: %v\n", cmd_com, cmd_args)
	cmd := exec.Command("cmd"."/c", cmd_com, cmd_args)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err := cmd.Run()
	iferr ! =nil {
		log.Fatalf("err:%v", err)
	}
	time.Sleep(2 * time.Second)
}

Copy the code

Let’s build into an EXE file

go bulid urlCmd.go
Copy the code

Put urlcmd. exe in C:\Windows\system32\ and all urls we open in the browser using CMD :// will be routed to this application.


I’m a loser. What about you? Urlcmd. go has a lot of room for improvement, interested students can make their own parser try oh.


Feel free to point out any shortcomings in this article in the comments section.

Welcome to collect, like and ask questions. Keep an eye on top water cooler managers and sometimes do more than just boil hot water.