浏览器打开自定义scheme参考上一篇:Chromium 自定义scheme协议启动过程分析c++
1、注册表里面按照如下格式填写自定义scheme协议导入:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\jdtest]
@="URL:jdtest Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\jdtest\DefaultIcon]
@="D:\\Program Files (x86)\\Test\\jdtest.exe"
[HKEY_CLASSES_ROOT\Test\shell]
[HKEY_CLASSES_ROOT\Test\shell\open]
[HKEY_CLASSES_ROOT\jdtest\shell\open\command]
@="\"D:\\Program Files (x86)\\Test\\jdtest.exe\" \"%1\""
2、jdtest.exe代码
#include <iostream>
int main(int arg, const char *args[])
{
std::cout << "param:" << args[1];
std::cout << "\nHello World!\n";
system("pause");
}
3、运行jdtest协议测试程序代码【jdtest02.exe】:
#include <iostream>
#include <stdlib.h>
#include <tchar.h>
#include <string>
#include <windows.h>
#include <KnownFolders.h>
#include <shlobj.h>
void runjdtest() {
std::string escaped_url = "jdtest://jdtest.com:8080/dl/news/open?data=902323¶ms=test";
escaped_url.insert(0, "\"");
escaped_url += "\"";
const size_t kMaxUrlLength = 2048;
if (escaped_url.length() > kMaxUrlLength)
return;
char system_buffer[MAX_PATH];
system_buffer[0] = 0;
::GetSystemDirectoryA(system_buffer, MAX_PATH);
std::string sys_path(system_buffer);
if (reinterpret_cast<ULONG_PTR>(ShellExecuteA(
NULL, "open", escaped_url.c_str(), NULL,
sys_path.c_str(), SW_SHOWNORMAL)) <= 32) {
return;
}
}
int main()
{
runjdtest();
std::cout << "run jdtest \n";
}
4、运行jdtest02.exe看下效果:jdtest02.exe已经将jdtest.exe启动,并且参数已经传到jdtest.exe。