/// <summary> /// Initializes a new instance of the <see cref="JavaVM" /> class. /// </summary> /// <param name="jvm">Pointer to the native JavaVM structure, returned from the native function JNI_CreateJavaVM.</param> public JavaVM(IntPtr jvm) { this.jvm = jvm; JNIInvokeInterface functions = *(*(JNIInvokeInterfacePtr*)jvm.ToPointer()).Functions; this.destroyJavaVM = (DestroyJavaVM)Marshal.GetDelegateForFunctionPointer(functions.DestroyJavaVM, typeof(DestroyJavaVM)); }
public JniInvokeMethods(IntPtr jvm) { JniInvokeInterface func = **(JniInvokeInterface **)jvm; attachCurrentThread = (AttachCurrentThread)Marshal.GetDelegateForFunctionPointer(func.AttachCurrentThread, typeof(AttachCurrentThread)); detachCurrentThread = (DetachCurrentThread)Marshal.GetDelegateForFunctionPointer(func.DetachCurrentThread, typeof(DetachCurrentThread)); destroyJavaVM = (DestroyJavaVM)Marshal.GetDelegateForFunctionPointer(func.DestroyJavaVM, typeof(DestroyJavaVM)); }
public static void Attach(int pid, string path, string arguments = "") { IntPtr vm = IntPtr.Zero, env = IntPtr.Zero; JavaVMInitArgs args = new JavaVMInitArgs(); args.version = 0x00010008; args.nOptions = 0; int result = JNI_CreateJavaVM(ref vm, ref env, ref args); Console.WriteLine("result " + result); IntPtr real = Marshal.ReadIntPtr(env); IntPtr function = Marshal.ReadIntPtr(real, 668); IntPtr functionL = Marshal.ReadIntPtr(real, 656); IntPtr realVmStruct = Marshal.ReadIntPtr(vm); StringDelegate newStringUTF = Marshal.GetDelegateForFunctionPointer <StringDelegate>(function); GetStringLength stringLen = Marshal.GetDelegateForFunctionPointer <GetStringLength>(functionL); ExceptionCheck checkException = Marshal.GetDelegateForFunctionPointer <ExceptionCheck>(Marshal.ReadIntPtr(real, 912)); ExceptionDescribe printException = Marshal.GetDelegateForFunctionPointer <ExceptionDescribe>(Marshal.ReadIntPtr(real, 64)); NewObjectArray newObjectArray = Marshal.GetDelegateForFunctionPointer <NewObjectArray>(Marshal.ReadIntPtr(real, 688)); FindClass findClass = Marshal.GetDelegateForFunctionPointer <FindClass>(Marshal.ReadIntPtr(real, 24)); SetArray setArray = Marshal.GetDelegateForFunctionPointer <SetArray>(Marshal.ReadIntPtr(real, 696)); DestroyJavaVM destroyVM = Marshal.GetDelegateForFunctionPointer <DestroyJavaVM>(Marshal.ReadIntPtr(realVmStruct, 12)); //attach api start Java_sun_tools_attach_WindowsVirtualMachine_init(env, IntPtr.Zero); IntPtr stub = Java_sun_tools_attach_WindowsVirtualMachine_generateStub(env, IntPtr.Zero); long process = Java_sun_tools_attach_WindowsVirtualMachine_openProcess(env, IntPtr.Zero, pid); Console.WriteLine("exception " + checkException(env)); IntPtr cmd = newStringUTF(env, "load"); IntPtr pipeName = newStringUTF(env, "\\\\.\\pipe\\javatool22"); IntPtr pathJStr = newStringUTF(env, path); IntPtr unknownBoolean = newStringUTF(env, "true"); IntPtr argumentsJ = newStringUTF(env, arguments); Console.WriteLine("exception " + checkException(env)); IntPtr clazz = findClass(env, "java/lang/String"); IntPtr array = newObjectArray(env, 3, clazz, IntPtr.Zero); setArray(env, array, 0, pathJStr); setArray(env, array, 1, unknownBoolean); setArray(env, array, 2, argumentsJ); Java_sun_tools_attach_WindowsVirtualMachine_enqueue(env, IntPtr.Zero, process, stub, cmd, pipeName, array); Console.WriteLine("exception " + checkException(env)); printException(env); /* * * var pipe = new NamedPipeServerStream("javatool22"); * pipe.WaitForConnection(); * BinaryReader reader = new BinaryReader(pipe); * char callback = reader.ReadChar(); * * * Console.WriteLine("pipe result " + callback); */ Java_sun_tools_attach_WindowsVirtualMachine_closeProcess(env, IntPtr.Zero, process); int rr = destroyVM(vm); Console.WriteLine("destroyed vm: " + result); }