Add licensing to a C or C++ app
At the end, your C or C++ app activates a licence key and unlocks its features offline.
From your product page in PermitPlane (app.permitplane.com/ops/vendor/), click Download permitplane.json and Create test key.
1. Install
Download permitplane-c-0.1.0-x86_64-pc-windows-msvc.zip for Windows x86_64. Compare its sha256 with the one in manifest.json. Unpack it, add include/ to your include path and link lib/permitplane:
$zip = ".\permitplane-c-0.1.0-x86_64-pc-windows-msvc.zip"
Get-FileHash -Algorithm SHA256 $zip
Expand-Archive $zip -DestinationPath C:\libs
Rename-Item "C:\libs\$((Get-Item $zip).BaseName)" permitplane2. Activate a licence key
Open the client with the built-in storage, key, clock and HTTPS, then activate:
#include <stdio.h>
#include "permitplane.h"
int main(void) {
pp_client *client = NULL;
/* Reads the config and the saved state. */
pp_status st =
pp_client_open_default("permitplane.json", NULL, &client);
if (st != PP_OK) return 1;
// paste your test key here; in your real app, ask the user for it
st = pp_client_activate_with_key(
client, "PP-7KQ4M-X9T2H-B6WRN-3FJ8D", "Office PC");
if (st != PP_OK)
fprintf(stderr, "activate: %s\n", pp_status_code(st));
/* ... unlock features here (step 3) ... */
pp_client_free(client);
return 0;
}3. Unlock features in your app
Turn parts of your app on or off based on what the customer's licence includes. The check reads the signed grant and works offline up to your policy's offline days.
bool can_export = false;
pp_client_has_feature(client, "export", &can_export);
if (can_export) {
enable_export();
} else {
show_upgrade_offer();
}pp_limit seats;
pp_client_feature_value(client, "seats", &seats);
/* seats.unlimited, or seats.value */
char status[512];
size_t len = 0;
pp_client_status(client, status, sizeof status, &len);
/* {"status":"paid","message":"...","activation_id":"...",...} */4. Keep it current
- Call
pp_client_refreshon start and every hour. - Call
pp_client_persistnow and then and before exit. - Call
pp_client_deactivateto release the device.
Errors
Every function returns a pp_status. pp_status_code gives its short name. pp_client_last_error gives the service's code as JSON.
| Error | What to do |
|---|---|
Status not_a_license_key | The key is mistyped. Ask the user to check it. |
Service code invalid_license_key | The key is unknown or revoked. Ask for the key from the purchase email. |
Service code device_limit | Every device slot is taken. Deactivate another device first. |
Service code license_expired, license_suspended or license_revoked | Show a renew or contact link. |
Status transport | The network failed. Retry later. Unlocked features keep working. |
char err[512];
size_t n = 0;
if (st != PP_OK) pp_client_last_error(client, err, sizeof err, &n);Going further
- Unattended and offline activation: request and response files for air-gapped machines.
- Floating licences: concurrent seats with leases.
- Network requirements: what your customer's firewall must allow.
- Reference: built-in host, your own storage, C++, floating seats, offline files and build settings.
Reference
Package contents
include/permitplane.hand the header-only C++17 wrapperinclude/permitplane.hpp.- The DLL, import library and static library in
lib/. examples/verify_grant.cand a README with exact compiler lines.- The licence and the third-party notices.
- Downloads need no sign-in. On Linux or macOS, check a file with
sha256sum <file>.
Built-in host
pp_client_open_default(config_path, dir, &client) gives the client:
- The saved state and a P-256 installation key, in files protected with DPAPI.
- The folder
%LOCALAPPDATA%\PermitPlane, ordirwhen it is notNULL. Files are named per product. - A monotonic clock that includes sleep, plus the boot id.
- WinHTTP with the system proxy and certificate store. It sends HTTPS only, follows no redirects and accepts at most 1 MiB.
For a folder every user of the machine shares, call pp_client_new_default(config_json, PP_SCOPE_MACHINE, NULL, &client). It uses %ProgramData%\PermitPlane, open only to SYSTEM, Administrators and the creating user.
In an installer, create a folder per product first and pass it as dir:
$dir = "$env:ProgramData\PermitPlane\Acme\Widget"
New-Item -ItemType Directory -Force $dir | Out-Null
icacls $dir /inheritance:r `
/grant:r "*S-1-5-18:(OI)(CI)F" "*S-1-5-32-544:(OI)(CI)F"- A folder or file that is a link, or that other accounts can open, fails with
host.
Use your own storage or HTTP
Fill one pp_host and pass it to pp_client_new. Your functions take the arguments shown in permitplane.h.
pp_host host = {
.struct_size = sizeof(pp_host),
/* handed back to every callback; must outlive the client */
.user_data = &my_app,
/* load saved licence state from disk */
.state_load = my_state_load,
/* save licence state in protected storage */
.state_save = my_state_save,
/* delete the saved state */
.state_clear = my_state_clear,
/* create or load this device's P-256 key */
.key_init = my_key_init,
/* sign requests with this device's private key */
.key_sign = my_key_sign,
/* monotonic time, so clock changes can't extend a licence */
.clock_observe = my_clock,
/* send one HTTPS request; never follow redirects */
.http_send = my_http,
};
pp_client *client = NULL;
pp_status st = pp_client_new(config_json, &host, &client);- The state holds a bearer installation token and the signed grant.
key_initreturns the public key as 64 bytes:xtheny.key_signwrites 64 bytes:rthens.- Set
confident = 0in the clock sample if your clock skips suspend time or has no boot id. - Headers are plain
Name: value\r\nblocks in both directions. - Return the server's nonce response header unchanged in the response.
- The library adds the request proof, the nonce retry and the idempotency key.
- It calls only the PermitPlane licensing endpoints on your origin.
Buffers and memory
- Text outputs use your buffer.
- To size one, call with
buf = NULL, cap = 0, then call again. - The only memory you free is the client, with
pp_client_free.
Status values
pp_client_status returns one of free, trial, paid, online_required, expired, recovery_required or seat_unavailable.
- After a refresh, a suspended, expired or revoked licence shows in the status.
- A revoked or released activation also clears the saved state.
pp_client_last_errorreturns{"code":...,"http_status":...,"message":...,"retry_after":...}.
Threading
- You can share one client between threads.
- The library serialises calls, so a feature check can wait for another thread's request.
- A host callback must not call into the same client. That call fails with
busy.
C++
#include <iostream>
#include "permitplane.hpp"
auto client =
permitplane::Client::open_default("permitplane.json");
try {
client.activate_with_key("PP-7KQ4M-X9T2H-B6WRN-3FJ8D",
"Office PC");
} catch (const permitplane::Error& e) {
std::cerr << e.code() << " " << e.detail() << "\n";
}
bool can_export = client.has_feature("export");- Methods:
refresh(),persist(),deactivate(),feature_value(),status_json()andlast_error_json(). Error::code()is the status name.Error::detail()holds the service's JSON.- For your own storage, implement
permitplane::Host(seepermitplane.hpp) and pass it topermitplane::Client(config_json, host). - The C boundary catches exceptions from your host methods. They surface as
hosterrors. - The host object must outlive the client.
Floating seats
A floating licence holds a seat as a lease. The library has no timers, so you renew the lease yourself.
/* 0 = online lease; 1 to 504 = offline borrow, in hours */
if (pp_client_checkout_seat(client, 0) == PP_OK) {
/* status JSON: "lease":{"kind":"online","ttl_seconds":598,...},"heartbeat_in_seconds":299 */
/* From your timer, heartbeat_in_seconds after reading the status: */
pp_client_renew_seat(client);
}
/* Give the seat back early. A borrow cannot be returned. */
pp_client_release_seat(client);- No free seat:
checkout_seatfails withapiand the service codeseats_exhausted. - Try again after
retry_afterseconds frompp_client_last_error. seats_exhaustedandlease_requiredset the status toseat_unavailable.- The next successful checkout sets it back to
paid. lease_borrowedandnot_floatingchange nothing.- C++:
checkout_seat(),renew_seat()andrelease_seat().
Offline activation
char req[PP_OFFLINE_REQUEST_MAX_LEN];
size_t req_len = 0;
pp_client_create_offline_request(client, PP_OFFLINE_ACTIVATE,
"Lab PC", req, sizeof req, &req_len);
/* Carry req to the vendor console. Bring the response back. */
pp_status ok = pp_client_import_offline_response(client, response);std::string file = client.create_offline_request(
permitplane::Client::OfflineKind::Activate, "Lab PC");
client.import_offline_response(response_text);- The client accepts only the response to its pending request.
- The response must match this installation key.
- Rejections:
request(another request),installation(another machine),import_expired(too late). - A rejected file stores nothing.
PP_OFFLINE_DEACTIVATEends local rights at once. Its file frees the seat when the vendor processes it.
Build
add_executable(app main.c)
target_include_directories(app PRIVATE
C:/libs/permitplane/include)
target_link_libraries(app PRIVATE
C:/libs/permitplane/lib/permitplane.lib) # .a file on Linux
if(WIN32)
target_link_libraries(app PRIVATE bcrypt advapi32 kernel32 ntdll
userenv ws2_32 dbghelp crypt32 winhttp)
else()
target_link_libraries(app PRIVATE pthread dl m)
endif()- On Windows, build with the dynamic runtime (
/MD). - The static library needs
crypt32andwinhttpfor the built-in host. - For the shared library, link
permitplane.dll.liband shippermitplane.dll. - The archive README has the exact
clandcclines.
Good to know
- The library never stores the licence key.
- You can safely retry an activation with the same key and device name after a lost response.
pp_client_persistsaves the clock high-water mark.pp_client_deactivatealso clears the local state.