Questions tagged [nativecall]

Raku mechanism for calling code from installed C libraries. The following is a complete example that uses installed C libraries on Windows to open up a message box: use NativeCall; sub MessageBoxA(int32, Str, Str, int32) returns int32 is native('user32') { * } MessageBoxA(0, "We have NativeCall", "ohai", 64);

Raku mechanism for calling code from C libraries. The following examples are from https://docs.perl6.org/language/nativecall.

Function without arguments:

use NativeCall;
sub some_argless_function() is native('something') { * }
some_argless_function();

The first line imports various traits and types. The next line looks like a relatively ordinary Perl 6 sub declaration—with a twist. We use the "native" trait in order to specify that the sub is actually defined in a native library. The platform-specific extension (e.g., '.so' or '.dll'), as well as any customary prefixes (e.g., 'lib') will be added for you.

The first time you call "some_argless_function", the "libsomething" will be loaded and the "some_argless_function" will be located in it. A call will then be made. Subsequent calls will be faster, since the symbol handle is retained.

Function that takes and returns typed arguments:

use NativeCall;
sub add(int32, int32) returns int32 is native("calculator") { * }

Example on Windows:

use NativeCall;

sub MessageBoxA(int32, Str, Str, int32) returns int32 is native('user32') { * }

MessageBoxA(0, "We have NativeCall", "ohai", 64);
61 questions
5
votes
2 answers

Passing pointer to pointer in Perl 6 NativeCall

I'm trying to use NativeCall to interact with some C functions. For one case, I need to pass in pointers that get updated by the function, so it wants a pointer to a pointer, 'void **'. I tried it like this: class Foo { has Pointer $.first; …
Curt Tilmes
  • 3,035
  • 1
  • 12
  • 24
5
votes
1 answer

Raku NativeCall and C source files

What the best strategy to release a Raku binding for C library using NativeCall for both windows and Linux? Does the developer need to compile both the .dll and .so files and upload them with the raku code to github? Or there are an option on raku…
smith
  • 3,232
  • 26
  • 55
4
votes
3 answers

How do I "assign" the value in `CArray` that contains a memory address to a `Pointer`?

This is a NativeCall question. I have 8 bytes (little endian) in a CArray representing a memory address. How do I create a Pointer out it? (CArray and Pointer are two of NativeCall's C compatible types. Pointers are 8 bytes long. Things should line…
Todd
  • 976
  • 4
  • 10
4
votes
1 answer

What's the minimum code required to make a NativeCall to the md_parse function in the md4c library?

Note: This post is similar, but not quite the same as a more open-ended questions asked on Reddit: https://www.reddit.com/r/rakulang/comments/vvpikh/looking_for_guidance_on_getting_nativecall/ I'm trying to use the md4c c library to process a…
StevieD
  • 6,925
  • 2
  • 25
  • 45
4
votes
0 answers

cglobal() behaves differently when used with a "my" or "our" variable

I found out something I cannot explain, so I golfed it to what I think is the bare minimum: # file OurTest.rakumod unit module OurTest; use NativeCall; our $our-errno is export := cglobal('libc.so.6', 'errno', int32); # note the "our" # file…
Fernando Santagata
  • 1,487
  • 1
  • 8
  • 15
4
votes
1 answer

Raku: How do I assign values to CArray[WCHAR]?

$ raku -v This is Rakudo version 2019.07.1 built on MoarVM version 2019.07.1 The following was done on Raku REPL. What am I doing wrong here? How do I assign values to CArray[WCHAR]? I want $lpData[0] to be 0xABCD and $lpData[1] to be…
Todd
  • 976
  • 4
  • 10
4
votes
1 answer

Using void structs in Raku via NativeCall

I'm trying to link libzip to Raku, and it uses a void struct or a struct with no body, like this: struct zip; typedef struct zip zip_t; I declare it in my Raku program in the same way: class zip_t is repr('CStruct'){}; This fails with: Class zip_t…
jjmerelo
  • 22,578
  • 8
  • 40
  • 86
4
votes
1 answer

How can I correctly use libXL from Perl 6 using NativeCall?

I try to use libXL from Perl 6 (latest version) with NativeCall. I can't get utf-8 to correctly save the created xlsx file. Only CArray[uint16] seems to work nor Str is encoded('utf8') nor CArray[uint8]. Best result is saved workbook, sheet name and…
HanTyr
  • 43
  • 1
  • 4
4
votes
1 answer

How to mitigate a bug in Rakudo with NativeCall?

I want to be able to use a double pointer in a class with REPR CStruct/CPointer: typedef struct CipherContext { void *cipher; const uint8_t *key; size_t key_len; const uint8_t *path; size_t path_len; …
Kaiepi
  • 3,230
  • 7
  • 27
4
votes
2 answers

NativeCall loading a library symbol I don't call

I have two libraries, I want to call routines in the first library, they then call routines in the second library, but crash because those symbols are undefined. Is it possible to say "load these symbols" from library XX even though I don't want to…
Curt Tilmes
  • 3,035
  • 1
  • 12
  • 24
4
votes
1 answer

Putting Function Pointers in a Perl6 NativeCall CStruct

Trying to interface with a C library that takes a struct with a bunch of pointers to functions it calls at various points. something like this: struct callbacks { int (*foo)(int); int (*bar)(int); } int set_callbacks(callbacks *cbs); I can…
Curt Tilmes
  • 3,035
  • 1
  • 12
  • 24
4
votes
1 answer

ncurses: why doesn't getch wait until I press a key?

From the ncurses(3) linux man page: The nodelay option causes getch to be a non-blocking call. If no input is ready, getch returns ERR. If disabled (bf is FALSE), getch waits until a key is pressed. Why doesn't in my example getch wait until I…
sid_com
  • 24,137
  • 26
  • 96
  • 187
3
votes
2 answers

Incorporate C library function into Perl6 with NativeCall

I am attempting to use lgamma from C's math.h in Perl6. How can I incorporate this into Perl6? I have tried use NativeCall; sub lgamma(num64 --> num64) is native(Str) {}; say lgamma(3e0); my $x = 3.14; say lgamma($x); This works for the first…
con
  • 5,767
  • 8
  • 33
  • 62
3
votes
3 answers

NativeCall. How to get a string as a parameter of a C function

There is a C function which returns some string to a provided pointer: void snmp_error(netsnmp_session *sess, int *clib_errorno, int *snmp_errorno, char **errstring); The Perl6 version is: sub snmp_error(Snmp-session,…
Yuriy Zhilovets
  • 400
  • 2
  • 10
3
votes
1 answer

NativeCall code for using Posix forking and piping is not working

OK, so I'm new to Perl, and Perl 6. I thought I'd see if I could get forking and piping working, but so far have been unable to. Here's my Perl 6 code: use NativeCall; # http://www.perlmonks.org/?node_id=989766 our sub c_close(int32) returns int32…
blippy
  • 1,490
  • 1
  • 13
  • 22