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
6
votes
2 answers

Calling GSL functions via NativeCall in Raku throws error

Problem I am trying to call cumulative distribution function of chisq function in GSL from raku. This is my raku script chisq.raku #Calling gsl_cdf_chisq-P function in GSL from raku use NativeCall; sub gsl_cdf_chisq_P(num64, num64) returns num64…
Suman Khanal
  • 3,079
  • 1
  • 17
  • 29
6
votes
2 answers

How to write a Raku declaration for a C function returning a whole struct?

I have this C code: typedef struct { double dat[2]; } gsl_complex; gsl_complex gsl_poly_complex_eval(const double c[], const int len, const gsl_complex z); The C function returns a whole struct, not just a pointer, so I cannot write the Raku…
Fernando Santagata
  • 1,487
  • 1
  • 8
  • 15
6
votes
1 answer

Differing output between C++ and NativeCall in Raku

I am trying to write a function for cumulative distribution function taken from here. This is my cpp code: #include #include #include using namespace std; double normalCDF( double x ) { return 0.5 * ( 1.0 + erf(…
Suman Khanal
  • 3,079
  • 1
  • 17
  • 29
6
votes
1 answer

Pass a complex struct to the Windows API

I am trying to use the GetConsoleScreenBufferInfo(HANDLE, PCONSOLE_SCREEN_BUFFER_INFO) function from the Windows API using Perl 6 and (of course) NativeCall. I think I have set up the CONSOLE_SCREEN_BUFFER_INFO struct the function needs correctly,…
Holli
  • 5,072
  • 10
  • 27
6
votes
1 answer

NativeCall can't find a function in Kernel32.dll

I am trying to port this code to Perl6. While I can call GetStdHandle, GetConsoleMode and SetConsoleMode, my script breaks when I try to call ReadConsoleInput: Cannot locate symbol 'ReadConsoleInput' in native library 'Kernel32.dll' in method…
Holli
  • 5,072
  • 10
  • 27
6
votes
1 answer

perl6 NativeCall doesn't find library on Darwin

I'm playing a bit with NativeCall to get familiar with that side of Perl6. Of course, I'm trying to load libstatgrab first (what else?). So I start with easiest part - the host information. Since no cluster support yet, it's just one result - no…
Sno
  • 179
  • 1
  • 7
6
votes
1 answer

Perl6 NativeCall cannot locate symbol in native library

I have a module using NativeCall that works on both Linux and macOS, but not Windows. When I try to use the module on Windows I get lots of errors like: # Cannot locate symbol 'TinyTIFFReader_open' in native library 'tinytiff.dll' I use cmake for…
ryn1x
  • 1,052
  • 2
  • 8
  • 19
6
votes
1 answer

Perl6 NativeCall with Str is encoded('utf16') got randomly corrupted result

I am mapping the GetFullPathName windows API in a perl6 script using NativeCall, for so I wrote the following: #!perl6 use NativeCall; constant \WIN32_MAX_PATH = 260; #I may use directly $path.IO.absolute() sub Win32-GetFullPathName( …
xlat
  • 190
  • 8
6
votes
0 answers

Installing Perl 6 to MSYS2 on Windows 10

I want to develop a GTK+3 app on Windows 10 using Perl 6 and its NativeCall library. I have installed MSYS2 and GTK+3 on it and could run an example GTK+3 C program using it. But I couldn't install Perl 6 on MSYS2. I have tried to search web for…
ismailarilik
  • 2,236
  • 2
  • 26
  • 37
6
votes
2 answers

Passing an array of structures to a Perl 6 NativeCall function

I'm trying to use NativeCall to interact with some C functions. I have a simple C struct, and a function that wants an array of them. struct foo { int x; char *s; }; struct foo foo_array[3]; foo_array[0].x = 12; foo_array[0].s =…
Curt Tilmes
  • 3,035
  • 1
  • 12
  • 24
5
votes
2 answers

Raku NativeCall to LibX11 screen and display

Fedora 33 Raku I am trying to use Raku's NativeCall to talk to libX11.so to print out both my screen and my display: use NativeCall; class Display is repr('CStruct') { has Pointer $.DisplayPtr }; # libX11.so --> X11 sub XOpenDisplay(Str $name =…
Todd
  • 976
  • 4
  • 10
5
votes
2 answers

Raku/Perl6: how do you code a NULL with NativeCall

https://docs.perl6.org/language/nativecall "As you may have predicted by now, a NULL pointer is represented by the type object of the struct type." https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regqueryvalueexw C++ …
Todd
  • 976
  • 4
  • 10
5
votes
2 answers

Getting data out of Native pointers

It's no big deal to get data into Perl 6 Native pointers: sub memcpy( Pointer[void] $source, Pointer[void] $destination, int32 $size ) is native { * }; my Blob $blob = Blob.new(0x22, 0x33); my Pointer[void] $src-memcpy = nativecast(Pointer[void],…
jjmerelo
  • 22,578
  • 8
  • 40
  • 86
5
votes
1 answer

Passing an inlined CArray in a CStruct to a shared library using NativeCall

This is a follow-up question to "How to declare native array of fixed size in Perl 6?". In that question it was discussed how to incorporate an array of a fixed size into a CStruct. In this answer it was suggested to use HAS to inline a CArray in…
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
5
votes
1 answer

Adding user mode types for Perl 6 NativeCall structs

The Perl 6 docs list a bunch of types. Some of them, such as Str, have more complicated box/unbox behaviors. Is it possible to define my own type, specifying my own routines for the box/unboxing? For a particular project, I have a bunch of types…
Curt Tilmes
  • 3,035
  • 1
  • 12
  • 24