[Refactor] Utils

This commit is contained in:
xiaozhou233 committed 2025-10-13 09:22:27 +08:00
1 parent ad9d627175
commit 2df1e4c80f
6 files changed
+97 -31

No files matched your search

+24 -23
View File
@@ -6,6 +6,7 @@
#include "jvmti.h"
#include "InjectParameters.h"
#include "log.h"
#include "GlobalUtils.h"
#define WIN_X64
extern HINSTANCE hAppInstance;
@@ -44,38 +45,38 @@ DWORD WINAPI ThreadProc(LPVOID lpParam) {
/// ======== Check Environment ======== ///
// Init vars
JavaVM *jvm;
JNIEnv *env;
// Get JavaVM
if (JNI_GetCreatedJavaVMs(&jvm, 1, NULL) != JNI_OK || !jvm) {
log_error("[%s] JNI_GetCreatedJavaVMs failed", LOGNAME);
MessageBoxA(NULL, "JNI_GetCreatedJavaVMs failed", "Error", MB_OK);
} else {
log_trace("[%s] JNI_GetCreatedJavaVMs success", LOGNAME);
}
// Get JNIEnv
if ((*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL) != JNI_OK) {
log_error("[%s] AttachCurrentThread failed", LOGNAME);
MessageBoxA(NULL, "Failed to attach current thread to JVM!", "Error", MB_ICONERROR);
jint result = JNI_ERR;
JavaVM *jvm = NULL;
result = GetCreatedJVM(&jvm);
if (result != JNI_OK) {
log_error("[%s] Failed to get JVM (%d)", LOGNAME, result);
MessageBoxA(NULL, "Failed to get JVM", "Error", MB_OK);
return 1;
} else {
log_trace("[%s] AttachCurrentThread success", LOGNAME);
}
log_info("[%s] Got JVM", LOGNAME);
JNIEnv *env = NULL;
result = GetJNIEnv(jvm, &env);
if (result != JNI_OK) {
log_error("[%s] Failed to get JNIEnv (%d)", LOGNAME, result);
MessageBoxA(NULL, "Failed to get JNIEnv", "Error", MB_OK);
return 1;
}
log_info("[%s] Got JNIEnv", LOGNAME);
// Debug: JVM version
jint version = (*env)->GetVersion(env);
log_trace("[%s] JVM version: 0x%x", LOGNAME, version);
// Init and enable jvmti
jvmtiEnv *jvmti;
jint res = (*jvm)->GetEnv(jvm, (void**)&jvmti, JVMTI_VERSION_1_2);
if (res != JNI_OK || jvmti == NULL) {
log_error("[%s] GetEnv failed", LOGNAME);
jvmtiEnv *jvmti = NULL;
result = GetJVMTI(jvm, &jvmti);
if (result != JNI_OK) {
log_error("[%s] Failed to get JVMTI (%d)", LOGNAME, result);
MessageBoxA(NULL, "Failed to get JVMTI", "Error", MB_OK);
return 1;
} else {
log_trace("[%s] GetEnv success", LOGNAME);
}
log_info("[%s] enabled jvmti", LOGNAME);
+9 -2
View File
@@ -1,6 +1,13 @@
# src/utils/CMakeLists.txt
add_library(utils STATIC
test.c)
JVMUtils.c)
target_include_directories(utils PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_include_directories(utils PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/includes
${CMAKE_SOURCE_DIR}/includes/jvm
${CMAKE_SOURCE_DIR}/includes/jvm/win32)
target_link_libraries(utils PRIVATE
jvm)
+6 -1
View File
@@ -1,8 +1,13 @@
#ifndef GLOBALUTILS_H
#define GLOBALUTILS_H
#include "windows.h"
#include "stdio.h"
#include "jni.h"
#include "jvmti.h"
void test();
jint GetCreatedJVM(JavaVM**);
jint GetJNIEnv(JavaVM*, JNIEnv**);
jint GetJVMTI(JavaVM*, jvmtiEnv**);
#endif
+49
View File
@@ -0,0 +1,49 @@
#include "./GlobalUtils.h"
// Get JavaVM
jint GetCreatedJVM(JavaVM **out_jvm) {
if (!out_jvm) return JNI_ERR;
*out_jvm = NULL;
jint res = JNI_GetCreatedJavaVMs(out_jvm, 1, NULL);
if (res != JNI_OK || *out_jvm == NULL) {
MessageBoxA(NULL, "JNI_GetCreatedJavaVMs failed", "Error", MB_OK);
return res;
}
return JNI_OK;
}
// Get JNIEnv
jint GetJNIEnv(JavaVM *jvm, JNIEnv **out_env) {
if (!jvm || !out_env) return JNI_ERR;
*out_env = NULL;
void *env = NULL;
jint res = (*jvm)->GetEnv(jvm, &env, JNI_VERSION_1_8);
if (res == JNI_OK && env != NULL) {
*out_env = (JNIEnv *)env;
return JNI_OK;
}
// Attach current thread
res = (*jvm)->AttachCurrentThread(jvm, &env, NULL);
if (res != JNI_OK || env == NULL) {
return res;
}
*out_env = (JNIEnv *)env;
return JNI_OK;
}
// Get JVMTI
jint GetJVMTI(JavaVM *jvm, jvmtiEnv **out_jvmti) {
if (!jvm || !out_jvmti) return JNI_ERR;
*out_jvmti = NULL;
jint res = (*jvm)->GetEnv(jvm, (void**)out_jvmti, JVMTI_VERSION_1_2);
if (res == JNI_OK && *out_jvmti != NULL) {
return JNI_OK;
}
*out_jvmti = NULL;
return res;
}
-5
View File
@@ -1,5 +0,0 @@
#include "GlobalUtils.h"
void test() {
printf("Hello World!\n");
}