我正在尝试使用DLL注入来更改运行Java过程中INT的值。
我的dll:
#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <jni.h>
using namespace std;
BOOL APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved)
{
HMODULE m_hDllInstance = LoadLibrary("jvm.dll");
if( m_hDllInstance == 0)
{
cout << "The jvm.dll could not be found" << endl;
getchar();
}
else
cout << "jvm.dll found" << endl;
JavaVM *jvm;
JNIEnv *env;
typedef jint (JNICALL * GetCreatedJavaVMs)(JavaVM**, jsize, jsize*);
GetCreatedJavaVMs jni_GetCreatedJavaVMs = (GetCreatedJavaVMs)GetProcAddress(m_hDllInstance, "JNI_GetCreatedJavaVMs");
jint size = 1;
jint vmCount;
jint ret= jni_GetCreatedJavaVMs(&jvm, size, &vmCount);
cout << endl << "GetCreated: " << vmCount << endl;
getchar();
jint rc = jvm->AttachCurrentThread((void **) & env, NULL);
jclass cls = env->FindClass("com/InjectMe");
jfieldID field = env->GetStaticFieldID(cls, "prefix", "I");
jint integer = 1;
env->SetIntField(cls, field, integer);
return TRUE;
}
Java类:
package com;
public class InjectMe
{
static int prefix = 0;
public static void main(String[] args)
{
java.util.Scanner sc = new java.util.Scanner(System.in);
while(true)
{
String s = sc.nextLine();
System.out.println(prefix + ": " + s);
if(prefix == 1)
break;
}
sc.close();
}
}
当我在DLL注射器中调用GetLastError()
时,我得到0,所以我没有将代码发布到它。
当我运行以下代码时,什么也不会发生。我在做什么错?
谢谢
由于 prefix
是静态变量,因此您需要使用正确的函数:
env->SetStaticIntField(cls, field, integer);
,在DllMain()
的开始时,您可能还需要添加类似的东西,因此所有这些都不是一次:
if( Reason!=DLL_PROCESS_ATTACH ) return TRUE;
最后,要进行正确的清理:
jvm->DetachCurrentThread();