桌面软件账号登录与功能保护代码示例
先从后台复制对应语言的连接函数,再把下面的片段接到已有项目。邮箱与密码来自登录界面,export 必须换成策略中真实存在的功能名。每个片段只展示一次操作;实际应用在整个运行期间保留同一个客户端,并在退出时关闭。
Go
app, err := connectLaunch()
if err != nil { return err }
defer app.Close()
if _, err := app.Login(ctx, email, password); err != nil { return err }
return app.RunFeature("export", func() error {
return exportReport()
})Python
with connect_launch() as app:
app.login(email, password)
result = app.run_feature("export", export_report)C# / .NET
await using var app = LaunchConnection.Open();
await app.Login(email, password);
await app.RunFeature("export", async () => {
await ExportReport();
});Java
try (var app = LaunchConnection.open()) {
app.login(email, passwordChars);
app.runFeature("export", () -> exportReport());
}JavaScript / Node.js / TypeScript
const app = connectLaunch();
try {
await app.login(email, password);
const result = await app.runFeature('export', () => exportReport());
} finally {
await app.close();
}Rust
let mut app = connect_launch()?;
app.login(&email, &password)?;
let result = app.run_feature("export", || {
export_report()?;
Ok(())
})?;
app.close();Ruby
app = connect_launch
begin
app.login(email, password)
result = app.run_feature('export') { export_report }
ensure
app.close
endC
static int export_action(void *context) {
return export_report(context); /* Return 1 only on success. */
}
launch_application *app = connect_launch();
if (!app) return 0;
int ok = launch_application_login(app, email, password);
if (ok) ok = launch_application_run_feature(app, "export", export_action, context);
/* Check launch_application_error(app) on failure; never log credentials. */
launch_application_close(app);
return ok;C++
auto app = connectLaunch();
app.Login(email.c_str(), password.c_str());
app.RunFeature("export", [&] { exportReport(); });Go 的 ctx 应使用应用生命周期上下文。Java 密码使用 char[]。C 的功能回调仅在业务成功时返回 1。Node.js / Electron 将 SDK 放在主进程或可信后端;网络请求应避免阻塞界面线程。