上篇文章提到了本地exe服务的启动。其中url唤醒大家比较感兴趣,但是有些朋友有点懵,现在做个实例大体说一下。
步骤一:添加注册表
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\你的唤醒协议]
"URL Protocol"="D:\\soft\\你的exe名字.exe"
@="TscPrintProtocol"
[HKEY_CLASSES_ROOT\你的唤醒协议 \DefaultIcon]
@="D:\\soft\\你的exe名字.exe,1"
[HKEY_CLASSES_ROOT\你的唤醒协议 \shell]
[HKEY_CLASSES_ROOT\你的唤醒协议 \shell\open]
[HKEY_CLASSES_ROOT\你的唤醒协议 \shell\open\command]
@="\"D:\\soft\\你的exe名字 .exe\" \"%1\""
修改好内容保存到文本文件,并修改扩展名为 .reg,然后双击执行,添加到注册表(注意对应相关路径)

步骤二:编写exe
delphi新建工程,File->Application。vcl/fmx工程都可以。
然后在主窗体拖一个memo,在form的create事件添加以下代码
Memo1.Lines.Add(ParamStr(0));
Memo1.Lines.Add(ParamStr(1));
拉起exe的时候,会把链接地址作为参数传递给exe。
步骤三:唤起,传参
把exe文件放到注册表里对应的目录下,然后在浏览器里按以下规则打开网址
urlprotocol://host/path/uri?name1=value2&name=value2
例如:baobaourl://print/task?taskid=001
然后就可以唤醒你的exe

unigui种使用以下代码进行调用:
unisession.addjs('window.open("
baobaourl://print/task?taskid=001")');
步骤四:解析唤醒参数
uses IdURI;
var
idurl: TIdURI;
begin
idurl := TIdURI.Create('baobaourl://asdf/qwer/zxcv/vbnm?c=d&a=b');
Memo1.Lines.Add('protocol :' + idurl.Protocol);
Memo1.Lines.Add('Host :' + idurl.Host);
Memo1.Lines.Add('Path :' + idurl.Path);
Memo1.Lines.Add('URI :' + idurl.URI);
Memo1.Lines.Add('Document :' + idurl.Document);
Memo1.Lines.Add('Params :' + idurl.Params);
idurl.Free;
end;

然后就可以根据各个参数执行各个不同命令或功能。
tip:提示以下,该方法每次都会唤醒一个exe,也就是说执行多次就会打开多个exe,对于此问题,可以使用进程互斥(CreateMutex)等方法,防止exe多次启动,但是同时要处理参数传递,也就是要通过共享变量,或者namepipt,或者通讯等方式后启动的exe把接收到的参数传递给前一个exe。
此方法适用场景:例如身份拍照留档,表单种填写完基础信息,然后把表单id,在唤醒exe作为参数传入,exe对用户拍照,拍照完成后把表单id和照片上传。这个场景多个exe可以同时运行使用。扩展一下,exe接收到传入的表单id,然后到服务器查询该表单id对应的用户姓名,这样可以防止多个exe信息混乱。
Comments | 1 条评论
Wohh exactly what I was searching for, thank you for putting up. Jose Sicari