[Ecls-list] Accessing C variables
Goffioul Michael
goffioul at imec.be
Wed Sep 14 02:05:18 UTC 2005
> Bit long in coming, but thx alot Michael for your advice.
> Much appreciated.
> I have made a fair bit of progress with things.
> Still having a few issues, but here is an example of what
> worked for me.
> If there is a better way, hit me with it :)
>
> In my C++ app:
>
> char *string_ptr = "hi there";
> cl_object f_obj =
> ecl_make_foreign_data(make_simple_string("CSTRING"),
> strlen(string_ptr), (void*)&string_ptr);
> cl_set(c_string_to_object("TEST"), f_obj);
>
> // then load and execute below script.
>
> In Lisp (interpreted):
>
> ;; This is how I cld see the string "hi there".
> (deref-pointer test :cstring)
>
> To be honest, this WAS working, then I did something (?:) and now it
> won't work (for now).
> But when it was kicking over, I could create pointers to
> "int" or whatever.
> The thing that threw me for a bit was that for c-strings, you had to
> feed cl_make_foreign_data a pointer to the char pointer.
To pass strings, I'd use something like this:
in the C file -> ecl_make_foreign_data(c_string_to_object("(* :char)"), strlen(string_ptr)+1, string_ptr);
in the LISP file -> (ffi:convert-from-foreign-string test :null-terminated-p t)
Note: in your above code, the size of the foreign-data object should be 1;
indeed, what you create is a pointer to a single :cstring object.
Michael.
More information about the ecl-devel
mailing list