Skip to content

Instantly share code, notes, and snippets.

@PreSoichiSumi
Last active March 22, 2016 12:47
Show Gist options
  • Save PreSoichiSumi/48c41efae28acd298221 to your computer and use it in GitHub Desktop.
Save PreSoichiSumi/48c41efae28acd298221 to your computer and use it in GitHub Desktop.
JNAの使用例. JNAはjavaから簡単にnativeライブラリを呼び出すためのツール. 内部でJNIを使っている
this is title
class CsDllHandler {
public interface IKeywordRun extends Library {
public String KeywordRun(String action, String xpath, String inputData,
String verifyData);
public java.lang.String testtest(Pointer p);
}
private static IKeywordRun INSTANCE =(IKeywordRun) Native.loadLibrary(
"sumi2", IKeywordRun.class);
public void runDllMethod(String action, String xpath, String inputData,
String verifyData) {
/*NativeLibrary.addSearchPath("sumi",
"${projectPath}/bin/x64/Debug");*/
String csResult = INSTANCE.KeywordRun(action, xpath, inputData,
verifyData);
int a[] = new int[2];
a[0] = 100;
a[1] = 500;
Pointer p = new Memory(Native.getNativeSize(int.class)*a.length);
p.write(0, a, 0, a.length);
String res = INSTANCE.testtest(p);
System.out.println(csResult);
System.out.println(res);
}
}
//for jna
plugins {
id 'java' // or 'groovy' Must be explicitly applied
id 'com.github.johnrengelman.shadow' version '1.2.3' //for shadowJar
id 'application' //必須
}
group 'tst'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
mainClassName = 'Main'
repositories {
mavenCentral()
}
dependencies {
compile 'net.java.dev.jna:jna:4.2.1'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
/**
* Created by s-sumi on 2016/03/15.
*/
public class Main {
String picPath="CcEZ9FCUYAEcGrL.jpg";
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)Native.loadLibrary((Platform.isWindows()) ? "msvcrt" : "c", CLibrary.class);
void printf(byte[] format, Object... args);//byte[]として使えば全角文字も扱える.stringをbyte[]で置き換え可能っぽい?
}
public static void main(String[] args) throws Exception{
char[] s=new char[100];
String str="Clibrary-A";
str.getChars(0,str.length()-1,s,0);
CLibrary.INSTANCE.printf(str.getBytes());
}
}
@PreSoichiSumi
Copy link
Author

最初にスペースを入れたファイルを作るとタイトルになるのか・・

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment