Windows注册URL protocol

1. 添加注册表项:

command项只要1个参数,因为IE会把整个URL都当成1个参数传进来

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\myapp]
@="URL:myapp Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\myapp\DefaultIcon]
@="C:\\PyInstaller\\UrlProtocol\\dist\\UrlProtocol.exe"

[HKEY_CLASSES_ROOT\myapp\shell]

[HKEY_CLASSES_ROOT\myapp\shell\open]

[HKEY_CLASSES_ROOT\myapp\shell\open\command]
@="C:\\PyInstaller\\UrlProtocol\\dist\\UrlProtocol.exe \"%1\""

 

2. 参数传入之后,需要我们自行解析参数,并用来启动程序(需要把python编译成exe)。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import re
import os
import sys

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print("Wrong parameters, exit")
        assert False

        cmd_argvs = sys.argv[1] 
        #cmd_argvs = "myapp:///mcstd C:\\Users\\qiujin\\AppData\\LocalLow\\MyApp" 
        pattern = re.compile('^myapp\://(/mcstd)\s+(.+)$') 
        match_results = pattern.match(cmd_argvs) 
        if match_results: 
            argv1 = match_results.group(1) 
            argv2 = match_results.group(2) 
            print("argv1: %s\n" % argv1) 
            print("argv2: %s\n" % argv2) 
        else: 
            print("Wrong argv : %s\n" % cmd_argvs) 
            assert False
        
        cqmgr = "C:\\ProgramData\\MyApp\\cqmgr.exe"
        cqmgr_cmd = cqmgr + “ ” + argv1 + “ ” + argv2
        print("cqmgr cmd: %s" % atmgr_cmd)
        os.system(cqmgr_cmd) 

 

3. 在IE中输入:myapp:///mcstd C:\Users\qiujin\AppData\LocalLow\MyApp

posted on 2016-07-05 13:40  大宴天下  阅读(4137)  评论(0编辑  收藏  举报