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

郎咸武 langxianzhe@REDACTED
Wed Dec 14 17:25:11 CET 2011


hi, Gleb Peregud, Thanks to  your help. My quest is solved.
 56     ErlNifBinary input;
 57     if(!enif_inspect_iolist_as_binary(env, argv[0], &input)) {
 58      return enif_make_badarg(env);
 59     }
 60     char *y="yy";
 61     unsigned char* a=input.data;
 62     int i=0;
 63     //test

 64     while(i<input.size){
 65         fprintf(stderr,"a=%i,", a[i]);
 66         i++;
 67         //fprintf(stderr,"a=%i\n\r", *a);
 68         //a++;
 69     }
 70     fprintf(stderr,"input.size=%i\n\r", input.size);
 71     fprintf(stderr,"input.data=%s\n\r", input.data);
 72     //return enif_make_string(env, a, ERL_NIF_LATIN1);
 73     return enif_make_string_len(env, a, i,ERL_NIF_LATIN1);


Thanks agin.

from jason
2011/12/14 Gleb Peregud <gleber.p@REDACTED>

> 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.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111215/a7adebfb/attachment.htm>


More information about the erlang-questions mailing list