SDK 初始化

最近更新时间:2025-12-19 15:00:12

我的收藏
本文将为您介绍如何初始化终端性能监控 Pro Windows SDK。

操作步骤

1. 参考 BuglyDemo 工程,设置不同构建配置下的外部包含目录库目录
2. 链接器 > 输入 > 附加依赖项中添加 bugly_logger.lib
3. BuglySDK\\lib\\x64_release 目录中的 client_extension.dll、crashpad_handler.exe、crashpad_handler_extension.exe、bugly_logger.dll、bugly_extra_handler.dll 文件拷贝到业务应用 exe 的同目录下。


示例代码

参考以下代码初始化 SDK,建议您尽早完成初始化,这样才能及时捕获异常。
/// Bugly 相关头文件
#include "bugly_agent.h"

/// appid 和 appkey,去这里创建产品
/// https://cloud.tencent.com/login?s_url=https%3A%2F%2Fcloud.tencent.com%2F
#define BUGLY_APP_ID L"your app id"
#define BUGLY_APP_KEY L"your app key"
#define BUGLY_APP_BUNDLE_ID L"com.your.app"

/// <summary>
/// 设置 Bugly 工作配置目录,Bugly 捕获到异常后生成数据会记录在这里
/// </summary>
std::wstring GenBuglyDataDirW() {
wchar_t tmpPathBuffer[1024] = { 0 };
::GetTempPath(1024, tmpPathBuffer);
std::wstring result = tmpPathBuffer;
return result + L"BuglyDemo";
}

/// <summary>
/// 获取 crashpad_handler.exe 所在的路径
/// </summary>
std::wstring GenHandlerPathW() {
std::wstring path = getExecutableDir();
path += L"\\\\crashpad_handler.exe";
return path;
}

bool RegisterBuglyExtraHandlerImpl(const wchar_t* path) {
HMODULE hMod = ::LoadLibraryW(L"client_extension.dll");
if (NULL == hMod) {
return false;
}

RegisterBuglyExtraHandlerT fun_ptr = (RegisterBuglyExtraHandlerT)::GetProcAddress(hMod, "RegisterBuglyExtraHandler");
if (nullptr == fun_ptr) {
return false;
}

return fun_ptr(path);
}

/// <summary>
/// crash 回调函数,bugly 捕获到 crash 后,会先回调给业务
/// 业务可以在这里添加一些日志,连同 crash 一起上报到 bugly 后台
/// </summary>
void CrashCallback(IBuglyAgent* agent, void* context) {
if (nullptr == agent) {
return;
}

std::wstring log_path = L"C:\\\\your_apps_log_path\\\\your_log_file.log";
agent->SetLogFilePath(GetCurrentProcessId(), L"Log1", 4, log_path.c_str(), log_path.size());
}

/// <summary>
/// 初始化 Bugly,用于监控和上报 crash
/// </summary>
bool InitBugly() {
std::wstring handler = GenHandlerPathW();
std::wstring reportsDir = GenBuglyDataDirW();

// 如果是监控 windows 系统服务进程,这个改成 true,普通程序为 false
bool bIsServerProcess = false;
std::wstring metricsDir = reportsDir;
wchar_t pipe_name[MAX_PATH] = {0};
BuglyAgentResult result = CreateBuglyAgent(&bugly_agent);
if (result != BuglyAgentResult::kSuccess || bugly_agent == nullptr) {
return false;
}

int bugly_init_result = bugly_agent->InitBuglyManager2(handler.c_str(),
reportsDir.c_str(),
metricsDir.c_str(),
BUGLY_APPID,
BUGLY_APPKEY,
L"3.16.0.2",
BUGLY_BUNDLE_ID,
APP_NAME,
APP_DISP_NAME,
"123.456.789",
true, // is_reuse_bugly_server
5000,
true, // is_pop_dialog
true, // is_need_attach_info
true, // is_need_upload
false, // is_monitor_self
bIsServerProcess, // is_server_process
L"InitAccountID2", // account_id
nullptr
);
if (bugly_init_result != BUGLY_INIT_NO_ERROR) {
return false;
}

// 设置 userid
bugly_agent->SetKey(L"account_id", L"BuglyDemoUserId");

// 可选,除了 crsah 线程以外,额外将其他所有线程调用栈都报上去。
bugly_agent->SetEnableDumpAllThreadCallstack(true);

// 可选,初始化 Bugly logger
auto buglyLogDir = GenBuglyLogDir();
bugly_agent->LoadBuglyLoggerModule(buglyLogDir.c_str(), nullptr);

// 注册 extra handler,用于监控 strcpy_s 一类安全函数抛出的异常以及虚函数调用 purecall 错误
std::wstring extraHandlerModule = getExecutableDir();
extraHandlerModule += L"\\\\bugly_extra_handler.dll";
RegisterBuglyExtraHandlerImpl(extraHandlerModule.c_str());
bugly_agent->EnablExtraHandler(true, extraHandlerModule.c_str());

// 设置 crash 回调
bugly_agent->SetCrashCallback(CrashCallback, nullptr);

// minidump 收集 indirectlyRefMemory, 第二个参数为额外收集内存的大小限额,不填的话默认就是10MB
bugly_agent->SetEnableGatherIndirectlyReferencedMemory(true);

// 设置渠道
bugly_agent->SetKey("app_channel", "MyChannel");

// 设置进程名
std::string pname_key = "procname_by_procid_";
pname_key += std::to_string(::GetCurrentProcessId());
bugly_agent->SetKey(pname_key.c_str(), "My ProcessName_by_pid");

return true;
}

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

// TODO: 在此处放置代码。

// 初始化全局字符串
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_DEMO, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
....

/// 初始化 Bugly
bool bugly_init_result = InitBugly();
// 如果第一次失败,再重试一次(很重要)
if (!bugly_init_result) {
bugly_init_result = InitBugly();
}
}