Skip to content

C and Cpp Integration

v2rayroot edited this page Jun 14, 2026 · 1 revision

C and C++ Integration

Include the generated header from the same release ZIP as the binary.

C Ownership Helper

char *status = GetStatus();
if (status != NULL) {
    printf("%s\n", status);
    FreeCString(status);
}

Start Pattern

char *validation = ValidateConfig(config_json, "{}");
if (validation != NULL) {
    /* Parse JSON before releasing. */
    FreeCString(validation);
}

char *error = Start(config_json, "{}");
if (error != NULL) {
    fprintf(stderr, "%s\n", error);
    FreeCString(error);
}

C++ RAII

#include <memory>
#include <string>

struct V2RootStringDeleter {
    void operator()(char* value) const noexcept {
        FreeCString(value);
    }
};

using V2RootString = std::unique_ptr<char, V2RootStringDeleter>;

std::string status() {
    V2RootString value(GetStatus());
    return value ? value.get() : "";
}

Never unload the library while the runtime or an FFI call is active.

Clone this wiki locally