Re: NativeCall interface for a char ** argument
On Wed, May 25, 2016 at 8:50 PM, Brandon Allberywrote: > > On Wed, May 25, 2016 at 2:41 PM, Fernando Santagata < > nando.santag...@gmail.com> wrote: > >> >> gbooleannotify_get_server_info (char **ret_name, >> char **ret_vendor, >> char **ret_version, >> char **ret_spec_version); >> >> I think that the four arguments return the information about the system. >> The problem here is that I'm not an expert on NativeCall, which I'm just >> beginning to love, and I don't know how to map those char **. >> >> What I concocted on the Perl6 side so far is this: >> >> sub notify_get_server_info(Str $name is rw, Str $vendor is rw, Str >> $version is rw, Str $spec_version is rw) returns Bool is native('notify', >> v4) is export {}; >> >> The calling program goes like this: >> >> my Str ($name, $vendor, $version, $spec_version); >> if notify_get_server_info($name, $vendor, $version, $spec_version) { >> say $name, $vendor, $version, $spec_version; >> }else{ >> say 'No server info'; >> } >> >> and it prints >> >> (Str)(Str)(Str)(Str) >> >> So, I get that the mapping is wrong. >> > > In this case, you need to pass a Pointer[Str] which will be filled in by > the C function. Just using a Str directly passes a (char *), not a (char > **), and you ended up with a pointer where rakudo expected characters. > I rewrote the interface like this: sub notify_get_server_info(Pointer[Str] $name, Pointer[Str] $vendor, Pointer[Str] $version, Pointer[Str] $spec_version) returns Bool is native('notify', v4) is export {}; skipping the "rw" role. I can't declare the variables as Str, because I would get a mismatch error, but I got this result: (Any)(Any)(Any)(Any) Declaring those variables as Pointer[Str] I get: (NativeCall::Types::Pointer[Str])(NativeCall::Types::Pointer[Str])(NativeCall::Types::Pointer[Str])(NativeCall::Types::Pointer[Str]) Should I try to define a class with the repr('CPointer') role to encapsulate that variable type, or am I off tracks? -- Fernando Santagata
Re: NativeCall interface for a char ** argument
On Wed, May 25, 2016 at 2:41 PM, Fernando Santagata < nando.santag...@gmail.com> wrote: > > gbooleannotify_get_server_info (char **ret_name, > char **ret_vendor, > char **ret_version, > char **ret_spec_version); > > I think that the four arguments return the information about the system. > The problem here is that I'm not an expert on NativeCall, which I'm just > beginning to love, and I don't know how to map those char **. > > What I concocted on the Perl6 side so far is this: > > sub notify_get_server_info(Str $name is rw, Str $vendor is rw, Str > $version is rw, Str $spec_version is rw) returns Bool is native('notify', > v4) is export {}; > > The calling program goes like this: > > my Str ($name, $vendor, $version, $spec_version); > if notify_get_server_info($name, $vendor, $version, $spec_version) { > say $name, $vendor, $version, $spec_version; > }else{ > say 'No server info'; > } > > and it prints > > (Str)(Str)(Str)(Str) > > So, I get that the mapping is wrong. > In this case, you need to pass a Pointer[Str] which will be filled in by the C function. Just using a Str directly passes a (char *), not a (char **), and you ended up with a pointer where rakudo expected characters. -- brandon s allbery kf8nh sine nomine associates allber...@gmail.com ballb...@sinenomine.net unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net
Re: NativeCall interface for a char ** argument
On Wed, May 25, 2016 at 6:20 PM, Brandon Allberywrote: > On Wed, May 25, 2016 at 11:50 AM, Fernando Santagata < > nando.santag...@gmail.com> wrote: > >> When I write a C program I'm able to call that function and I receive the >> strings, so I guess my problem is just a mapping one. > > > It can also mean a preallocated array of strings, though; C is sloppy > about the difference between pointer-to and array-of. In NativeCall, the > former is Pointer[Str] and the latter is CArray[Str]. From your > description, you want the former. > In this case I'm working on libnotify. The interface to all the core functionality works fine and I can show notifications on screen. Now I'm working on this function: gbooleannotify_get_server_info (char **ret_name, char **ret_vendor, char **ret_version, char **ret_spec_version); I think that the four arguments return the information about the system. The problem here is that I'm not an expert on NativeCall, which I'm just beginning to love, and I don't know how to map those char **. What I concocted on the Perl6 side so far is this: sub notify_get_server_info(Str $name is rw, Str $vendor is rw, Str $version is rw, Str $spec_version is rw) returns Bool is native('notify', v4) is export {}; The calling program goes like this: my Str ($name, $vendor, $version, $spec_version); if notify_get_server_info($name, $vendor, $version, $spec_version) { say $name, $vendor, $version, $spec_version; }else{ say 'No server info'; } and it prints (Str)(Str)(Str)(Str) So, I get that the mapping is wrong. -- Fernando Santagata
Re: NativeCall interface for a char ** argument
On Wed, May 25, 2016 at 11:50 AM, Fernando Santagata < nando.santag...@gmail.com> wrote: > When I write a C program I'm able to call that function and I receive the > strings, so I guess my problem is just a mapping one. It can also mean a preallocated array of strings, though; C is sloppy about the difference between pointer-to and array-of. In NativeCall, the former is Pointer[Str] and the latter is CArray[Str]. From your description, you want the former. Does the C API provide a way to free the result, or is it some static horror that will cause problems in a threaded environment? -- brandon s allbery kf8nh sine nomine associates allber...@gmail.com ballb...@sinenomine.net unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net
Re: NativeCall interface for a char ** argument
In that case it's a pointer to a pointer: since in C a function can't multiple values, when one wants to return two strings, one needs to use several char ** arguments in the function call. The caller passes a reference to a pointer and the function returns the address of a malloc-ed memory area with a return string in it. When I write a C program I'm able to call that function and I receive the strings, so I guess my problem is just a mapping one. On Wed, May 25, 2016 at 5:40 PM, Bennett Toddwrote: > A C char** is an array (of unknown length) of pounters to C strings ( each > of unknown length). > > I don't know NativeCall, can't tell you how it should be declared, but I > hope that highlights the issue. > -- Fernando Santagata