【vcpkg】cpprestsdk编译链接及实战 Chrome Devtool Protocol

导读

关于Chrome Devtool Protocol的介绍,可以参考文章 《[自动化] PyChromeDevTools源码分析 https://blog.csdn.net/kinghzking/article/details/122650766》,之前用的Python写的一些工具,现在有需求要使用到C++版本的,国际惯例,github走一走。

这里用到一个github搜索技巧Chrome Devtool Protocol language:c++,用来指定编程语言(指定语言为c语言是没有合适结果的),其对应的效果就是跳转到了https://github.com/search?q=Chrome+Devtool+Protocol+language%3Ac%2B%2B&type=Repositories的URL。
在这里插入图片描述

开发环境

版本号描述
操作系统Win11-21H2内部版本号22000.588
Visual Studio 201916.11.12
Windows SDK10.0.17763.0

编译

Chrome Devtool Protocol中使用了websockets。所以,

  • 命令行用的是:vcpkg install cpprestsdk[websockets]:x86-windows-static --recurse
  • 命令行用的是:vcpkg install cpprestsdk[websockets]:x86-windows-static --recurse
  • 命令行用的是:vcpkg install cpprestsdk[websockets]:x86-windows-static --recurse

如果只是使用命令行vcpkg install cpprestsdk:x86-windows-static的话,会报下面的错误Error | LNK2019 | unresolved external symbol

实战

引入cpprest头文件和命名空间

#include <cpprest\containerstream.h>
#include <cpprest\filestream.h>
#include <cpprest\http_client.h>
#include <cpprest\json.h>
#include <cpprest\producerconsumerstream.h>
#include <cpprest\details\web_utilities.h>
#include <cpprest\uri.h>
#include <cpprest\streams.h>
#include <cpprest\ws_client.h>
#include <cpprest\ws_msg.h>
#include <cpprest\asyncrt_utils.h>

using namespace ::pplx;
using namespace utility;
using namespace web;
using namespace concurrency::streams;
using namespace web::http;
using namespace web::http::client;
using namespace web::json;
using namespace web::websockets::client;

引入库文件

#ifdef _DEBUG
#define MY_COMMON_LIB_TYPE "d"
#define MY_BOOST_LIB_TYPE "-mt-gd"
#define MY_POCO_LIB_TYPE "mtd"
#else
#define MY_COMMON_LIB_TYPE ""
#define MY_BOOST_LIB_TYPE "-mt"
#define MY_POCO_LIB_TYPE "mt"
#endif

#pragma comment(lib, "Winhttp.lib")
#pragma comment(lib, "Crypt32.lib")
#pragma comment(lib, "brotlicommon-static.lib")
#pragma comment(lib, "brotlidec-static.lib")
#pragma comment(lib, "brotlienc-static.lib")
#pragma comment(lib, "cpprest_2_10" MY_COMMON_LIB_TYPE ".lib")

引入别人写好的IChromeLink类

下载https://github.com/kirino17/chromelink的代码,找到下面文件,引入自己的项目。

  • IChromeLink.h
  • IChromeLink.cpp

编写测试代码

  • 重载IChromeTabInfo的operator IString()函数。
	struct IChromeTabInfo {
		//标签描述
		IString strDescription;

		//Web调试地址
		IString strDevToolsFrontendUrl;

		//标签ID
		IString strId;

		//标签标题
		IString strTitle;

		//标签类型
		IString strType;

		//地址
		IString strUrl;

		//WS调试地址
		IString strWebSocketDebuggerUrl;

        operator IString() const
		{
            // std::cout << "[operator std::string()]";
            return _T("[] strId: ") + strId
                + _T("\n\t    strTitle: ") + strTitle
                + _T("\n\t    strUrl  : ") + strUrl
                + _T("\n\t    strWebSocketDebuggerUrl: ") + strWebSocketDebuggerUrl
                + _T("\n\t    strDevToolsFrontendUrl : ") + strDevToolsFrontendUrl
				;
        }
	};
  • 测试:打印所有tab页面
int foobar_IChromeLink()
{
	LPCTSTR  szTraceAddr = _T("localhost:9829");
    Komari::IChromeCurTabs curTabs;
    if (Komari::IChromeLink::GetTabs(szTraceAddr, curTabs) == FALSE)
    {
        return 1;
    }

	for (auto item : curTabs)
	{
		MyLogD("%s \n", ls_common::string_helper::w2c(Komari::IString(item).data()).data());
	}
    
    // Komari::IChromeLink cdp();
}

测试效果

通过命令行cefclient.exe --remote-debugging-port=9829打开一个可调式的端口9829,然后我们如下图打开三个网页。
cefclient
然后我们分别通过浏览器和程序分别查看页面列表,结果如下图所示,均能正常显示三个调试页面:
在这里插入图片描述

ps: 页面排序是按队列的方式,新打开一个页面,会在队列的前端插入新页面。

文章小结

Error | LNK2019 | unresolved external symbol

  • 命令行用的是:vcpkg install cpprestsdk[websockets]:x86-windows-static --recurse。Chrome Devtool Protocol中使用了websockets,如果只是使用命令行vcpkg install cpprestsdk:x86-windows-static的话,会报下面的错误。
  • Error | LNK2019 | unresolved external symbol "__declspec(dllimport) public: void __cdecl web::websockets::client::details::websocket_client_task_impl::close_pending_tasks_with_error(class web::websockets::client::websocket_exception const &)" (__imp_?close_pending_tasks_with_error@websocket_client_task_impl@details@client@websockets@web@@QEAAXAEBVwebsocket_exception@345@@Z) referenced in function __catch$??R<lambda_c8bcff768c1b7a6664919177f490ce2a>@@QEBAXV?$task@X@Concurrency@@@Z$0
  • 这个问题找了很久,资料很少,最后尝试在github上的Issues中定位找到了解决方案。其实想想,这个报错就是说少编译了东西,而且跟websocket相关,其他解决方案压根跟这个问题就没有关系,多思考,能少走弯路。
    在这里插入图片描述

文件占用错误

  • 遇到错误,多看日志,而且不要盯着标红的日志,下面的标红日志没有任何意义。在这里插入图片描述

参考资料

**ps:**文章中内容仅用于技术交流,请勿用于违规违法行为。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜猫逐梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值