【发布时间】:2015-07-22 02:14:13
【问题描述】:
我是新手,我正在测试 JNA jar 库 jna-4.1.0.jar 和 jna-platform-4.1.0.jar。
我正在使用 Kernel32、User32、WinBase、WinDef、WinN 和 WinUser dll 测试性能。
但是,现在我不想使用 JNA(我知道更好更容易)我想使用 JNI(太难了)代替(也许我逆流而上)!!!
我想摆脱对外部 jar Java 文件的依赖
问题:什么对象可以用纯Java对象代替JNA对象?
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.Pointer;
import com.sun.jna.Union;
import com.sun.jna.NativeLong;
import com.sun.jna.ptr.IntByReference;
问题:我有一个使用 JNA 进行测试的代码,但我需要知道如何使用纯 Java 翻译这个结构?
public static class SYSTEM_INFO extends Structure {
public static class ByReference extends SYSTEM_INFO
implements Structure.ByReference { }
public static class UNION extends Union {
public static class ByReference extends UNION
implements Structure.ByReference { }
public static class IntStruct extends Structure {
public static class ByValue extends IntStruct
implements Structure.ByValue {}
public short wProcessorArchitecture;
public short wReserved;
}
public int dwOemId;
public IntStruct s;
}
int dwPageSize;
Pointer lpMinimumApplicationAddress;
Pointer lpMaximumApplicationAddress;
NativeLong dwActiveProcessorMask;
int dwNumberOfProcessors;
int dwProcessorType;
int dwAllocationGranularity;
short wProcessorLevel;
short wProcessorRevision;
}
typedef struct _OSVERSIONINFOEX {
DWORD dwOSVersionInfoSize;
DWORD dwMajorVersion;
DWORD dwMinorVersion;
DWORD dwBuildNumber;
DWORD dwPlatformId;
TCHAR szCSDVersion[128];
WORD wServicePackMajor;
WORD wServicePackMinor;
WORD wSuiteMask;
BYTE wProductType;
BYTE wReserved;
} OSVERSIONINFOEX, *POSVERSIONINFOEX, *LPOSVERSIONINFOEX;
也许这是最简单的部分:
public interface Advapi32 extends Library {
Advapi32 INSTANCE = (Advapi32)Native.loadLibrary("advapi32", Advapi32.class);
boolean GetUserNameA(byte[] name, IntByReference intRef);
}
PD我需要一个真正的教程如何使用JNI调用DLL函数(不是Hello world),使用指针和数据结构等等......
对不起,如果我输了... 谢谢!
【问题讨论】:
-
您可能想解释一下 “我想摆脱对外部 jar Java 文件的依赖”的意思,因为手动编码 JNI 可能不是您唯一的选择.
-
也许,你有理由,我想深入了解JNI,(我不了解JNI)
标签: java windows dll java-native-interface jna