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

关键字: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://,将启动这个应用。

posted @   Ldlchina  阅读(8120)  评论(1编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示