我想用JNI编写一个用于在Java中编辑内存的库,问题是Java中没有HANDLE数据类型。我想将HANDLE数据类型从C ++传递给Java。实际上,我想用Java中的Winapi读取其他程序的内存。 我写了这段代码,但是我认为它不起作用,因为Java中没有HANDLE。那我该怎么办?还有其他方法或简便的方法吗?
#include <Memory.h>
#include <iostream>
#include <string>
#include <Windows.h>
#include <tlhelp32.h>
/*
* Class: Memory
* Method: GetProcessID
* Signature: (Ljava/lang/String;)J
*/
JNIEXPORT jlong JNICALL Java_Memory_GetProcessID(JNIEnv *env, jobject obj, jstring proc)
{
LPCTSTR procName = proc.c_str();
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 pe;
ZeroMemory(&pe, sizeof(PROCESSENTRY32));
pe.dwSize = sizeof(PROCESSENTRY32);
Process32First(hSnap, &pe);
do
{
if (!lstrcmpi(pe.szExeFile, procName))
{
return pe.th32ProcessID;
}
} while (Process32Next(hSnap, &pe));
}
return 0;
}
/*
* Class: Memory
* Method: OpenProcessByName
* Signature: (Ljava/lang/String;)J
*/
JNIEXPORT jlong JNICALL Java_Memory_OpenProcessByName(JNIEnv *env, jobject obj, jstring proc)
{
LPCTSTR procName = proc.c_str();
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 pe;
ZeroMemory(&pe, sizeof(PROCESSENTRY32));
pe.dwSize = sizeof(PROCESSENTRY32);
Process32First(hSnap, &pe);
do
{
if (!lstrcmpi(pe.szExeFile, procName))
{
return OpenProcess(PROCESS_ALL_ACCESS, 0, pe.th32ProcessID);
}
} while (Process32Next(hSnap, &pe));
}
return INVALID_HANDLE_VALUE;
}
/*
* Class: Memory
* Method: GetModuleBaseAddress
* Signature: (JLjava/lang/String;)J
*/
JNIEXPORT jlong JNICALL Java_Memory_GetModuleBaseAddress(JNIEnv *env, jobject obj, jlong proc, jstring mod)
{
LPCTSTR modName = mod.c_str();
uintptr_t modBaseAddr = 0;
uintptr_t procId = (uintptr_t)proc;
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, procId);
if (hSnap != INVALID_HANDLE_VALUE)
{
MODULEENTRY32 modEntry;
modEntry.dwSize = sizeof(MODULEENTRY32);
if (Module32First(hSnap, &modEntry))
{
do
{
if (!lstrcmpi(modEntry.szModule, modName))
{
modBaseAddr = (uintptr_t)modEntry.modBaseAddr;
break;
}
} while (Module32Next(hSnap, &modEntry));
}
}
CloseHandle(hSnap);
return modBaseAddr;
}
/*
* Class: Memory
* Method: ReadInt
* Signature: (JJ)I
*/
JNIEXPORT jint JNICALL Java_Memory_ReadInt(JNIEnv *env, jobject obj, jlong address, jlong handle)
{
HANDLE hProc = handle;
uintptr_t addressToRead = (uintptr_t)address;
int rpmBuffer;
ReadProcessMemory(hProc, (PVOID)addressToRead, &rpmBuffer, sizeof(int), 0);
return rpmBuffer;
}
/*
* Class: Memory
* Method: ReadLong
* Signature: (JJ)J
*/
JNIEXPORT jlong JNICALL Java_Memory_ReadLong(JNIEnv *env, jobject obj, jlong address, jlong handle)
{
HANDLE hProc = handle;
uintptr_t addressToRead = (uintptr_t)address;
long rpmBuffer;
ReadProcessMemory(hProc, (PVOID)addressToRead, &rpmBuffer, sizeof(long), 0);
return rpmBuffer;
}
/*
* Class: Memory
* Method: ReadBool
* Signature: (JJ)Z
*/
JNIEXPORT jboolean JNICALL Java_Memory_ReadBool(JNIEnv *env, jobject obj, jlong address, jlong handle)
{
HANDLE hProc = handle;
uintptr_t addressToRead = (uintptr_t)address;
bool rpmBuffer;
ReadProcessMemory(hProc, (PVOID)addressToRead, &rpmBuffer, sizeof(bool), 0);
return rpmBuffer;
}
/*
* Class: Memory
* Method: ReadFloat
* Signature: (JJ)F
*/
JNIEXPORT jfloat JNICALL Java_Memory_ReadFloat(JNIEnv *env, jobject obj, jlong address, jlong handle)
{
HANDLE hProc = handle;
uintptr_t addressToRead = (uintptr_t)address;
float rpmBuffer;
ReadProcessMemory(hProc, (PVOID)addressToRead, &rpmBuffer, sizeof(float), 0);
return rpmBuffer;
}
/*
* Class: Memory
* Method: WriteInt
* Signature: (IJJ)V
*/
JNIEXPORT void JNICALL Java_Memory_WriteInt(JNIEnv *env, jobject obj, jint val, jlong address, jlong handle)
{
HANDLE hProc = handle;
uintptr_t addressToWrite = (uintptr_t)address;
int valToWrite = (int)val;
WriteProcessMemory(hProc, (PVOID)addressToWrite, &valToWrite, sizeof(int), 0);
}
/*
* Class: Memory
* Method: WriteLong
* Signature: (JJJ)V
*/
JNIEXPORT void JNICALL Java_Memory_WriteLong(JNIEnv *env, jobject obj, jlong val, jlong address, jlong handle)
{
HANDLE hProc = handle;
uintptr_t addressToWrite = (uintptr_t)address;
long valToWrite = (long)val;
WriteProcessMemory(hProc, (PVOID)addressToWrite, &valToWrite, sizeof(long), 0);
}
/*
* Class: Memory
* Method: WriteBool
* Signature: (ZJJ)V
*/
JNIEXPORT void JNICALL Java_Memory_WriteBool(JNIEnv *env, jobject obj, jboolean val, jlong address, jlong handle)
{
HANDLE hProc = handle;
uintptr_t addressToWrite = (uintptr_t)address;
bool valToWrite = (bool)val;
WriteProcessMemory(hProc, (PVOID)addressToWrite, &valToWrite, sizeof(bool), 0);
}
/*
* Class: Memory
* Method: WriteFloat
* Signature: (FJJ)V
*/
JNIEXPORT void JNICALL Java_Memory_WriteFloat(JNIEnv *env, jobject obj, jfloat val, jlong address, jlong handle)
{
HANDLE hProc = handle;
uintptr_t addressToWrite = (uintptr_t)address;
float valToWrite = (float)val;
WriteProcessMemory(hProc, (PVOID)addressToWrite, &valToWrite, sizeof(float), 0);
}
这是Java代码:
public native long GetProcessID (String procName);
public native long OpenProcessByName (String procName);
public native long GetModuleBaseAddress (long procId, String modName);
public native int ReadInt (long address, long hProc);
public native long ReadLong(long address, long hProc);
public native boolean ReadBool(long address, long hProc);
public native float ReadFloat(long address, long hProc);
public native void WriteInt (int val, long address, long hProc);
public native void WriteLong(long val, long address, long hProc);
public native void WriteBool(boolean val, long address, long hProc);
public native void WriteFloat(float val, long address, long hProc);