📄 jnativetester.java
字号:
package org.xvolks.test;
import static org.xvolks.jnative.misc.MSG.WindowsConstants.SW_SHOW;
import static org.xvolks.jnative.misc.MSG.WindowsConstants.WS_OVERLAPPEDWINDOW;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import javax.swing.JFileChooser;
import org.xvolks.jnative.Convention;
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
import org.xvolks.jnative.logging.JNativeLogger.SEVERITY;
import org.xvolks.jnative.misc.MSG;
import org.xvolks.jnative.misc.basicStructures.HWND;
import org.xvolks.jnative.pointers.NullPointer;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;
import org.xvolks.jnative.pointers.memory.NativeMemoryBlock;
import org.xvolks.jnative.util.Kernel32;
import org.xvolks.jnative.util.User32;
import org.xvolks.test.callbacks.TestCallback;
import org.xvolks.test.callbacks.linux.LinuxCallback;
/**
*
* $Id: JNativeTester.java,v 1.27 2007/05/06 16:49:02 mdenty Exp $
*
* This software is released under the LGPL.
*
* @author Created by Marc DENTY - (c) 2006 JNative project
*/
public class JNativeTester {
private static File searchFile(final String pattern, String root) {
System.out.print("Searching " + pattern + " in " + root);
for (File f : new File(root).listFiles(new FileFilter() {
public boolean accept(File pathname) {
return Pattern.matches(pattern, pathname.getName());
}
})) {
if (f.isFile()) {
System.out.println("... found " + f);
return f;
}
}
System.out
.println("... Not found, sorry cannot demonstrate JNative with "
+ pattern);
return null;
}
public static void main(String[] args) throws NativeException,
IllegalAccessException, InterruptedException, IOException {
System.getProperties().put("jnative.loadNative", "manual");
loadLib();
if (System.getProperty("os.name").toLowerCase().indexOf("linux") != -1) {
JNative.setDefaultCallingConvention(Convention.STDCALL);
Pointer p_asm = new Pointer(MemoryBlockFactory.createMemoryBlock(16));
int cpt=0;
//push ebp
cpt += p_asm.setByteAt(cpt, (byte)0x55);
// //push esp
cpt += p_asm.setByteAt(cpt, (byte)0x54);
// //lea eax, [esp+0x0c]
cpt += p_asm.setIntAt(cpt, 0x0c24448d);
cpt += p_asm.setByteAt(cpt, (byte)0x31); // xor eax, eax
cpt += p_asm.setByteAt(cpt, (byte)0xc0);
cpt += p_asm.setByteAt(cpt, (byte)0x66); // inc eax
cpt += p_asm.setByteAt(cpt, (byte)0x40);
// cpt += p_asm.setByteAt(cpt, (byte)0xb8);
// cpt += p_asm.setByteAt(cpt, (byte)0x9a);
// cpt += p_asm.setByteAt(cpt, (byte)0x99);
// cpt += p_asm.setByteAt(cpt, (byte)0xc9);
// cpt += p_asm.setByteAt(cpt, (byte)0x40);
//sub esp, 8
// cpt += p_asm.setByteAt(cpt, (byte)0x83);
// cpt += p_asm.setByteAt(cpt, (byte)0xc4);
// cpt += p_asm.setByteAt(cpt, (byte)0x8);
//
// pop esp
cpt += p_asm.setByteAt(cpt, (byte)0x5c);
// // pop ebp
cpt += p_asm.setByteAt(cpt, (byte)0x5d);
// ret
p_asm.setByteAt(cpt++, (byte)0xc3);
// ret
// p_asm.setByteAt(cpt++, (byte)0xc2);
// p_asm.setByteAt(cpt++, (byte)0x00);
// p_asm.setByteAt(cpt++, (byte)0x32);
JNative n_asm = new JNative(p_asm.getPointer(), Convention.STDCALL);
n_asm.setRetVal(Type.INT);
// for(int i = 0; i<1000000; i++) {
n_asm.invoke();
// }
System.err.println("n_asm returned : " + Integer.toString(n_asm.getRetValAsInt(), 16));
// System.exit(02);
/*
System.err.println("g_type_init");
JNative g_type_init = new JNative("/usr/lib/libgnomeui-2.so.0", "g_type_init");
g_type_init.invoke();
g_type_init.dispose();
JNative gtk_init = new JNative("/usr/lib/libgnomeui-2.so.0", "gtk_init");
gtk_init.setParameter(0, NullPointer.NULL);
gtk_init.setParameter(1, NullPointer.NULL);
gtk_init.invoke();
MessageBoxGnome(
"You are using Linux !?\nYou are a fool ?\nYou are not doing as all other dummies ?\nCongratulations !",
"info",
new String[] { "Welcome to JNative", "Ok"}
);*/
/*
new Thread() {
public void run() {
try {
JNative gtk_main = new JNative("/usr/lib/libbonoboui-2.so.0", "gtk_main");
gtk_main.invoke();
} catch (NativeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
*/
System.err.println(System.getProperty("java.library.path"));
JNative strcpy = new JNative("/lib/libc.so.6", "strcpy", false);
strcpy.setRetVal(Type.INT);
Pointer p = new Pointer(MemoryBlockFactory.createMemoryBlock(100));
p.zeroMemory();
p.setStringAt(0, "Toto was not here, but ");
System.err.println("Before strcat");
System.err.println("Get string from pointer p: " + p.getAsString());
strcpy.setParameter(0, p);
strcpy.setParameter(1, Type.STRING,
"strcat was used from /lib/libc.so.6");
strcpy.invoke();
// System.err.println(strcpy.getRetVal());
System.err.println("After strcat");
System.err
.println("Get string from pointer p : " + p.getAsString());
JNative strcat = new JNative("/lib/libc.so.6", "strcat", false);
strcat.setRetVal(Type.INT);
Pointer p0 = new Pointer(MemoryBlockFactory.createMemoryBlock(100));
p0.zeroMemory();
p0.setStringAt(0, "Toto was not here, but ");
System.err.println("Before strcat");
System.err.println("Get string from pointer p: " + p0.getAsString());
strcat.setParameter(0, p0);
strcat.setParameter(1, Type.STRING,
"strcat was used from /lib/libc.so.6");
strcat.invoke();
// System.err.println(strcat.getRetVal());
System.err.println("After strcat");
System.err
.println("Get string from pointer p : " + p0.getAsString());
// strcat.dispose();
// Be aware the the following libglib-.* is a regular expression and
// .* represents any characters
// Not dot followed by any characters.
File f = searchFile("libglib-.*", "/usr/lib");
if (f == null) {
System.err.println("Cannot demonstrate glib");
} else {
JNative b = new JNative(f.getCanonicalPath(),
"g_random_int_range", false);
b.setRetVal(Type.INT);
b.setParameter(0, 0);
b.setParameter(1, 255);
b.invoke();
System.err
.println("Random number from g_random_int in /usr/lib/libglib-2.0.so: "
+ b.getRetVal());
b.dispose();
}
// String ss[] =
// JNative.getDLLFileExports("/lib/libparted-1.7.so.1");
// for(String s : ss) { System.err.println(s); }
f = searchFile("libparted.*", "/usr/lib");
if (f == null)
f = searchFile("libparted.*", "/lib");
/*
if (f == null) {
MessageBoxGnome(
"Cannot demonstrate gparted calls",
"info",
new String[] { "Ok"}
);
return;
} else {
MessageBoxGnome(
"Demonstrate of gparted calls",
"info",
new String[] { "Ok"}
);
}*/
JNative ped_device_get = new JNative(f.toString(), "ped_device_get");
ped_device_get.setRetVal(Type.INT);
ped_device_get.setParameter(0, Type.STRING, "/dev/hda");
ped_device_get.invoke();
Pointer devicePointer = new Pointer(new NativeMemoryBlock(
ped_device_get.getRetValAsInt(), 19 * 4));
System.err.format("DevicePointer = %x\n", devicePointer
.getPointer());
if (devicePointer.getPointer() != 0) {
JNative ped_device_open = new JNative(f.toString(),
"ped_device_open");
ped_device_open.setRetVal(Type.INT);
ped_device_open.setParameter(0, devicePointer);
ped_device_open.invoke();
int ret = ped_device_open.getRetValAsInt();
if (ret != 0) {
System.err.println(JNative.getMemoryAsString(devicePointer
.getAsInt(4), 64));
MessageBoxGnome(
"Your /dev/hda is "+JNative.getMemoryAsString(devicePointer.getAsInt(4), 64),
"info",
new String[] { "Welcome to JNative", "Ok"}
);
JNative ped_device_close = new JNative(f.toString(),
"ped_device_close");
ped_device_close.setRetVal(Type.INT);
ped_device_close.setParameter(0, devicePointer);
ped_device_close.invoke();
} else {
System.err.println("Can't open device!");
}
}
// Be aware the the following libglib-.* is a regular expression and
// .* represents any characters
// Not dot followed by any characters.
f = searchFile("libglib-.*", "/usr/lib");
// Multi-threading
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -