Init Commit - Basic Func

This commit is contained in:
xiaozhou233 committed 2025-09-16 14:45:42 +08:00
commit ec4492322f
25 files changed
+10464

No files matched your search

+107
View File
@@ -0,0 +1,107 @@
#include "ReflectiveLoader.h"
#include "windows.h"
#include "unistd.h"
#include "stdio.h"
#include "jni.h"
#include "jvmti.h"
#define WIN_X64
extern HINSTANCE hAppInstance;
DWORD WINAPI ThreadProc(LPVOID lpParam) {
printf("libagent: New thread created\n");
// Get environment variable JuiceAgent_Dir
printf("libagent: Get Env JuiceAgent_Dir");
const char* dir = getenv("JuiceAgent_Dir");
printf("libagent: JuiceAgent_Dir: %s\n", dir);
if (dir == NULL) {
printf("libagent: JuiceAgent_Dir is not set\n");
MessageBoxA(NULL, "JuiceAgent_Dir is not set", "Error", MB_OK);
return 1;
}
// Check JuiceLoader.jar
printf("libagent: Check JuiceLoader.jar");
char path[MAX_PATH];
sprintf(path, "%s\\JuiceLoader.jar", dir);
if (access(path, 0) == -1) {
printf("libagent: JuiceLoader.jar not found\n");
MessageBoxA(NULL, "JuiceLoader.jar not found", "Error", MB_OK);
return 1;
}
// Init vars
JavaVM *jvm;
JNIEnv *env;
// Get JavaVM
if (JNI_GetCreatedJavaVMs(&jvm, 1, NULL) != JNI_OK || !jvm) {
printf("DllMain: JNI_GetCreatedJavaVMs failed\n");
MessageBoxA(NULL, "JNI_GetCreatedJavaVMs failed", "Error", MB_OK);
} else {
printf("DllMain: JNI_GetCreatedJavaVMs success\n");
}
// Get JNIEnv
if ((*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL) != JNI_OK) {
MessageBoxA(NULL, "Failed to attach current thread to JVM!", "Error", MB_ICONERROR);
return 1;
} else {
printf("DllMain: AttachCurrentThread success\n");
}
// Debug: JVM version
jint version = (*env)->GetVersion(env);
printf("JVM version: 0x%x\n", version);
// Init and enable jvmti
printf("libagent: enabling jvmti\n");
jvmtiEnv *jvmti;
jint res = (*jvm)->GetEnv(jvm, (void**)&jvmti, JVMTI_VERSION_1_2);
if (res != JNI_OK || jvmti == NULL) {
printf("libagent: GetEnv failed\n");
return 1;
} else {
printf("libagent: GetEnv success\n");
}
// Inject jars
printf("libagent: injecting jars...\n");
(*jvmti)->AddToBootstrapClassLoaderSearch(jvmti, path);
// Invoke init(dir)
printf("libagent: invoking init()...\n");
jclass cls = (*env)->FindClass(env, "cn/xiaozhou233/juiceloader/JuiceLoader");
jmethodID mid = (*env)->GetStaticMethodID(env, cls, "init", "(Ljava/lang/String;)V");
(*env)->CallStaticVoidMethod(env, cls, mid, (*env)->NewStringUTF(env, dir));
printf("done\n");
return 0;
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved){
BOOL bReturnValue = TRUE;
switch (dwReason){
case DLL_QUERY_HMODULE:
if (lpReserved != NULL)
*(HMODULE *)lpReserved = hAppInstance;
break;
case DLL_PROCESS_ATTACH:
hAppInstance = hinstDLL;
printf("libagent: DLL_PROCESS_ATTACH\n");
HANDLE hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
if (hThread)
CloseHandle(hThread);
break;
case DLL_PROCESS_DETACH:
printf("libagent: DLL_PROCESS_DETACH\n");
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
}
return bReturnValue;
}