- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想在 Java 中创建一个 dll 注入(inject)器(并且仅在 Java 中)用于我自己的教育计划,并在 website especialized in online game 中找到了一个基本示例。 .
作者只说是用JNA接口(interface)做的。
所以,我正在研究这段代码并尝试使用 NetBeans IDE 和 JNA 成功编译,但似乎我这里的 JNA 接口(interface) ( 4.2.2 ) 并没有留下这段代码上使用的所有方法和函数作者。
他们是:
所以,如果可能的话,我想在这里寻求一些帮助,尝试解决 JNA 中缺少方法的问题。
我已经修复了这些错误的大部分,但仍然缺少 JNA 中的一些方法,比如我将通过注释逐点展示。
package inject;
//////////////////// JNA-4.2.2 /////////////////////
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.Tlhelp32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinDef.HMODULE;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.W32APIOptions;
import java.io.File;
//////////////////////////////////////////////////
// Extracted from: https://github.com/warmuuh/AndroidCtx/tree/master/HotContext/src/luz/winapi
import inject.luz.winapi.constants.DwDesiredAccess;
import inject.luz.winapi.tools.Advapi32Tools;
import inject.luz.winapi.tools.Kernel32Tools;
import luz.winapi.api.exception.Kernel32Exception;
//////////////////////////////////////////////////////////////////////////////////////////////
public class Inject {
private static int GetPid(String proc){
int id = 0;
Kernel32 kernel32 = (Kernel32) Native.loadLibrary(Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();
WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
try {
while (kernel32.Process32Next(snapshot, processEntry)) {
if (Native.toString(processEntry.szExeFile).equalsIgnoreCase(proc)) {
id = processEntry.th32ProcessID.intValue();
}
}
}
finally {
kernel32.CloseHandle(snapshot);
}
return id;
}
private static String findProcessByPID(int pid){
String name = "";
Kernel32 kernel32 = (Kernel32) Native.loadLibrary(Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();
WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
try {
while (kernel32.Process32Next(snapshot, processEntry)) {
if (pid == processEntry.th32ProcessID.intValue()) {
name = processEntry.szExeFile.toString();
}
}
}
finally {
kernel32.CloseHandle(snapshot);
}
return name;
}
public static void inject(File dll, Integer pId) throws Kernel32Exception {
if(null == dll || !dll.exists() || !dll.isFile() || !dll.getName().endsWith(".dll"))
return;
String p = findProcessByPID(pId);
if(null == p) return;
Kernel32 kernel = Kernel32.INSTANCE;
HMODULE kernel32Pointer = kernel.GetModuleHandle("Kernel32");
// Cannot find "GetProcAddress"
Pointer loadLibraryAddress = kernel.GetProcAddress(kernel32Pointer, "LoadLibraryA");
HANDLE process = null;
DwDesiredAccess access = new DwDesiredAccess();
access.setPROCESS_ALL_ACCESS();
try {
Advapi32Tools.getInstance().enableDebugPrivilege(Kernel32Tools.getInstance().GetCurrentProcess());
} catch (Exception e) {
}
// Incompatible types "Pointer" and "HANDLE"
process = Kernel32Tools.getInstance().OpenProcess(access, false, pId);
String path = dll.getPath() + '\0';
byte[] bytes = path.getBytes();
int pathLength = bytes.length;
// Cannot find "VirtualAllocEx"
Pointer memoryDllPath = kernel.VirtualAllocEx(process, null, pathLength, Kernel32Tools.MEM_COMMIT, Kernel32Tools.PAGE_READWRITE);
Memory dllPathContent = new Memory(pathLength);
for(int i=0;i<pathLength;i++)
dllPathContent.setByte(i, bytes[i]);
IntByReference writeResult = new IntByReference();
boolean successWritting = kernel.WriteProcessMemory(process, memoryDllPath, dllPathContent, pathLength, writeResult);
if(!successWritting) {
kernel.CloseHandle(process);
return;
}
IntByReference threadId = new IntByReference();
// Pointer cannot be converted to "FOREIGN_THREAD_START_ROUTINE"
Pointer thread = kernel.CreateRemoteThread(process, null, 0, loadLibraryAddress, memoryDllPath, 0, threadId);
boolean res = false;
// Incompatible types "Pointer" and "HANDLE" //Cannot find "WAIT_TIMEOUT"
res = kernel.WaitForSingleObject(thread, Integer.MAX_VALUE) != Kernel32Tools.WAIT_TIMEOUT;
// Cannot find "VirtualFreeEx" method // Cannot find "MEM_RELEASE"
kernel.VirtualFreeEx(process, memoryDllPath, pathLength, Kernel32Tools.MEM_RELEASE);
kernel.CloseHandle(process);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println(GetPid("notepad.exe"));
}
}
提前感谢任何建议或帮助:-)
最佳答案
JNA 缺少方法? It ain't so!
您只需要扩展该库并添加您自己的(理想情况下,还可以将“缺失的”方法贡献回 JNA 库,以便其他人可以受益。
Here is an example了解某人如何映射 GetProcAddress。
Someone has mapped VirtualAllocEx here (尽管他们应该适本地扩展 Kernel32 而不是完全复制它并编辑部分)
我在找到其他 VirtualFreeEx 的 15 秒内找不到示例...并不意味着它不存在,但在编写其他示例之后,您编写它也不会有太大问题。
关于java - JNA:缺少一些具体方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38493357/
我正在使用 JNA 加载 native 库: MyLibrary INSTANCE = (MyLibrary) Native.loadLibrary("mylibrary.so", MyLibrary
任何人都可以为我提供一个 JNA 鼠标钩子(Hook)的工作示例,它能够在我的 Java Swing 应用程序之外跟踪鼠标移动/单击吗? 提前致谢 最佳答案 是的,这里是代码... public cl
我在哪里可以获得支持 ARM64 的 JNA 库? com.sun.jna_4.jar 包含适用于 ARM 的 native libjnidispatch.so 库,但不包含适用于 ARM64 的库。
我正在尝试使用 JNA,因为我想使用一个用 c++ 编写的 .dll,而我的其余代码是用 Java 编写的。但是,如果我尝试导入 JNA 类 Eclipse 声称“com.sun.jna.Librar
我使用以下代码从java程序控制Windows服务 public class PostgresService2 { public static void main(String[] args)
我有一个用于 C 函数 mpv_set_option_string 的 JNA Java 接口(interface),定义为: public interface MPV extends StdCall
我正在查看中央 Maven 存储库,并看到一个 net.java.dev.jna 和一个用于 JNA 的 com.sun.jna groupId。 JNA 的 github,使用 com.sun.jn
我已经开始构建一个 java 服务,它结合了 JNA 来加载 native C/C++ 库,我想在 Raspberry PI aka arm 平台上运行 java 服务。我已经成功地建立了一个稳定的服
我要离开 this如何传递 com.sun.jna.Structure 的示例包含 com.sun.jna.StringArray使用 JNA 从 Java 转换为 native C 代码,并且无法在
我正在准备 spring-boot 应用程序。然后我得到以下错误: JNA not found. native methods will be disabled. java.lang.ClassNot
遇到令人抓狂的阻塞错误: Exception in thread "main" java.lang.Error: Structure.getFieldOrder() on class com.luke
我已经下载并复制 Jna.jar 和 Platform.jar 到/usr/cassandra/apache-cassandra-1.0.7/lib 文件夹,但在 Cassandra 启动时仍然看到以
我有一个像这样的 JNA 库 stub : public interface FREngine extends Library { NativeLibrary JNA_NATIVE_LIB =
我想使用 JNAerator 为 JNA 生成一些 C++ API 函数的 Java 包装器。问题是我无法在工作时使用 JNAerator Studio 或从我的 PC 访问 Github。我只有 G
在 JNA 中,如何从 Xlib 映射联合结构,如以下 XEvent typedef union _XEvent { int type; /* must not be changed *
我正在尝试使用 JNA 从 java 运行 ShellExecute 函数。我在非 unicode 文件夹上运行 ShellExecuteA 没有任何问题 import com.sun.jna.*;
在弄清楚如何将 ClientToScreen winapi 函数与 JNA 结合使用时遇到问题。 我仍然得到窗口句柄坐标的 0, 0 输出。我引用了这个,但我确信我做得不对 https://msdn.
我有两个局部变量: Pointer output; int output_len; /* or better `size_t output_len;` */ 我需要将这些变量的指针传递到 JNA 函数
我有一个openvr binding有一段时间以来有一个小问题 基本上,每当我释放某些几何 3D 模型(基站或 Controller )的内存时,我都会时不时地收到错误: "java.lang.Err
我是 JNA 新手,我想将 DsGetDcName 方法转换为 JNA。 DWORD DsGetDcName( __in LPCTSTR ComputerName, __in LPCT
我是一名优秀的程序员,十分优秀!