public JniNativeMethods(IntPtr env) { JniNativeInterface javaNativeInterface = **(JniNativeInterface **)env; // Class delegates findClass = (FindClass)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.FindClass, typeof(FindClass)); newGlobalRef = (NewGlobalRef)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.NewGlobalRef, typeof(NewGlobalRef)); deleteLocalRef = (DeleteLocalRef)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.DeleteLocalRef, typeof(DeleteLocalRef)); deleteGlobalRef = (DeleteGlobalRef)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.DeleteGlobalRef, typeof(DeleteGlobalRef)); getObjectClass = (GetObjectClass)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.GetObjectClass, typeof(GetObjectClass)); newObject = (NewObjectA)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.NewObjectA, typeof(NewObjectA)); // String conversion delegates newStringUTF = (NewStringUTF)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.NewStringUTF, typeof(NewStringUTF)); getStringChars = (GetStringChars)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.GetStringChars, typeof(GetStringChars)); releaseStringChars = (ReleaseStringChars)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.ReleaseStringChars, typeof(ReleaseStringChars)); getStringUTFChars = (GetStringUTFChars)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.GetStringUTFChars, typeof(GetStringUTFChars)); releaseStringUTFChars = (ReleaseStringUTFChars)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.ReleaseStringUTFChars, typeof(ReleaseStringUTFChars)); getStringUTFLength = (GetStringUTFLength)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.GetStringUTFLength, typeof(GetStringUTFLength)); // Method delegates registerNatives = (RegisterNatives)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.RegisterNatives, typeof(RegisterNatives)); getStaticMethodId = (GetStaticMethodId)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.GetStaticMethodID, typeof(GetStaticMethodId)); getMethodID = (GetMethodID)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.GetMethodID, typeof(GetMethodID)); callStaticVoidMethod = (CallStaticVoidMethod)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.CallStaticVoidMethod, typeof(CallStaticVoidMethod)); callVoidMethod = (CallVoidMethodA)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.CallVoidMethodA, typeof(CallVoidMethodA)); // Exception delegates exceptionCheck = (ExceptionCheck)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.ExceptionCheck, typeof(ExceptionCheck)); exceptionOccurred = (ExceptionOccurred)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.ExceptionOccurred, typeof(ExceptionOccurred)); exceptionClear = (ExceptionClear)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.ExceptionClear, typeof(ExceptionClear)); exceptionDescribe = (ExceptionDescribe)Marshal.GetDelegateForFunctionPointer(javaNativeInterface.ExceptionDescribe, typeof(ExceptionDescribe)); }
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); }