从浏览器启动应用程序 - Application URL

本文介绍了如何在Windows和OSX操作系统下创建自定义协议的应用程序,并通过注册表和HTML代码实现协议的调用。包括C++程序、注册表配置、HTML测试代码及操作步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关键字:Browser,Application,URL Protocol,Windows,Mac,IE,Chrome,Safari。

OS: Windows 7, OS X Yosemite。

 

Windows:

1.写一个C++ 应用程序,代码如下:

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
    if(argc > 1)
    {
        std::string argi(argv[1]);
        cout<<argi<<endl;
    }

    cout<<"Press any key to continue"<<endl;
    int i;
    cin>>i;
    return 0;
}

2.在windows上新建文件MyApp.reg,文件内容如下:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\MyApp]
@="Open MyApp"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\MyApp\shell]

[HKEY_CLASSES_ROOT\MyApp\shell\open]

[HKEY_CLASSES_ROOT\MyApp\shell\open\command]
@="\"L:\\Dev\\MyApp\\Debug\\MyApp.exe\" \"%1\""

注:把"L:\\Dev\\MyApp\\Debug\\MyApp.exe\"更新为MyApp.exe所在的路径。

在Windows上运行MyApp.reg添加内容到注册表,如下图:

4.写一段HTML测试代码如下,保存到文件MyAppTest.html:

<!DOCTYPE html>
<html>
<body>
file URL or path: <input id='fileinput' type='text'/><br>
<button type='button' onclick='window.location.href = "MyApp://command=open&file=" + encodeURIComponent(document.getElementById("fileinput").value)'>Open in MyApp</button>
<button type='button' onclick='window.location.href = "MyApp://command=insert&file=" + encodeURIComponent(document.getElementById("fileinput").value)'>Insert in MyApp</button>
</body>
</html>

分别在Windows上用浏览器运行MyAppTest.html,点击“Open in MyApp”,结果如下:

 

OSX:

用Xode打开你的project

1.选择你的 project.

2.选择你的 target.

3.打开 Info tab.

4.在URL Types下面添加新的URL.

如下图:

在Safari里面运行URL:myapp://,将启动这个应用。

转载于:https://www.cnblogs.com/ldlchina/p/5045950.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值