Only the Chinese version of this page is provided currently. The English version will be provided soon.

SDK 初始化

Last updated: 2026-11-11 10:50:32
本文将为您介绍如何初始化终端性能监控 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;
}
Bugly::BuglyConfig buglyConfig;
buglyConfig.size_ = sizeof(Bugly::BuglyConfig);
buglyConfig.handler_ = handlerPath.c_str();
buglyConfig.database_ = reportsDir.c_str();
buglyConfig.metrics_dir_ = metricsDir.c_str();
buglyConfig.app_id_ = BUGLY_APP_ID;
buglyConfig.app_key_ = BUGLY_APP_KEY;
buglyConfig.app_version_ = L"3.18.0.2";
buglyConfig.bundle_id_ = BUGLY_APP_BUNDLE_ID;
buglyConfig.app_name_ = APP_NAME;
buglyConfig.app_disp_name_ = APP_DISP_NAME;
buglyConfig.build_id_ = L"123.456.789";
buglyConfig.account_id_ = BUGLY_INIT_USER_ID; /// 这里是初始化时的默认userid,业务注册bugly时可能还没有登录,这里推荐业务自己保存上一次登录的userid填到这里。
buglyConfig.deviceId_ = nullptr;

buglyConfig.is_server_process_ = false; // 如果是普通app进程,传false,如果是系统服务进程,传true
buglyConfig.server_host_type_ = kBuglyServerHostTypeTencentCloud;
int bugly_init_result = bugly_agent->InitBuglyManager3(&buglyConfig);
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();
}
}