搜关键词网站,网站后期培训机构全国排名,上海企业微信网站制作,北京神州网站建设你可以使用自定义协议方案#xff08;Protocol Scheme#xff09;实现网页上点击URL后自动启动远程桌面连接#xff08;mstsc#xff09;#xff0c;参考你提供的C代码思路#xff0c;如下实现#xff1a;
第一步#xff1a;注册自定义协议
使用类似openmstsc://协议…
你可以使用自定义协议方案Protocol Scheme实现网页上点击URL后自动启动远程桌面连接mstsc参考你提供的C代码思路如下实现
第一步注册自定义协议
使用类似openmstsc://协议。
注册示例 (reg 文件形式)
Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\openmstsc]
URL:openmstsc Protocol
URL Protocol[HKEY_CLASSES_ROOT\openmstsc\shell\open\command]
\C:\\your-path\\open_mstsc.exe\ \%1\或通过你的C代码自动完成注册代码里已经包含该功能。 第二步网页中调用协议URL
网页端代码简单HTML
!DOCTYPE html
html langzh
headmeta charsetUTF-8title远程桌面连接示例/title
/head
bodya hrefopenmstsc://192.168.1.100:3389连接远程桌面 192.168.1.100/a
/body
/html注意
点击此链接时浏览器会提示用户是否允许调用该协议首次使用时会询问确认即可。 第三步你的C程序实现要点已提供以下强调注意点
你的C程序中关键实现点你代码中已经包含了
解析传入的URL提取IP:端口。使用ShellExecuteExW调用mstsc.exe并传入/v:IP:Port参数。
示例摘录
void OpenWithMstsc(const std::wstring serverIP) {if (serverIP.empty()) return;std::wstring mstscArgs L/v: serverIP;SHELLEXECUTEINFOW sei { sizeof(sei) };sei.lpFile Lmstsc.exe;sei.lpParameters mstscArgs.c_str();sei.nShow SW_SHOWNORMAL;sei.fMask SEE_MASK_NOASYNC;ShellExecuteExW(sei);
}完整流程说明
网页链接点击 → 浏览器触发openmstsc://IP:Port。浏览器调用注册好的协议→ 执行open_mstsc.exe传入参数。程序解析URL → 调用mstsc.exe→ 远程桌面客户端打开。
这样即可实现点击网页上的链接自动打开远程桌面连接的功能。
// open_mstsc.cpp
// C 重写版本实现与 C# 相同功能包括注册自定义协议、解析 URL并通过 WPS 打开文件且不弹出控制台窗口。
//语言功能 结构化绑定 需要编译器标志 /std:c17
//链接器-》系统-》子系统-》窗口模式
#include windows.h
#include shlwapi.h
#include iostream
#include string
#include urlmon.h
#include shellapi.h
#include algorithm // for std::transform
#pragma comment(lib, Shlwapi.lib)
#pragma comment(lib, urlmon.lib)
#include cctype
const std::wstring PROTOCOL_NAME LopenMstsc;
#include windows.h
#include shellapi.hbool IsRunAsAdmin() {BOOL isAdmin FALSE;PSID adminGroup;SID_IDENTIFIER_AUTHORITY NtAuthority SECURITY_NT_AUTHORITY;if (AllocateAndInitializeSid(NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,0, 0, 0, 0, 0, 0, adminGroup)) {CheckTokenMembership(NULL, adminGroup, isAdmin);FreeSid(adminGroup);}return isAdmin;
}void RelaunchAsAdmin() {wchar_t exePath[MAX_PATH];GetModuleFileNameW(NULL, exePath, MAX_PATH);SHELLEXECUTEINFOW sei { sizeof(sei) };sei.lpVerb Lrunas; // 以管理员权限运行sei.lpFile exePath;sei.nShow SW_SHOWNORMAL;sei.fMask SEE_MASK_NOASYNC;if (ShellExecuteExW(sei)) {ExitProcess(0); // 关闭当前进程}
}
// 替代 HttpUtility.UrlDecode 的简单实现
// 使用 UrlUnescapeW API 进行解码
static std::wstring UrlDecode(const std::wstring encoded) {if (encoded.empty()) return L;// 预留缓冲区存放解码后的结果const size_t BUFFER_SIZE 4096;wchar_t buffer[BUFFER_SIZE];wcsncpy_s(buffer, encoded.c_str(), BUFFER_SIZE);DWORD dwSize (DWORD)BUFFER_SIZE;HRESULT hr UrlUnescapeW(buffer, NULL, dwSize, URL_UNESCAPE_INPLACE);if (SUCCEEDED(hr)) {return std::wstring(buffer);}else {// 如果解码失败可以根据需求返回空字符串或原始值return L;}
}static std::wstring ProcessServerIP(const std::wstring inputUrl, const std::wstring protocolName)
{// 1) 构造 {protocol}:// 的小写形式std::wstring lowerProtocol protocolName;std::transform(lowerProtocol.begin(), lowerProtocol.end(), lowerProtocol.begin(), ::towlower);std::wstring protocolPrefix lowerProtocol L://;// 2) 转换 inputUrl 为小写进行查找std::wstring lowerInput inputUrl;std::transform(lowerInput.begin(), lowerInput.end(), lowerInput.begin(), ::towlower);// 3) 移除 protocol://std::wstring url;size_t pos lowerInput.find(protocolPrefix);if (pos ! std::wstring::npos){url inputUrl.substr(pos protocolPrefix.size());}else{url inputUrl; // 原始 URL}// 4) URL 解码假设 UrlDecode 是可用函数url UrlDecode(url);// 5) 去除路径部分仅保留 host:portsize_t pathPos url.find(L/);if (pathPos ! std::wstring::npos){url url.substr(0, pathPos);}return url;
}//-----------------------------------------------------------
// 下面是原有的函数声明与实现
//-----------------------------------------------------------void RegisterUrlScheme(const std::wstring protocol, const std::wstring exePath);
std::wstring GetRegisteredPath(const std::wstring protocol);void OpenWithMstsc(const std::wstring fileUrl);
std::pairstd::wstring, std::wstring GetWpsLauncherPath();
void EnsureUrlScheme(const std::wstring protocol);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
//int main(){if (!IsRunAsAdmin()) {RelaunchAsAdmin(); // 如果不是管理员权限则重新以管理员权限运行return 0;}EnsureUrlScheme(PROTOCOL_NAME);int argc;LPWSTR* argv CommandLineToArgvW(GetCommandLineW(), argc);if (argv NULL) return 0;if (argc 2) {//MessageBoxW(NULL, L⚠️ 未检测到 URL 参数。, L提示, MB_OK | MB_ICONWARNING);return 0;}// 调用新版 ProcessUrlstd::wstring url ProcessServerIP(argv[1], PROTOCOL_NAME);if (PathIsURLW(url.c_str())) {OpenWithMstsc(url);}else {}LocalFree(argv);return 0;
}void EnsureUrlScheme(const std::wstring protocol) {wchar_t exePath[MAX_PATH];GetModuleFileNameW(NULL, exePath, MAX_PATH);std::wstring registeredPath GetRegisteredPath(protocol);if (registeredPath.empty() || !_wcsicmp(registeredPath.c_str(), exePath) 0) {RegisterUrlScheme(protocol, exePath);}
}std::wstring GetRegisteredPath(const std::wstring protocol) {HKEY hKey;std::wstring regPath L;std::wstring keyPath protocol L\\shell\\open\\command;if (RegOpenKeyExW(HKEY_CLASSES_ROOT, keyPath.c_str(), 0, KEY_READ, hKey) ERROR_SUCCESS) {wchar_t value[MAX_PATH];DWORD value_length sizeof(value);if (RegQueryValueExW(hKey, NULL, NULL, NULL, (LPBYTE)value, value_length) ERROR_SUCCESS) {std::wstring commandLine(value);size_t firstQuoteEnd commandLine.find(L, 1);if (firstQuoteEnd ! std::wstring::npos) {regPath commandLine.substr(1, firstQuoteEnd - 1);}}RegCloseKey(hKey);}return regPath;
}void RegisterUrlScheme(const std::wstring protocol, const std::wstring exePath) {HKEY hKey;std::wstring keyPath protocol;if (RegCreateKeyExW(HKEY_CLASSES_ROOT, keyPath.c_str(), 0, NULL, 0, KEY_WRITE, NULL, hKey, NULL) ERROR_SUCCESS) {RegSetValueExW(hKey, NULL, 0, REG_SZ, (const BYTE*)(LURL: protocol L Protocol).c_str(),(DWORD)((protocol.size() 10) * sizeof(wchar_t)));RegSetValueExW(hKey, LURL Protocol, 0, REG_SZ, (const BYTE*)L, sizeof(wchar_t));HKEY hCommandKey;if (RegCreateKeyExW(hKey, Lshell\\open\\command, 0, NULL, 0, KEY_WRITE, NULL, hCommandKey, NULL) ERROR_SUCCESS) {std::wstring command L\ exePath L\ \%1\;RegSetValueExW(hCommandKey, NULL, 0, REG_SZ, (const BYTE*)command.c_str(), (DWORD)(command.size() * sizeof(wchar_t)));RegCloseKey(hCommandKey);}RegCloseKey(hKey);MessageBoxW(NULL, L远程组件注册成功。, L成功, MB_OK | MB_ICONINFORMATION);}
}void OpenWithMstsc(const std::wstring serverIP) {if (serverIP.empty()) {// MessageBoxW(NULL, L❌ 服务器 IP 不能为空。, L错误, MB_OK | MB_ICONERROR);return;}// 远程桌面连接的完整命令行参数std::wstring mstscArgs L/v: serverIP;SHELLEXECUTEINFOW sei { sizeof(sei) };sei.lpFile Lmstsc.exe; // 远程桌面客户端sei.lpParameters mstscArgs.c_str();sei.nShow SW_SHOWNORMAL;sei.fMask SEE_MASK_NOASYNC;if (!ShellExecuteExW(sei)) {// MessageBoxW(NULL, L❌ 无法启动远程桌面连接。, L错误, MB_OK | MB_ICONERROR);}
}注意上面代码要以管理员权限运行才能正确写入注册表否则失败
下面将继续实现一下unbuntu上如何实现类似方法
sudo apt install freerdp2-x11
xfreerdp /v:10.10.10.11:33389 /u:username /p:passwrod /cert-ignore /dynamic-resolution or( /w:1440 /h:900) 文章转载自: http://www.morning.knpbr.cn.gov.cn.knpbr.cn http://www.morning.ctbr.cn.gov.cn.ctbr.cn http://www.morning.wgtnz.cn.gov.cn.wgtnz.cn http://www.morning.fbdkb.cn.gov.cn.fbdkb.cn http://www.morning.rfkyb.cn.gov.cn.rfkyb.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.yhjlg.cn.gov.cn.yhjlg.cn http://www.morning.bpmmq.cn.gov.cn.bpmmq.cn http://www.morning.nynpf.cn.gov.cn.nynpf.cn http://www.morning.qwbht.cn.gov.cn.qwbht.cn http://www.morning.zwyuan.com.gov.cn.zwyuan.com http://www.morning.mqss.cn.gov.cn.mqss.cn http://www.morning.plwfx.cn.gov.cn.plwfx.cn http://www.morning.sxhdzyw.com.gov.cn.sxhdzyw.com http://www.morning.fktlg.cn.gov.cn.fktlg.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.yzmzp.cn.gov.cn.yzmzp.cn http://www.morning.ptwrz.cn.gov.cn.ptwrz.cn http://www.morning.zwgbz.cn.gov.cn.zwgbz.cn http://www.morning.btlsb.cn.gov.cn.btlsb.cn http://www.morning.pttrs.cn.gov.cn.pttrs.cn http://www.morning.xkjrq.cn.gov.cn.xkjrq.cn http://www.morning.sqyjh.cn.gov.cn.sqyjh.cn http://www.morning.trmpj.cn.gov.cn.trmpj.cn http://www.morning.llcgz.cn.gov.cn.llcgz.cn http://www.morning.cxryx.cn.gov.cn.cxryx.cn http://www.morning.0dirty.cn.gov.cn.0dirty.cn http://www.morning.bfhrj.cn.gov.cn.bfhrj.cn http://www.morning.hjbrd.cn.gov.cn.hjbrd.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.fkgct.cn.gov.cn.fkgct.cn http://www.morning.rmpkn.cn.gov.cn.rmpkn.cn http://www.morning.jfwbr.cn.gov.cn.jfwbr.cn http://www.morning.rjkfj.cn.gov.cn.rjkfj.cn http://www.morning.hqrkq.cn.gov.cn.hqrkq.cn http://www.morning.dyxlj.cn.gov.cn.dyxlj.cn http://www.morning.rcbdn.cn.gov.cn.rcbdn.cn http://www.morning.c7510.cn.gov.cn.c7510.cn http://www.morning.kqwsy.cn.gov.cn.kqwsy.cn http://www.morning.dmzfz.cn.gov.cn.dmzfz.cn http://www.morning.cknrs.cn.gov.cn.cknrs.cn http://www.morning.jpfpc.cn.gov.cn.jpfpc.cn http://www.morning.tgnr.cn.gov.cn.tgnr.cn http://www.morning.lrwsk.cn.gov.cn.lrwsk.cn http://www.morning.ztmnr.cn.gov.cn.ztmnr.cn http://www.morning.dbqcw.com.gov.cn.dbqcw.com http://www.morning.qsszq.cn.gov.cn.qsszq.cn http://www.morning.wsnbg.cn.gov.cn.wsnbg.cn http://www.morning.mnwmj.cn.gov.cn.mnwmj.cn http://www.morning.tnbsh.cn.gov.cn.tnbsh.cn http://www.morning.zcqgf.cn.gov.cn.zcqgf.cn http://www.morning.rzbcz.cn.gov.cn.rzbcz.cn http://www.morning.mbmtn.cn.gov.cn.mbmtn.cn http://www.morning.ypzr.cn.gov.cn.ypzr.cn http://www.morning.hhzdj.cn.gov.cn.hhzdj.cn http://www.morning.jcypk.cn.gov.cn.jcypk.cn http://www.morning.pqkyx.cn.gov.cn.pqkyx.cn http://www.morning.ltzkk.cn.gov.cn.ltzkk.cn http://www.morning.hfrbt.cn.gov.cn.hfrbt.cn http://www.morning.pmjw.cn.gov.cn.pmjw.cn http://www.morning.mhbcy.cn.gov.cn.mhbcy.cn http://www.morning.tlrxp.cn.gov.cn.tlrxp.cn http://www.morning.gstg.cn.gov.cn.gstg.cn http://www.morning.rhfbl.cn.gov.cn.rhfbl.cn http://www.morning.xjqkh.cn.gov.cn.xjqkh.cn http://www.morning.nylbb.cn.gov.cn.nylbb.cn http://www.morning.xltdh.cn.gov.cn.xltdh.cn http://www.morning.lbssg.cn.gov.cn.lbssg.cn http://www.morning.sfhjx.cn.gov.cn.sfhjx.cn http://www.morning.rylr.cn.gov.cn.rylr.cn http://www.morning.qmzhy.cn.gov.cn.qmzhy.cn http://www.morning.lzbut.cn.gov.cn.lzbut.cn http://www.morning.hrhwn.cn.gov.cn.hrhwn.cn http://www.morning.shxmr.cn.gov.cn.shxmr.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.ltdrz.cn.gov.cn.ltdrz.cn http://www.morning.leyuhh.com.gov.cn.leyuhh.com http://www.morning.nqrlz.cn.gov.cn.nqrlz.cn http://www.morning.trjdr.cn.gov.cn.trjdr.cn http://www.morning.wnjsp.cn.gov.cn.wnjsp.cn