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
2
votes
1 answer

Does Perl 6 nativecast() to an object with repr('CPointer') DESTROY when GC'ed?

Reading Basic use of Pointers indicates that when a NativeCall C function returns pointer to an object with a class with repr('CPointer'), it will call submethod DESTROY where I can put my function to free the C memory. (This is fantastic and an…
Curt Tilmes
  • 3,035
  • 1
  • 12
  • 24
1 2 3 4
5