Created
June 29, 2012 10:07
-
-
Save timyates/3017046 to your computer and use it in GitHub Desktop.
JNA and Groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gcc -o libgreet.so -shared greet.c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gcc -dynamiclib greet.c -o libgreet.dylib |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
void greet( char * name, void * buf, long buflen ) { | |
snprintf( buf, buflen, "Hello %s", name ) ; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
groovy test.groovy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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