On Wednesday, 1 July 2020 at 05:47:16 UTC, Anthony wrote:
On Wednesday, 1 July 2020 at 05:33:48 UTC, H. S. Teoh wrote:
On Wed, Jul 01, 2020 at 05:04:28AM +0000, Anthony via Digitalmars-d-learn wrote: [...]
auto str_utf8 = str.toUTF8();
bson_error_t error

auto bson = bson_new_from_json(cast(const uint8_t*)str_utf8.ptr, -1,
&error);


I get a "Program exited with code -11" message.
Does anyone know what I'm doing wrong?

D strings are generally not null-terminated (except for literals). Before passing them to a C function you need to add a trailing null. Try using std.conv.toStringz instead of casting the pointer yourself.


T

Thanks H. S. Teoh.
Hmm, still same result though.


    import std.string;

    auto str = toStringz("{\"a\":1}");

    bson_error_t error;

    bson_new_from_json(str, -1, &error);




    extern(C) {
        ...
bson_t* bson_new_from_json(const char* data, long len, bson_error_t* error);
    }

Noob mistake:
I declared an array that should be of fixed size.

    struct bson_error_t {
        ....
        char[] message;
    };

Should be:

    struct bson_error_t {
        ....
        char[504] message;
    };

:/

Reply via email to