windows 如何创建自己的 URL 协议?(例如所以://...)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/389204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create my own URL protocol? (e.g. so://...)
提问by Michael Beck
I have seen:
我见过:
http://www...
ftp://blah.blah...
file://blah.blah...
unreal://blah.blah...
mailto://blah.blah...
http://www...
ftp://blah.blah...
file://blah.blah...
unreal://blah.blah...
mailto://blah.blah...
What is that first section where you see http
and the like called?
你看到的第一部分http
叫什么?
Can I register my own?
我可以自己注册吗?
采纳答案by James Gregory
The portion with the HTTP://
,FTP://
, etc are called URI Schemes
与该部分HTTP://
,FTP://
等被称为URI方案
You can register your own through the registry.
您可以通过注册表注册自己的。
HKEY_CLASSES_ROOT/
your-protocol-name/
(Default) "URL:your-protocol-name Protocol"
URL Protocol ""
shell/
open/
command/
(Default) PathToExecutable
Sources: https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml, http://msdn.microsoft.com/en-us/library/aa767914(v=vs.85).aspx
来源:https: //www.iana.org/assignments/uri-schemes/uri-schemes.xhtml,http: //msdn.microsoft.com/en-us/library/aa767914( v= vs.85).aspx
回答by Codebeat
Open notepad and paste the code below into it. Change "YourApp" into your app's name. Save it to YourApp.reg and execute it by clicking on it in explorer. That's it! Cheers! Erwin Haantjes
打开记事本并将下面的代码粘贴到其中。将“YourApp”更改为您的应用程序名称。将它保存到 YourApp.reg 并通过在资源管理器中单击它来执行它。就是这样!干杯! 埃尔文·汉杰斯
REGEDIT4
[HKEY_CLASSES_ROOT\YourApp]
@="URL:YourApp Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\YourApp\DefaultIcon]
@="\"C:\Program Files\YourApp\YourApp.exe\""
[HKEY_CLASSES_ROOT\YourApp\shell]
[HKEY_CLASSES_ROOT\YourApp\shell\open]
[HKEY_CLASSES_ROOT\YourApp\shell\open\command]
@="\"C:\Program Files\YourApp\YourApp.exe\" \"%1\" \"%2\" \"%3\" \"%4\" \"%5\" \"%6\" \"%7\" \"%8\" \"%9\""
回答by Peter Oehlert
This is different for each browser, in IE and windows you need to create what they call a pluggable protocol handler.
这对于每个浏览器都是不同的,在 IE 和 Windows 中,您需要创建他们所谓的可插入协议处理程序。
The basic steps are as follows:
基本步骤如下:
- Implement the IInternetProtocol interface.
- Implement the IInternetProtocolRoot interface.
- Implement the IClassFactory interface.
- Optional. Implement the IInternetProtocolInfo interface. Support for the HTTP protocol is provided by the transaction handler.
- If IInternetProtocolInfo is implemented, provide support for PARSE_SECURITY_URL and PARSE_SECURITY_DOMAIN so the URL security zone manager can handle the security properly. Write the code for your protocol handler.
- Provide support for BINDF_NO_UI and BINDF_SILENTOPERATION.
- Add a subkey for your protocol handler in the registry under HKEY_CLASSES_ROOT\PROTOCOLS\Handler.
- Create a string value, CLSID, under the subkey and set the string to the CLSID of your protocol handler.
- 实现 IInternetProtocol 接口。
- 实现 IInternetProtocolRoot 接口。
- 实现 IClassFactory 接口。
- 可选的。实现 IInternetProtocolInfo 接口。事务处理程序提供对 HTTP 协议的支持。
- 如果实现了 IInternetProtocolInfo,请提供对 PARSE_SECURITY_URL 和 PARSE_SECURITY_DOMAIN 的支持,以便 URL 安全区域管理器可以正确处理安全性。为您的协议处理程序编写代码。
- 提供对 BINDF_NO_UI 和 BINDF_SILENTOPERATION 的支持。
- 在注册表中的 HKEY_CLASSES_ROOT\PROTOCOLS\Handler 下为您的协议处理程序添加一个子项。
- 在子项下创建一个字符串值 CLSID,并将该字符串设置为您的协议处理程序的 CLSID。
See About Asynchronous Pluggable Protocols on MSDNfor more details on the windows side. There is also a sample in the windows SDK.
有关Windows 端的更多详细信息,请参阅MSDN上的关于异步可插拔协议。windows SDK 中还有一个示例。
A quick google also showed this article on codeproject: http://www.codeproject.com/KB/IP/DataProtocol.aspx.
一个快速的谷歌也显示了这篇关于 codeproject 的文章:http: //www.codeproject.com/KB/IP/DataProtocol.aspx。
Finally, as a security guy I have to point out that this code needs to be battle hardened. It's at a high risk because to do it reliably you can't do it in managed code and have to do it in C++ (I suppose you could use VB6). You should consider whether you really need to do this and if you do, design it carefully and code it securely. An attacker can easily control the content that gets passed to you by simply including a link on a page. For example if you have a simple buffer overflow then nobody better do this: <a href="custom:foooo{insert long string for buffer overflow here}"> Click me for free porn</a>
最后,作为一名安全人员,我必须指出,这段代码需要经过实战。它的风险很高,因为要可靠地做到这一点,您不能在托管代码中做到这一点,而必须在 C++ 中做到这一点(我想您可以使用 VB6)。您应该考虑是否真的需要这样做,如果需要,请仔细设计并安全编码。攻击者只需在页面上包含一个链接,就可以轻松控制传递给您的内容。例如,如果您有一个简单的缓冲区溢出,那么最好不要这样做:<a href="custom:foooo{insert long string for buffer overflow here}"> Click me for free porn</a>
Strongly consider using strsafe and the new secure CRT methods included in the VC8 and above compilers. See http://blogs.msdn.com/michael_howard/archive/2006/02/27/540123.aspxif you have no idea what I'm talking about.
强烈考虑使用 VC8 及以上编译器中包含的 strsafe 和新的安全 CRT 方法。如果您不知道我在说什么,请参阅http://blogs.msdn.com/michael_howard/archive/2006/02/27/540123.aspx。
回答by Yuval F
Here's a list of the registered URI schemes. Each one has an RFC - a document defining it, which is almost a standard. The RFC tells the developers of new applications (such as browsers, ftp clients, etc.) what they need to support. If you need a new base-level protocol, you can use an unregistered one. The other answers tell you how. Please keep in mind you can do lots of things with the existing protocols, thus gaining their existing implementations.
这是已注册URI 方案的列表。每个都有一个 RFC - 一个定义它的文档,这几乎是一个标准。RFC 告诉新应用程序(例如浏览器、ftp 客户端等)的开发人员他们需要支持什么。如果你需要一个新的基础协议,你可以使用一个未注册的协议。其他答案告诉你如何。请记住,您可以使用现有协议做很多事情,从而获得它们现有的实现。
回答by Dmitry Khalatov
For most Microsoft products (Internet Explorer, Office, "open file" dialogs etc) you can register an applicationto be run when URI with appropriate prefix is opened. This is a part of more common explanation- how to implement your own protocol.
对于大多数 Microsoft 产品(Internet Explorer、Office、“打开文件”对话框等),您可以注册一个应用程序,以便在打开具有适当前缀的 URI 时运行。这是更常见的解释的一部分- 如何实现您自己的协议。
回答by JSB????
It's called the protocol. The only thing that prevents you from making your own protocol is you have to:
它被称为协议。唯一阻止您制定自己的协议的是您必须:
- Write a browser or user agent of some kinds that understands that protocol, both in its URL form and in the actual data format
- Write a server that understands that protocol
- Preferably, have a specification for the protocol so that browser and server can continue to work together.
- 编写一个浏览器或某种类型的用户代理来理解该协议,包括 URL 形式和实际数据格式
- 编写一个理解该协议的服务器
- 最好有协议规范,以便浏览器和服务器可以继续协同工作。
Windows makes #1 really easy, an in many cases this is all you actually need. Viz:
Windows 使 #1 变得非常简单,在许多情况下,这就是您真正需要的。可视化:
回答by GaryF
The first section is called a protocol and yes you can register your own. On Windows (where I'm assuming you're doing this given the C# tag - sorry Mono fans), it's done via the registry.
第一部分称为协议,是的,您可以注册自己的协议。在 Windows 上(我假设你在给定 C# 标签的情况下这样做 - 对不起 Mono 粉丝),它是通过 registry完成的。
回答by Hugoware
A Protocol?
协议?
I found this, it appears to be a local setting for a computer...
我发现了这个,它似乎是计算机的本地设置...
回答by Kibbee
You don't really have to do any registering as such. I've seen many programs, like emule, create their own protocol specificier (that's what I think it's called). After that, you basically just have to set some values in the registry as to what program handles that protocol. I'm not sure if there's any official registry of protocol specifiers. There isn't really much to stop you from creating your own protocol specifier for your own application if you want people to open your app from their browser.
您实际上不必进行任何注册。我见过很多程序,比如 emule,创建了自己的特定协议(这就是我认为的名字)。之后,您基本上只需要在注册表中设置一些值,以确定哪个程序处理该协议。我不确定是否有任何协议说明符的官方注册表。如果您希望人们从他们的浏览器打开您的应用程序,没有什么可以阻止您为自己的应用程序创建自己的协议说明符。