附加到已在运行的JVM [英] Attach to already running JVM
问题描述
有没有办法附加到已经运行的JVM?
例如,在JNI中,您可以使用 JNI_CreateJavaVM
创建一个VM并运行一个jar并检查所有的类..
然而,如果jar已经运行,我找不到一个方法来附加到它的JVM并与其类进行通信或获取其 env
指针。
另一个问题是,如果jar加载我的本地库(.dll),我想在.dll内部创建一个JVM,我不能...也不能附加jar的当前JVM,无需调用我的函数..
Java侧的示例:
class Foo
{
static {loadLibrary Foo)}
}
b
void Foo()
{
// CreateJVM
//附加到当前进程..
//从加载我的jar中调用函数。
}
这不能在没有调用 Foo
第一。
任何想法?是否无法获取当前的JVM或附加到它或外部jvm实例?
/ p>
1)在托管JVM的进程中插入一个DLL(例如 java.exe
或 javaw.exe
或 iexplore.exe
)。常见的注入技术是使用 SetWindowsHookEx
2)在DLL中,使用<$ c获取 jvm.dll
的模块句柄$ c> GetModuleHandle
3)获取 JNI_GetCreatedJavaVMs
函数,使用 GetProcAddress
4)如果成功,使用 JavaVM
struture中的 AttachCurrentThread
函数指针,将您的线程附加到找到的第一个JVM。
使用快速链接: /docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html\">调用API
Is there a way to attach to an already running JVM?
For example, in JNI you can use JNI_CreateJavaVM
to create a VM and run a jar and inspect all its classes..
However, if the jar is already running, I cannot find a way to attach to its JVM and communicate with its classes or get its env
pointer..
Another problem is that if the jar loads my native library (.dll) and I want to create a JVM inside the .dll, I cannot.. Nor can I attach the jar's current JVM either without the jar calling my function..
Example on the Java side:
class Foo
{
static {loadLibrary("Foo")}
}
on the C++ side:
void Foo()
{
//CreateJVM
//Attach to the current process..
//Call function from the jar that loaded me.
}
This cannot be done without the jar calling Foo
first.
Any ideas? Is there no way to get the current JVM or to attach to it or an external jvm instance?
Yes you can.
1) Inject a DLL in the process hosting the JVM (eg, java.exe
, or javaw.exe
, or iexplore.exe
). A common injection technique is to use SetWindowsHookEx
2) In the DLL, get the module handle of the jvm.dll
using GetModuleHandle
3) Get the address of the JNI_GetCreatedJavaVMs
function, using GetProcAddress
4) Call the function and, if successfull, attach your thread to the first JVM found, using the AttachCurrentThread
function pointer from the JavaVM
struture.
5) Done.
Usefull link: The Invocation API
这篇关于附加到已在运行的JVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!