[erlang-questions] [Erlang][Request] Invoke C funtion in Erlang

Gleb Peregud gleber.p@REDACTED
Wed Dec 14 14:23:16 CET 2011


2011/12/14 郎咸武 <langxianzhe@REDACTED>:
> Thanks Gleb Peregud.
>
> Your are right. But a new request is come on. I input list [1,7,0,9] as
> parameter. Ii only return [1,7]. The [0,9] is lose. Maybe the '0' elment as
> a NUL in C.
> What  should i do .
>
> Thanks
>
> 8>complex6:get_name([1,7,0,9]).
> [1,7]
> 9>

Most probably enif_get_string does copy all four bytes to the buffer,
but enif_make_string treats NUL as termination mark of a string. Try
using enif_make_string_len instead.

On the other side it looks like you are interested in a binary-like
data, instead of a string-like data. You can make use of the following
code:

ErlNifBinary input;
if(!enif_inspect_iolist_as_binary(env, argv[0], &input)) {
 return enif_make_badarg(env);
}

As the result of this:
input.data - will contain pointer to a memory, where all bytes from
argument are stored
input.size - size of that memory area

Also it's worth noting that this will work for both lists (with
integers less than 256) and binaries, and for any nested combinations
of them.



More information about the erlang-questions mailing list