Skip to content

Instantly share code, notes, and snippets.

@timyates
Created June 29, 2012 10:07
Show Gist options
  • Save timyates/3017046 to your computer and use it in GitHub Desktop.
Save timyates/3017046 to your computer and use it in GitHub Desktop.
JNA and Groovy
gcc -o libgreet.so -shared greet.c
gcc -dynamiclib greet.c -o libgreet.dylib
#include <stdio.h>
#include <stdlib.h>
void greet( char * name, void * buf, long buflen ) {
snprintf( buf, buflen, "Hello %s", name ) ;
}
groovy test.groovy
@Grab( 'net.java.dev.jna:jna:3.4.0' )
import com.sun.jna.Library
import com.sun.jna.Native
import com.sun.jna.Memory
interface Greet extends Library {
public void greet( String name, Memory buffer, long len )
}
def test = Native.loadLibrary( 'greet', Greet )
// Generate a return buffer
def buf = new Memory( 1024 )
// Call the C function
test.greet( 'Tim', buf, buf.size() )
// Print the result
println buf.getString( 0, false )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment