El dom, 6 dic 2020 a las 10:09, ToddAndMargo via perl6-users (< perl6-us...@perl.org>) escribió:
> On 12/4/20 8:55 PM, ToddAndMargo via perl6-users wrote: > > On 2020-12-04 20:28, Curt Tilmes wrote: > >> On Fri, Dec 4, 2020 at 10:52 PM ToddAndMargo via perl6-users > >> <perl6-us...@perl.org> wrote: > >> > >>> This is the C way, although it shows deleted printers as well: > >>> > >>> #include <iostream> > >>> #include <cups/cups.h> > >>> > >>> int main() { > >>> cups_dest_t* dests; > >>> int nCount = cupsGetDests2(CUPS_HTTP_DEFAULT, &dests); > >>> > >>> for (int i = 0; i < nCount; i++) { > >>> cups_dest_t dest = dests[i]; > >>> std::cout << dest.name << std::endl; > >>> } > >>> } > >> > >> Roughly translating that into Raku Nativecall (my system has > >> cupsGetDests(), but not cupsGetDests2() -- you'll have to update to > >> that) > >> > >> use NativeCall; > >> class CupsDest is repr('CStruct') { > >> has Str $.name; # This is the first field in the struct -- > >> add more if you need them > >> } > >> > >> sub cupsGetDests(Pointer is rw --> int32) is native('cups') {} > >> > >> my $ptr = Pointer.new; > >> my $nCount = cupsGetDests($ptr); > >> > >> for ^$nCount -> $i { > >> my $dest = nativecast(CupsDest, Pointer.new($ptr + $i * > >> nativesizeof(Pointer))); > >> say $dest.name; > >> } > >> > > > > > > > > Hi Curt, > > > > What am I doing wrong? > > > > #!/usr/bin/env raku > > > > use NativeCall; > > class CupsDest is repr('CStruct') { > > has Str $.name; # This is the first field in the struct -- > > add more if you need them > > } > > > > sub cupsGetDests(Pointer is rw --> int32) is native('cups') {} > > > > my $ptr = Pointer.new; > > my $nCount = cupsGetDests($ptr); > > > > for ^$nCount -> $i { > > my $dest = nativecast(CupsDest, Pointer.new($ptr + $i * > > nativesizeof(Pointer))); > > say $dest.name; > > } > > > > $ ListPrinters.pl6 > > Cannot locate native library 'libcups.so': libcups.so: cannot open > > shared object file: No such file or directory > > in method setup at > > > /opt/rakudo-pkg/share/perl6/core/sources/947BDAB9F96E0E5FCCB383124F923A6BF6F8D76B > > > (NativeCall) line 298 > > in block cupsGetDests at > > > /opt/rakudo-pkg/share/perl6/core/sources/947BDAB9F96E0E5FCCB383124F923A6BF6F8D76B > > > (NativeCall) line 587 > > > > > > $ locate libcups.so > > /usr/lib/libcups.so.2 > > /usr/lib64/libcups.so.2 > > > > > > -T > > I did the following: > > # cd /usr/lib > # gls libcups > libcupsimage.so.2 > libcups.so.2 > > # ln -s libcups.so.2 libcups.so > Why did you do so? ;-) # ls -al libcups* > -rwxr-xr-x. 1 root root 14612 Nov 10 06:07 libcupsimage.so.2 > lrwxrwxrwx. 1 root root 12 Dec 6 01:03 libcups.so -> libcups.so.2 > -rwxr-xr-x. 1 root root 710236 Nov 10 06:07 libcups.so.2 > > # cd /usr/lib64 > # ln -s libcups.so.2 libcups.so > # ls -al libcups* > lrwxrwxrwx. 1 root root 23 Nov 23 22:09 libcupsfilters.so.1 -> > libcupsfilters.so.1.0.0 > -rwxr-xr-x. 1 root root 264440 Nov 23 22:09 libcupsfilters.so.1.0.0 > -rwxr-xr-x. 1 root root 15256 Nov 10 06:08 libcupsimage.so.2 > lrwxrwxrwx. 1 root root 12 Dec 6 01:04 libcups.so -> libcups.so.2 > -rwxr-xr-x. 1 root root 686128 Nov 10 06:08 libcups.so.2 > > > Running the program now gives: > > $ ListPrinters.pl6 > B4350 > (Str) > Segmentation fault (core dumped) > Well, the second $dest clearly has no value in "name". And in the next one you are getting into a loop and casting pointers and stuff without actually checking if they're good. It looks like it's pointing nowhere; either the size is off, or there's any other problem. Cheers JJ -- JJ