├── README.md ├── JNISharp ├── NativeInterface │ ├── JString.cs │ ├── ReleaseMode.cs │ ├── ReferenceType.cs │ ├── Version.cs │ ├── JNIResultException.cs │ ├── JThrowableException.cs │ ├── JThrowable.cs │ ├── JavaVMOption.cs │ ├── JFieldID.cs │ ├── JMethodID.cs │ ├── JVMImports.cs │ ├── JavaVMInitArgs.cs │ ├── JObjectArray.cs │ ├── JArray.cs │ ├── Result.cs │ ├── JavaVM.cs │ ├── JObject.cs │ ├── TypeSignature.cs │ ├── JValue.cs │ ├── JClass.cs │ ├── JNIEnv.cs │ └── JNI.cs ├── ToolInterface │ ├── EventMode.cs │ ├── Version.cs │ ├── JVMTIErrorException.cs │ ├── JClassAccessFlags.cs │ ├── JFieldAccessFlags.cs │ ├── JMethodAccessFlags.cs │ ├── JMethodSignature.cs │ ├── JFieldSignature.cs │ ├── JClassSignature.cs │ ├── Event.cs │ ├── Error.cs │ ├── JVMTIArray.cs │ ├── JVMTI.cs │ ├── EventCallbacks.cs │ └── JVMTIEnv.cs └── JNISharp.csproj ├── LICENSE ├── JNISharp.sln ├── .gitattributes └── .gitignore /README.md: -------------------------------------------------------------------------------- 1 | # JNISharp 2 | JNI & JVMTI bindings for C# 3 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/JString.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | public class JString : JObject 4 | { 5 | public JString() : base() { } 6 | 7 | public string GetString() => JNI.GetJStringString(this); 8 | } 9 | -------------------------------------------------------------------------------- /JNISharp/ToolInterface/EventMode.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.ToolInterface; 2 | 3 | public static partial class JVMTI 4 | { 5 | public enum EventMode 6 | { 7 | Enable = 1, 8 | Disable = 0 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/ReleaseMode.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | public static partial class JNI 4 | { 5 | public enum ReleaseMode 6 | { 7 | Default = 0, 8 | Commit = 1, 9 | Abort = 2 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/ReferenceType.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | public static partial class JNI 4 | { 5 | public enum ReferenceType 6 | { 7 | Local = 0, 8 | Global = 1, 9 | WeakGlobal = 2 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /JNISharp/ToolInterface/Version.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.ToolInterface; 2 | 3 | public static partial class JVMTI 4 | { 5 | public enum Version 6 | { 7 | V1 = 0x30010000, 8 | V1_1 = 0x30010100, 9 | V1_2 = 0x30010200, 10 | V = 0x30000000 + (1 * 0x10000) + (2 * 0x100) + 1 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/Version.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | public static partial class JNI 4 | { 5 | public enum Version 6 | { 7 | V1_1 = 0x00010001, 8 | V1_2 = 0x00010001, 9 | V1_4 = 0x00010004, 10 | V1_6 = 0x00010006, 11 | V1_8 = 0x00010008 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /JNISharp/ToolInterface/JVMTIErrorException.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.ToolInterface; 2 | 3 | public class JVMTIErrorException : Exception 4 | { 5 | public JVMTIErrorException(JVMTI.Error error) : base($"JVMTI Error: {error}") { } 6 | 7 | public JVMTIErrorException(JVMTI.Error error, Exception inner) : base($"JVMTI Error: {error}", inner) { } 8 | } 9 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/JNIResultException.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | public class JNIResultException : Exception 4 | { 5 | public JNI.Result Result { get; init; } 6 | 7 | public JNIResultException(JNI.Result result) : base($"JNI error occurred: {result}") 8 | { 9 | this.Result = result; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /JNISharp/ToolInterface/JClassAccessFlags.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.ToolInterface; 2 | 3 | 4 | [Flags] 5 | public enum JClassAccessFlags 6 | { 7 | Public = 0x0001, 8 | Final = 0x0010, 9 | Super = 0x0020, 10 | Interface = 0x0200, 11 | Abstract = 0x0400, 12 | Synthetic = 0x1000, 13 | Annotation = 0x2000, 14 | Enum = 0x4000 15 | } 16 | -------------------------------------------------------------------------------- /JNISharp/ToolInterface/JFieldAccessFlags.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.ToolInterface; 2 | 3 | public enum JFieldAccessFlags 4 | { 5 | Public = 0x0001, 6 | Private = 0x0002, 7 | Protected = 0x0004, 8 | Static = 0x0008, 9 | Final = 0x0010, 10 | Volatile = 0x0040, 11 | Transient = 0x0080, 12 | Synthetic = 0x1000, 13 | Enum = 0x4000 14 | } 15 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/JThrowableException.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | public class JThrowableException : Exception 4 | { 5 | public JThrowable Throwable { get; init; } 6 | 7 | public JThrowableException() { } 8 | 9 | public JThrowableException(JThrowable throwable) : base() 10 | { 11 | this.Throwable = throwable; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /JNISharp/ToolInterface/JMethodAccessFlags.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.ToolInterface; 2 | 3 | [Flags] 4 | public enum JMethodAccessFlags 5 | { 6 | Public = 0x0001, 7 | Private = 0x0002, 8 | Protected = 0x0004, 9 | Static = 0x0008, 10 | Final = 0x0010, 11 | Synchronized = 0x0020, 12 | Bridge = 0x0040, 13 | VarArgs = 0x0080, 14 | Native = 0x0100, 15 | Abstract = 0x400, 16 | Strict = 0x0800, 17 | Synthetic = 0x1000 18 | } 19 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/JThrowable.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | public class JThrowable : JObject 4 | { 5 | public JThrowable() { } 6 | 7 | public string GetMessage() => JNI.FindClass("java/lang/Throwable").CallObjectMethod(this, "getMessage", "()Ljava/lang/String;").GetString(); 8 | 9 | public override string ToString() => JNI.FindClass("java/lang/Throwable").CallObjectMethod(this, "toString", "()Ljava/lang/String;").GetString(); 10 | } 11 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/JavaVMOption.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | using System.Runtime.InteropServices; 4 | 5 | [StructLayout(LayoutKind.Sequential)] 6 | public unsafe struct JavaVMOption 7 | { 8 | public IntPtr OptionString; 9 | 10 | public IntPtr ExtraInfo; 11 | 12 | public JavaVMOption(string optionString) 13 | { 14 | this.OptionString = Marshal.StringToHGlobalAnsi(optionString); 15 | this.ExtraInfo = IntPtr.Zero; 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/JFieldID.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | public readonly struct JFieldID : IEquatable 4 | { 5 | public readonly IntPtr Handle { get; init; } 6 | 7 | internal JFieldID(IntPtr handle) 8 | { 9 | this.Handle = handle; 10 | } 11 | 12 | public static implicit operator IntPtr(JFieldID fieldID) => fieldID.Handle; 13 | 14 | public static implicit operator JFieldID(IntPtr pointer) => new(pointer); 15 | 16 | public bool Equals(JFieldID other) 17 | { 18 | return this.Handle == other.Handle; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/JMethodID.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | public readonly struct JMethodID : IEquatable 4 | { 5 | public readonly IntPtr Handle { get; init; } 6 | 7 | internal JMethodID(IntPtr handle) 8 | { 9 | this.Handle = handle; 10 | } 11 | 12 | public static implicit operator IntPtr(JMethodID methodID) => methodID.Handle; 13 | 14 | public static implicit operator JMethodID(IntPtr pointer) => new(pointer); 15 | 16 | public bool Equals(JMethodID other) 17 | { 18 | return this.Handle == other.Handle; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /JNISharp/ToolInterface/JMethodSignature.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.ToolInterface; 2 | 3 | 4 | public class JMethodSignature 5 | { 6 | public string Name { get; init; } 7 | 8 | public string Signature { get; init; } 9 | 10 | public string Generic { get; init; } 11 | 12 | public JMethodAccessFlags AccessFlags { get; init; } 13 | 14 | public JMethodSignature(string name, string sig, string generic, JMethodAccessFlags flags) 15 | { 16 | this.Name = name; 17 | this.Signature = sig; 18 | this.Generic = generic; 19 | this.AccessFlags = flags; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/JVMImports.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | using System.Runtime.InteropServices; 4 | 5 | internal unsafe static class JVMImports 6 | { 7 | [DllImport("jvm.dll", CallingConvention = CallingConvention.Winapi)] 8 | internal static extern JNI.Result JNI_CreateJavaVM(out JavaVM* jvm, out JNIEnv* env, JavaVMInitArgs* args); 9 | 10 | [DllImport("jvm.dll", CallingConvention = CallingConvention.Winapi)] 11 | internal static extern JNI.Result JNI_GetDefaultJavaVMInitArgs(IntPtr argsPtr); 12 | 13 | [DllImport("jvm.dll", CallingConvention = CallingConvention.Winapi)] 14 | internal static extern JNI.Result JNI_GetCreatedJavaVMs(out JavaVM* jvm, int bufferLength, out int createdVMs); 15 | } 16 | -------------------------------------------------------------------------------- /JNISharp/ToolInterface/JFieldSignature.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.ToolInterface; 2 | 3 | using System.Text.Json.Serialization; 4 | 5 | public class JFieldSignature 6 | { 7 | [JsonInclude] 8 | public string Name { get; init; } 9 | 10 | [JsonInclude] 11 | public string Signature { get; init; } 12 | 13 | 14 | [JsonInclude] 15 | public string Generic { get; init; } 16 | 17 | [JsonInclude] 18 | public JFieldAccessFlags AccessFlags { get; init; } 19 | 20 | public JFieldSignature(string name, string sig, string generic, JFieldAccessFlags flags) 21 | { 22 | this.Name = name; 23 | this.Signature = sig; 24 | this.Generic = generic; 25 | this.AccessFlags = flags; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/JavaVMInitArgs.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | using System.Runtime.InteropServices; 4 | 5 | [StructLayout(LayoutKind.Sequential)] 6 | public unsafe struct JavaVMInitArgs 7 | { 8 | public int Version; 9 | 10 | public int OptionsCount; 11 | 12 | public JavaVMOption* Options; 13 | 14 | public bool IgnoreUnrecognized; 15 | 16 | public JavaVMInitArgs(JNI.Version version, JavaVMOption[] options, bool ignoreUnrecognized) 17 | { 18 | this.Version = (int)version; 19 | this.OptionsCount = options.Length; 20 | this.Options = (JavaVMOption*)Marshal.UnsafeAddrOfPinnedArrayElement(options, 0); 21 | this.IgnoreUnrecognized = ignoreUnrecognized; 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/JObjectArray.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | using System.Collections; 4 | 5 | public class JObjectArray : JObject, IEnumerable where T : JObject, new() 6 | { 7 | public int Length => JNI.GetArrayLength(this); 8 | 9 | public T this[int index] 10 | { 11 | get => JNI.GetObjectArrayElement(this, index); 12 | set => JNI.SetObjectArrayElement(this, index, value); 13 | } 14 | 15 | public void SetElement(T value, int index) 16 | { 17 | JNI.SetObjectArrayElement(this, index, value); 18 | } 19 | 20 | public IEnumerator GetEnumerator() 21 | { 22 | for (int i = 0; i < this.Length; i++) 23 | { 24 | yield return JNI.GetObjectArrayElement(this, i); 25 | } 26 | } 27 | 28 | IEnumerator IEnumerable.GetEnumerator() 29 | { 30 | return this.GetEnumerator(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/JArray.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | using System.Collections; 4 | using System.Collections.Generic; 5 | 6 | public class JArray : JObject, IEnumerable 7 | { 8 | public JArray() : base() { } 9 | 10 | public JArray(int size) => JNI.NewArray(size); 11 | 12 | public int Length => JNI.GetArrayLength(this); 13 | 14 | public T this[int index] 15 | { 16 | get => JNI.GetArrayElement(this, index); 17 | set => JNI.SetArrayElement(this, index, value); 18 | } 19 | 20 | public T[] GetElements() 21 | { 22 | return JNI.GetArrayElements(this); 23 | } 24 | 25 | public IEnumerator GetEnumerator() 26 | { 27 | for (int i = 0; i < this.Length; i++) 28 | { 29 | yield return JNI.GetArrayElement(this, i); 30 | } 31 | } 32 | 33 | IEnumerator IEnumerable.GetEnumerator() 34 | { 35 | return this.GetEnumerator(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/Result.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | public static partial class JNI 4 | { 5 | /// 6 | /// Represents a JNI error. 7 | /// 8 | public enum Result 9 | { 10 | /// 11 | /// Success. 12 | /// 13 | Ok = 0, 14 | /// 15 | /// Unknown error occured. 16 | /// 17 | Unknown = -1, 18 | /// 19 | /// Thread detached from the JVM. 20 | /// 21 | Detached = -2, 22 | /// 23 | /// Version unsupported. 24 | /// 25 | Version = -3, 26 | /// 27 | /// JVM out of memory. 28 | /// 29 | OutOfMemory = -4, 30 | /// 31 | /// JVM already exists. 32 | /// 33 | AlreadyExists = -5, 34 | /// 35 | /// Invalid arguments passed. 36 | /// 37 | InvalidArguments = -6 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /JNISharp/JNISharp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | Warren Ulrich 8 | JNI & JVMTI bindings for C#. 9 | MIT 10 | https://github.com/WarrenUlrich/JNISharp 11 | JNI, JVMTI, Java, JVM 12 | 13 | 14 | 15 | 16 | true 17 | true 18 | 19 | 20 | 21 | true 22 | 23 | 24 | 25 | true 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Warren 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /JNISharp/ToolInterface/JClassSignature.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.ToolInterface; 2 | 3 | using System.Text.Json; 4 | using System.Text.Json.Serialization; 5 | 6 | public class JClassSignature 7 | { 8 | [JsonInclude] 9 | public string Signature { get; init; } 10 | 11 | [JsonInclude] 12 | public string Generic { get; init; } 13 | 14 | [JsonInclude] 15 | public IEnumerable FieldSignatures { get; init; } 16 | 17 | [JsonInclude] 18 | public IEnumerable MethodSignatures { get; init; } 19 | 20 | public JClassSignature(string signature, string generic, IEnumerable fieldSignatures, IEnumerable methodSignatures) 21 | { 22 | this.Signature = signature; 23 | this.Generic = generic; 24 | this.FieldSignatures = fieldSignatures; 25 | this.MethodSignatures = methodSignatures; 26 | } 27 | 28 | public string ToJson() 29 | { 30 | return JsonSerializer.Serialize(this); 31 | } 32 | 33 | public string ToJson(JsonSerializerOptions options) 34 | { 35 | return JsonSerializer.Serialize(this, options); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /JNISharp/ToolInterface/Event.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.ToolInterface; 2 | 3 | public static partial class JVMTI 4 | { 5 | public enum Event 6 | { 7 | VmInit = 50, 8 | VmDeath = 51, 9 | ThreadStart = 52, 10 | ThreadEnd = 53, 11 | ClassFileLoadHook = 54, 12 | ClassLoad = 55, 13 | ClassPrepare = 56, 14 | VmStart = 57, 15 | Exception = 58, 16 | ExceptionCatch = 59, 17 | SingleStep = 60, 18 | FramePop = 61, 19 | Breakpoint = 62, 20 | FieldAccess = 63, 21 | FieldModification = 64, 22 | MethodEntry = 65, 23 | MethodExit = 66, 24 | NativeMethodBind = 67, 25 | CompiledMethodLoad = 68, 26 | CompiledMethodUnload = 69, 27 | DynamicCodeGenerated = 70, 28 | DataDumpRequest = 71, 29 | MonitorWait = 73, 30 | MonitorWaited = 74, 31 | MonitorContendedEnter = 75, 32 | MonitorContendedEntered = 76, 33 | ResourceExhausted = 80, 34 | GarbageCollectionStart = 81, 35 | GarbageCollectionFinish = 82, 36 | ObjectFree = 83, 37 | VmObjectAlloc = 84 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /JNISharp.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30803.129 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JNISharp", "JNISharp\JNISharp.csproj", "{E4B88E1E-0E7C-4184-970C-8F9E62ED6076}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Debug|x86 = Debug|x86 12 | Release|Any CPU = Release|Any CPU 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {E4B88E1E-0E7C-4184-970C-8F9E62ED6076}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {E4B88E1E-0E7C-4184-970C-8F9E62ED6076}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {E4B88E1E-0E7C-4184-970C-8F9E62ED6076}.Debug|x86.ActiveCfg = Debug|x86 19 | {E4B88E1E-0E7C-4184-970C-8F9E62ED6076}.Debug|x86.Build.0 = Debug|x86 20 | {E4B88E1E-0E7C-4184-970C-8F9E62ED6076}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {E4B88E1E-0E7C-4184-970C-8F9E62ED6076}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {E4B88E1E-0E7C-4184-970C-8F9E62ED6076}.Release|x86.ActiveCfg = Release|x86 23 | {E4B88E1E-0E7C-4184-970C-8F9E62ED6076}.Release|x86.Build.0 = Release|x86 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {BD3274F0-3BDF-46B7-86ED-70D880518D12} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/JavaVM.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | using System; 4 | using System.Runtime.InteropServices; 5 | 6 | /// 7 | /// Represents a JavaVM C struct. 8 | /// 9 | [StructLayout(LayoutKind.Sequential)] 10 | internal readonly unsafe struct JavaVM 11 | { 12 | [StructLayout(LayoutKind.Sequential)] 13 | internal readonly unsafe struct FunctionTable 14 | { 15 | internal readonly IntPtr Reserved0; 16 | 17 | internal readonly IntPtr Reserved1; 18 | 19 | internal readonly IntPtr Reserved2; 20 | 21 | // jint (JNICALL *DestroyJavaVM)(JavaVM *vm); 22 | internal readonly delegate* unmanaged[Stdcall] DestroyJavaVM; 23 | 24 | // jint (JNICALL *AttachCurrentThread)(JavaVM *vm, void **penv, void *args); 25 | internal readonly delegate* unmanaged[Stdcall] AttachCurrentThread; 26 | 27 | // jint (JNICALL *DetachCurrentThread)(JavaVM *vm); 28 | internal readonly delegate* unmanaged[Stdcall] DetachCurrentThread; 29 | 30 | // jint (JNICALL *GetEnv)(JavaVM *vm, void **penv, jint version); 31 | internal readonly delegate* unmanaged[Stdcall] GetEnv; 32 | 33 | // jint (JNICALL *AttachCurrentThreadAsDaemon)(JavaVM *vm, void **penv, void *args); 34 | internal readonly delegate* unmanaged[Stdcall] AttachCurrentThreadAsDaemon; 35 | } 36 | 37 | internal readonly FunctionTable* Functions; 38 | } 39 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/JObject.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | public class JObject : IDisposable 4 | { 5 | private bool Disposed { get; set; } 6 | 7 | public IntPtr Handle { get; init; } 8 | 9 | internal JNI.ReferenceType ReferenceType { get; init; } 10 | 11 | public JObject() { } 12 | 13 | public JObject(IntPtr handle, JNI.ReferenceType referenceType) 14 | { 15 | this.Handle = handle; 16 | this.ReferenceType = referenceType; 17 | } 18 | 19 | public JObject(JObject obj) : this(obj.Handle, obj.ReferenceType) 20 | { 21 | this.Disposed = obj.Disposed; 22 | obj.Disposed = true; 23 | } 24 | 25 | protected virtual void Dispose(bool disposing) 26 | { 27 | if (Disposed || this.Handle == IntPtr.Zero) 28 | return; 29 | 30 | switch (this.ReferenceType) 31 | { 32 | case JNI.ReferenceType.Local: 33 | JNI.DeleteLocalRef(this); 34 | break; 35 | 36 | case JNI.ReferenceType.Global: 37 | JNI.DeleteGlobalRef(this); 38 | break; 39 | 40 | case JNI.ReferenceType.WeakGlobal: 41 | JNI.DeleteWeakGlobalRef(this); 42 | break; 43 | } 44 | 45 | Disposed = true; 46 | } 47 | 48 | public bool Valid() 49 | { 50 | return this.Handle != IntPtr.Zero; 51 | } 52 | 53 | ~JObject() 54 | { 55 | Dispose(disposing: false); 56 | } 57 | 58 | public void Dispose() 59 | { 60 | Dispose(disposing: true); 61 | GC.SuppressFinalize(this); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/TypeSignature.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | public static partial class JNI 4 | { 5 | public static class TypeSignature 6 | { 7 | public const string Bool = "Z"; 8 | 9 | public const string Byte = "B"; 10 | 11 | public const string Char = "C"; 12 | 13 | public const string Short = "S"; 14 | 15 | public const string Int = "I"; 16 | 17 | public const string Long = "J"; 18 | 19 | public const string Float = "F"; 20 | 21 | public const string Double = "D"; 22 | } 23 | 24 | public static string GetTypeSignature() 25 | { 26 | Type t = typeof(T); 27 | 28 | if (t == typeof(bool)) 29 | { 30 | return TypeSignature.Bool; 31 | } 32 | else if (t == typeof(sbyte)) 33 | { 34 | return TypeSignature.Byte; 35 | } 36 | else if (t == typeof(char)) 37 | { 38 | return TypeSignature.Char; 39 | } 40 | else if (t == typeof(short)) 41 | { 42 | return TypeSignature.Short; 43 | } 44 | else if (t == typeof(int)) 45 | { 46 | return TypeSignature.Int; 47 | } 48 | else if (t == typeof(long)) 49 | { 50 | return TypeSignature.Long; 51 | } 52 | else if (t == typeof(float)) 53 | { 54 | return TypeSignature.Float; 55 | } 56 | else if (t == typeof(double)) 57 | { 58 | return TypeSignature.Double; 59 | } 60 | else 61 | { 62 | throw new ArgumentException($"GetTypeSignature Type {t} not supported."); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /JNISharp/ToolInterface/Error.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.ToolInterface; 2 | 3 | public static partial class JVMTI 4 | { 5 | public enum Error 6 | { 7 | None = 0, 8 | InvalidThread = 10, 9 | InvalidThreadGroup = 11, 10 | InvalidPriority = 12, 11 | ThreadNotSuspended = 13, 12 | ThreadSuspended = 14, 13 | ThreadNotAlive = 15, 14 | InvalidObject = 20, 15 | InvalidClass = 21, 16 | ClassNotPrepared = 22, 17 | InvalidMethodID = 23, 18 | InvalidLocation = 24, 19 | InvalidFieldID = 25, 20 | NoMoreFrames = 31, 21 | OpaqueFrame = 32, 22 | TypeMismatch = 34, 23 | InvalidSlot = 35, 24 | Duplicate = 40, 25 | NotFound = 41, 26 | InvalidMonitor = 50, 27 | NotMonitorOwner = 51, 28 | Interrupt = 52, 29 | InvalidClassFormat = 60, 30 | CircularClassDefinition = 61, 31 | FailsVerification = 62, 32 | UnsupportedRedefinitionMethodAdded = 63, 33 | UnsupportedRedefinitionSchemaChanged = 64, 34 | InvalidTypeState = 65, 35 | UnsupportedRedefinitionHierarchyChanged = 66, 36 | UnsupportedRedefinitionMethodDeleteted = 67, 37 | UnsupportedVersion = 68, 38 | NamesDontMatch = 69, 39 | UnsupportedRedefinitionClassModifiersChanged = 70, 40 | UnsupportedRedefinitionMethodModifiersChanged = 71, 41 | UnmodifiableClass = 79, 42 | NotAvailable = 98, 43 | MustPossessCapability = 99, 44 | NullPointer = 100, 45 | AbsentInformation = 101, 46 | InvalidEventType = 102, 47 | IllegalArgument = 103, 48 | NativeMethod = 104, 49 | ClassLoaderUnsupported = 106, 50 | OutOfMemory = 110, 51 | AccessDenied = 111, 52 | WrongPhase = 112, 53 | Internal = 113, 54 | UnattachedThread = 115, 55 | InvalidEnvironment = 116 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /JNISharp/ToolInterface/JVMTIArray.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.ToolInterface; 2 | 3 | using JNISharp.NativeInterface; 4 | using System.Collections; 5 | 6 | internal class JVMTIArray : IDisposable, IEnumerable 7 | { 8 | private bool Disposed { get; set; } 9 | 10 | private IntPtr Handle { get; init; } 11 | 12 | private T[] Elements { get; init; } 13 | 14 | internal JVMTIArray(IntPtr handle, int length) 15 | { 16 | unsafe 17 | { 18 | this.Handle = handle; 19 | IntPtr* arr = (IntPtr*)handle; 20 | Type t = typeof(T); 21 | 22 | if (t == typeof(JMethodID)) 23 | { 24 | JMethodID[] buffer = new JMethodID[length]; 25 | 26 | for (int i = 0; i < length; i++) 27 | buffer[i] = arr[i]; 28 | 29 | this.Elements = (T[])(object)buffer; 30 | } 31 | else if (t == typeof(JFieldID)) 32 | { 33 | JFieldID[] buffer = new JFieldID[length]; 34 | 35 | for (int i = 0; i < length; i++) 36 | buffer[i] = arr[i]; 37 | 38 | this.Elements = (T[])(object)buffer; 39 | } 40 | else if (t == typeof(JClass)) 41 | { 42 | JClass[] buffer = new JClass[length]; 43 | 44 | for (int i = 0; i < length; i++) 45 | buffer[i] = new JClass() { Handle = arr[i], ReferenceType = JNI.ReferenceType.Local }; 46 | 47 | this.Elements = (T[])(object)buffer; 48 | } 49 | } 50 | } 51 | 52 | protected virtual void Dispose(bool disposing) 53 | { 54 | if (this.Disposed) 55 | return; 56 | 57 | JVMTI.Deallocate(this.Handle); 58 | 59 | this.Disposed = true; 60 | } 61 | 62 | ~JVMTIArray() 63 | { 64 | Dispose(disposing: false); 65 | } 66 | 67 | public void Dispose() 68 | { 69 | Dispose(disposing: true); 70 | GC.SuppressFinalize(this); 71 | } 72 | 73 | public IEnumerator GetEnumerator() 74 | { 75 | //for (int i = 0; i < this.Elements.Length; i++) 76 | //yield return this.Elements[i]; 77 | 78 | return ((IEnumerable)this.Elements).GetEnumerator(); 79 | } 80 | 81 | IEnumerator IEnumerable.GetEnumerator() 82 | { 83 | return this.GetEnumerator(); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/JValue.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | using System.Runtime.InteropServices; 4 | 5 | [StructLayout(LayoutKind.Explicit, Size = 8)] 6 | public readonly struct JValue 7 | { 8 | [FieldOffset(0)] 9 | public readonly byte Z; 10 | 11 | [FieldOffset(0)] 12 | public readonly sbyte B; 13 | 14 | [FieldOffset(0)] 15 | public readonly char C; 16 | 17 | [FieldOffset(0)] 18 | public readonly short S; 19 | 20 | [FieldOffset(0)] 21 | public readonly int I; 22 | 23 | [FieldOffset(0)] 24 | public readonly long J; 25 | 26 | [FieldOffset(0)] 27 | public readonly float F; 28 | 29 | [FieldOffset(0)] 30 | public readonly double D; 31 | 32 | [FieldOffset(0)] 33 | public readonly IntPtr L; 34 | 35 | public JValue(bool value) 36 | { 37 | this = new JValue(); 38 | Z = Convert.ToByte(value); 39 | } 40 | 41 | public JValue(sbyte value) 42 | { 43 | this = new JValue(); 44 | B = value; 45 | } 46 | 47 | public JValue(char value) 48 | { 49 | this = new JValue(); 50 | C = value; 51 | } 52 | 53 | public JValue(short value) 54 | { 55 | this = new JValue(); 56 | S = value; 57 | } 58 | 59 | public JValue(int value) 60 | { 61 | this = new JValue(); 62 | I = value; 63 | } 64 | 65 | public JValue(long value) 66 | { 67 | this = new JValue(); 68 | J = value; 69 | } 70 | 71 | public JValue(float value) 72 | { 73 | this = new JValue(); 74 | F = value; 75 | } 76 | 77 | public JValue(double value) 78 | { 79 | this = new JValue(); 80 | D = value; 81 | } 82 | 83 | public JValue(IntPtr value) 84 | { 85 | this = new JValue(); 86 | L = value; 87 | } 88 | 89 | public JValue(JObject obj) 90 | { 91 | this = new JValue(); 92 | this.L = obj.Handle; 93 | } 94 | 95 | public JValue(object value) 96 | { 97 | this = new JValue(); 98 | switch (value) 99 | { 100 | case bool z: 101 | this.Z = Convert.ToByte(z); 102 | break; 103 | 104 | case sbyte b: 105 | this.B = b; 106 | break; 107 | 108 | case char c: 109 | this.C = c; 110 | break; 111 | 112 | case short s: 113 | this.S = s; 114 | break; 115 | 116 | case int i: 117 | this.I = i; 118 | break; 119 | 120 | case long j: 121 | this.J = j; 122 | break; 123 | 124 | case float f: 125 | this.F = f; 126 | break; 127 | 128 | case double d: 129 | this.D = d; 130 | break; 131 | 132 | case JObject obj: 133 | this.L = obj.Handle; 134 | break; 135 | } 136 | } 137 | 138 | public static implicit operator JValue(bool b) => new JValue(b); 139 | 140 | public static implicit operator JValue(sbyte b) => new JValue(b); 141 | 142 | public static implicit operator JValue(char c) => new JValue(c); 143 | 144 | public static implicit operator JValue(short s) => new JValue(s); 145 | 146 | public static implicit operator JValue(int i) => new JValue(i); 147 | 148 | public static implicit operator JValue(long j) => new JValue(j); 149 | 150 | public static implicit operator JValue(float f) => new JValue(f); 151 | 152 | public static implicit operator JValue(double d) => new JValue(d); 153 | 154 | public static implicit operator JValue(JObject obj) => new JValue(obj); 155 | } 156 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/JClass.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | public class JClass : JObject 4 | { 5 | private Dictionary, JFieldID> FieldCache { get; set; } = new(); 6 | 7 | private Dictionary, JMethodID> MethodCache { get; set; } = new(); 8 | 9 | public JClass() : base() { } 10 | 11 | public JFieldID GetFieldID(string name, string sig) 12 | { 13 | Tuple key = new(name, sig); 14 | 15 | if (this.FieldCache.TryGetValue(key, out JFieldID found)) 16 | { 17 | return found; 18 | } 19 | else 20 | { 21 | JFieldID id = JNI.GetFieldID(this, name, sig); 22 | this.FieldCache.Add(key, id); 23 | return id; 24 | } 25 | } 26 | 27 | public JFieldID GetStaticFieldID(string name, string sig) 28 | { 29 | Tuple key = new(name, sig); 30 | 31 | if (this.FieldCache.TryGetValue(key, out JFieldID found)) 32 | { 33 | return found; 34 | } 35 | else 36 | { 37 | JFieldID id = JNI.GetStaticFieldID(this, name, sig); 38 | this.FieldCache.Add(key, id); 39 | return id; 40 | } 41 | } 42 | 43 | public JMethodID GetMethodID(string name, string sig) 44 | { 45 | Tuple key = new(name, sig); 46 | 47 | if (this.MethodCache.TryGetValue(key, out JMethodID found)) 48 | { 49 | return found; 50 | } 51 | else 52 | { 53 | JMethodID id = JNI.GetMethodID(this, name, sig); 54 | this.MethodCache.Add(key, id); 55 | return id; 56 | } 57 | } 58 | 59 | public JMethodID GetStaticMethodID(string name, string sig) 60 | { 61 | Tuple key = new(name, sig); 62 | 63 | if (this.MethodCache.TryGetValue(key, out JMethodID found)) 64 | { 65 | return found; 66 | } 67 | else 68 | { 69 | JMethodID id = JNI.GetStaticMethodID(this, name, sig); 70 | this.MethodCache.Add(key, id); 71 | return id; 72 | } 73 | } 74 | 75 | public T GetStaticObjectField(string name, string sig) where T : JObject, new() 76 | { 77 | return JNI.GetStaticObjectField(this, this.GetStaticFieldID(name, sig)); 78 | } 79 | 80 | public T GetStaticField(string name) 81 | { 82 | return JNI.GetStaticField(this, this.GetStaticFieldID(name, JNI.GetTypeSignature())); 83 | } 84 | 85 | public void SetStaticField(string name, T value) 86 | { 87 | JNI.SetStaticField(this, this.GetStaticFieldID(name, JNI.GetTypeSignature()), value); 88 | } 89 | 90 | public T GetObjectField(JObject obj, string name, string sig) where T : JObject, new() 91 | { 92 | return JNI.GetObjectField(obj, this.GetFieldID(name, sig)); 93 | } 94 | 95 | public T GetField(JObject obj, string name) 96 | { 97 | return JNI.GetField(obj, this.GetFieldID(name, JNI.GetTypeSignature())); 98 | } 99 | 100 | public void SetObjectField(JObject obj, string name, string sig, JObject value) 101 | { 102 | JNI.SetObjectField(obj, this.GetFieldID(name, sig), value); 103 | } 104 | 105 | public void SetField(JObject obj, string name, T value) 106 | { 107 | JNI.SetField(obj, this.GetFieldID(name, JNI.GetTypeSignature()), value); 108 | } 109 | 110 | public T CallStaticObjectMethod(string name, string sig, params JValue[] args) where T : JObject, new() 111 | { 112 | return JNI.CallStaticObjectMethod(this, this.GetStaticMethodID(name, sig), args); 113 | } 114 | 115 | public T CallStaticMethod(string name, string sig, params JValue[] args) 116 | { 117 | return JNI.CallStaticMethod(this, this.GetStaticMethodID(name, sig), args); 118 | } 119 | 120 | public void CallStaticVoidMethod(string name, string sig, params JValue[] args) 121 | { 122 | JNI.CallStaticVoidMethod(this, this.GetMethodID(name, sig), args); 123 | } 124 | 125 | public T CallObjectMethod(JObject obj, string name, string sig, params JValue[] args) where T : JObject, new() 126 | { 127 | return JNI.CallObjectMethod(obj, this.GetMethodID(name, sig), args); 128 | } 129 | 130 | public T CallMethod(JObject obj, string name, string sig, params JValue[] args) 131 | { 132 | return JNI.CallMethod(obj, this.GetMethodID(name, sig), args); 133 | } 134 | 135 | public void CallVoidMethod(JObject obj, string name, string sig, params JValue[] args) 136 | { 137 | JNI.CallVoidMethod(obj, this.GetMethodID(name, sig), args); 138 | } 139 | 140 | public T NewObject(string name, string sig, params JValue[] args) where T : JObject, new() 141 | { 142 | return JNI.NewObject(this, this.GetMethodID(name, sig), args); 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Oo]ut/ 33 | [Ll]og/ 34 | [Ll]ogs/ 35 | 36 | # Visual Studio 2015/2017 cache/options directory 37 | .vs/ 38 | # Uncomment if you have tasks that create the project's static files in wwwroot 39 | #wwwroot/ 40 | 41 | # Visual Studio 2017 auto generated files 42 | Generated\ Files/ 43 | 44 | # MSTest test Results 45 | [Tt]est[Rr]esult*/ 46 | [Bb]uild[Ll]og.* 47 | 48 | # NUnit 49 | *.VisualState.xml 50 | TestResult.xml 51 | nunit-*.xml 52 | 53 | # Build Results of an ATL Project 54 | [Dd]ebugPS/ 55 | [Rr]eleasePS/ 56 | dlldata.c 57 | 58 | # Benchmark Results 59 | BenchmarkDotNet.Artifacts/ 60 | 61 | # .NET Core 62 | project.lock.json 63 | project.fragment.lock.json 64 | artifacts/ 65 | 66 | # ASP.NET Scaffolding 67 | ScaffoldingReadMe.txt 68 | 69 | # StyleCop 70 | StyleCopReport.xml 71 | 72 | # Files built by Visual Studio 73 | *_i.c 74 | *_p.c 75 | *_h.h 76 | *.ilk 77 | *.meta 78 | *.obj 79 | *.iobj 80 | *.pch 81 | *.pdb 82 | *.ipdb 83 | *.pgc 84 | *.pgd 85 | *.rsp 86 | *.sbr 87 | *.tlb 88 | *.tli 89 | *.tlh 90 | *.tmp 91 | *.tmp_proj 92 | *_wpftmp.csproj 93 | *.log 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio LightSwitch build output 298 | **/*.HTMLClient/GeneratedArtifacts 299 | **/*.DesktopClient/GeneratedArtifacts 300 | **/*.DesktopClient/ModelManifest.xml 301 | **/*.Server/GeneratedArtifacts 302 | **/*.Server/ModelManifest.xml 303 | _Pvt_Extensions 304 | 305 | # Paket dependency manager 306 | .paket/paket.exe 307 | paket-files/ 308 | 309 | # FAKE - F# Make 310 | .fake/ 311 | 312 | # CodeRush personal settings 313 | .cr/personal 314 | 315 | # Python Tools for Visual Studio (PTVS) 316 | __pycache__/ 317 | *.pyc 318 | 319 | # Cake - Uncomment if you are using it 320 | # tools/** 321 | # !tools/packages.config 322 | 323 | # Tabs Studio 324 | *.tss 325 | 326 | # Telerik's JustMock configuration file 327 | *.jmconfig 328 | 329 | # BizTalk build output 330 | *.btp.cs 331 | *.btm.cs 332 | *.odx.cs 333 | *.xsd.cs 334 | 335 | # OpenCover UI analysis results 336 | OpenCover/ 337 | 338 | # Azure Stream Analytics local run output 339 | ASALocalRun/ 340 | 341 | # MSBuild Binary and Structured Log 342 | *.binlog 343 | 344 | # NVidia Nsight GPU debugger configuration file 345 | *.nvuser 346 | 347 | # MFractors (Xamarin productivity tool) working folder 348 | .mfractor/ 349 | 350 | # Local History for Visual Studio 351 | .localhistory/ 352 | 353 | # BeatPulse healthcheck temp database 354 | healthchecksdb 355 | 356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 357 | MigrationBackup/ 358 | 359 | # Ionide (cross platform F# VS Code tools) working folder 360 | .ionide/ 361 | 362 | # Fody - auto-generated XML schema 363 | FodyWeavers.xsd -------------------------------------------------------------------------------- /JNISharp/ToolInterface/JVMTI.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.ToolInterface; 2 | 3 | using JNISharp.NativeInterface; 4 | using System.Runtime.InteropServices; 5 | 6 | 7 | public unsafe static partial class JVMTI 8 | { 9 | internal static JVMTIEnv* Env; 10 | 11 | internal static Dictionary LoadedClassCache { get; set; } = new(); 12 | 13 | static JVMTI() 14 | { 15 | if (Env == null) 16 | { 17 | unsafe 18 | { 19 | var err = JNI.VM->Functions->GetEnv(JNI.VM, out IntPtr env, (int)JVMTI.Version.V1); 20 | 21 | if (err != JNI.Result.Ok) 22 | throw new Exception("Failed to initialize JVMTI"); 23 | 24 | if (env == IntPtr.Zero) 25 | throw new Exception($"Failed to initialize JVMTI"); 26 | 27 | Env = (JVMTIEnv*)env; 28 | } 29 | } 30 | } 31 | 32 | public static JClass GetLoadedClass(string sig) 33 | { 34 | if (LoadedClassCache.TryGetValue(sig, out JClass found)) 35 | { 36 | return found; 37 | } 38 | else 39 | { 40 | foreach (JClass cls in GetLoadedClasses()) 41 | { 42 | if (GetClassSignature(cls).Item1 == sig) 43 | { 44 | JClass global = JNI.NewGlobalRef(cls); 45 | 46 | LoadedClassCache.Add(sig, global); 47 | return global; 48 | } 49 | } 50 | } 51 | 52 | return null; 53 | } 54 | 55 | public static IEnumerable GetLoadedClasses() 56 | { 57 | unsafe 58 | { 59 | var err = Env->Functions->GetLoadedClasses(Env, out int length, out IntPtr arrayHandle); 60 | 61 | if (err != JVMTI.Error.None) 62 | throw new JVMTIErrorException(err); 63 | 64 | return new JVMTIArray(arrayHandle, length); 65 | } 66 | } 67 | 68 | public static Tuple GetClassSignature(JClass cls) 69 | { 70 | var err = Env->Functions->GetClassSignature(Env, cls.Handle, out IntPtr sigPtr, out IntPtr genericPtr); 71 | 72 | if (err != JVMTI.Error.None && err != JVMTI.Error.ClassNotPrepared) 73 | throw new JVMTIErrorException(err); 74 | 75 | string sigString = null; 76 | string genericString = null; 77 | 78 | if (sigPtr != IntPtr.Zero) 79 | { 80 | sigString = Marshal.PtrToStringAnsi(sigPtr); 81 | 82 | if (genericPtr != IntPtr.Zero) 83 | { 84 | genericString = Marshal.PtrToStringAnsi(genericPtr); 85 | } 86 | } 87 | Deallocate(sigPtr); 88 | Deallocate(genericPtr); 89 | 90 | return new Tuple(sigString, genericString); 91 | } 92 | 93 | public static JClassSignature GetSignature(this JClass cls) 94 | { 95 | unsafe 96 | { 97 | var err = Env->Functions->GetClassSignature(Env, cls.Handle, out IntPtr sigPtr, out IntPtr genericPtr); 98 | 99 | if (err != JVMTI.Error.None && err != JVMTI.Error.ClassNotPrepared) 100 | throw new JVMTIErrorException(err); 101 | 102 | string sigString = null; 103 | string genericString = null; 104 | 105 | if (sigPtr != IntPtr.Zero) 106 | { 107 | sigString = Marshal.PtrToStringAnsi(sigPtr); 108 | 109 | if (genericPtr != IntPtr.Zero) 110 | { 111 | genericString = Marshal.PtrToStringAnsi(genericPtr); 112 | } 113 | } 114 | 115 | Deallocate(sigPtr); 116 | Deallocate(genericPtr); 117 | 118 | return new JClassSignature( 119 | sigString, 120 | genericString, 121 | cls.GetFields().Select(f => f.GetSignature(cls)), 122 | cls.GetMethods().Select(m => m.GetSignature())); 123 | } 124 | } 125 | 126 | public static IEnumerable GetMethods(this JClass cls) 127 | { 128 | unsafe 129 | { 130 | var err = Env->Functions->GetClassMethods(Env, cls.Handle, out int length, out IntPtr handle); 131 | if (err != JVMTI.Error.None && err != JVMTI.Error.ClassNotPrepared) 132 | throw new JVMTIErrorException(err); 133 | 134 | return new JVMTIArray(handle, length); 135 | } 136 | } 137 | 138 | public static IEnumerable GetFields(this JClass cls) 139 | { 140 | unsafe 141 | { 142 | var err = Env->Functions->GetClassFields(Env, cls.Handle, out int length, out IntPtr handle); 143 | if (err != JVMTI.Error.None && err != JVMTI.Error.ClassNotPrepared) 144 | throw new JVMTIErrorException(err); 145 | 146 | return new JVMTIArray(handle, length); 147 | } 148 | } 149 | 150 | public static JMethodSignature GetSignature(this JMethodID methodID) 151 | { 152 | unsafe 153 | { 154 | var err = Env->Functions->GetMethodName(Env, methodID, out IntPtr namePtr, out IntPtr sigPtr, out IntPtr genericPtr); 155 | if (err != JVMTI.Error.None && err != JVMTI.Error.ClassNotPrepared) 156 | throw new JVMTIErrorException(err); 157 | 158 | string name = null; 159 | string sig = null; 160 | string generic = null; 161 | 162 | if (namePtr != IntPtr.Zero) 163 | { 164 | name = Marshal.PtrToStringAnsi(namePtr); 165 | 166 | if (sigPtr != IntPtr.Zero) 167 | { 168 | sig = Marshal.PtrToStringAnsi(sigPtr); 169 | if (genericPtr != IntPtr.Zero) 170 | { 171 | generic = Marshal.PtrToStringAnsi(genericPtr); 172 | } 173 | } 174 | } 175 | 176 | Deallocate(namePtr); 177 | Deallocate(sigPtr); 178 | Deallocate(genericPtr); 179 | 180 | err = Env->Functions->GetMethodModifiers(Env, methodID, out int modifier); 181 | if (err != JVMTI.Error.None) 182 | throw new Exception($"Failed to get JMethodID modifier. Error: {err}"); 183 | 184 | 185 | return new JMethodSignature(name, sig, generic, (JMethodAccessFlags)modifier); 186 | } 187 | } 188 | 189 | public static JFieldSignature GetSignature(this JFieldID fieldID, JClass cls) 190 | { 191 | unsafe 192 | { 193 | var err = Env->Functions->GetFieldName(Env, cls.Handle, fieldID, out IntPtr namePtr, out IntPtr sigPtr, out IntPtr genericPtr); 194 | if (err != JVMTI.Error.None && err != JVMTI.Error.ClassNotPrepared) 195 | throw new JVMTIErrorException(err); 196 | 197 | string name = null; 198 | string sig = null; 199 | string generic = null; 200 | 201 | if (namePtr != IntPtr.Zero) 202 | { 203 | name = Marshal.PtrToStringAnsi(namePtr); 204 | Deallocate(namePtr); 205 | if (sigPtr != IntPtr.Zero) 206 | { 207 | sig = Marshal.PtrToStringAnsi(sigPtr); 208 | Deallocate(sigPtr); 209 | if (genericPtr != IntPtr.Zero) 210 | { 211 | generic = Marshal.PtrToStringAnsi(genericPtr); 212 | Deallocate(genericPtr); 213 | } 214 | } 215 | } 216 | 217 | err = Env->Functions->GetFieldModifiers(Env, cls.Handle, fieldID, out int flags); 218 | if (err != JVMTI.Error.None && err != JVMTI.Error.ClassNotPrepared) 219 | throw new JVMTIErrorException(err); 220 | 221 | return new JFieldSignature(null, sig, generic, (JFieldAccessFlags)flags); 222 | } 223 | } 224 | 225 | public static void Deallocate(IntPtr address) 226 | { 227 | unsafe 228 | { 229 | Env->Functions->Deallocate(Env, address); 230 | } 231 | } 232 | } 233 | -------------------------------------------------------------------------------- /JNISharp/ToolInterface/EventCallbacks.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.ToolInterface; 2 | 3 | using JNISharp.NativeInterface; 4 | using System.Runtime.InteropServices; 5 | 6 | 7 | public static partial class JVMTI 8 | { 9 | [StructLayout(LayoutKind.Sequential)] 10 | internal unsafe struct EventCallbacks 11 | { 12 | /* 13 | * typedef void (JNICALL *jvmtiEventVMInit) 14 | (jvmtiEnv *jvmti_env, 15 | JNIEnv* jni_env, 16 | jthread thread); 17 | */ 18 | public delegate* unmanaged[Stdcall] VMInit; 19 | 20 | /* 21 | * typedef void (JNICALL *jvmtiEventVMDeath) 22 | (jvmtiEnv *jvmti_env, 23 | JNIEnv* jni_env); 24 | */ 25 | public delegate* unmanaged[Stdcall] VMDeath; 26 | 27 | /* 28 | * typedef void (JNICALL *jvmtiEventThreadStart) 29 | (jvmtiEnv *jvmti_env, 30 | JNIEnv* jni_env, 31 | jthread thread); 32 | */ 33 | public delegate* unmanaged[Stdcall] ThreadStart; 34 | 35 | /* 36 | * typedef void (JNICALL *jvmtiEventThreadEnd) 37 | (jvmtiEnv *jvmti_env, 38 | JNIEnv* jni_env, 39 | jthread thread); 40 | */ 41 | public delegate* unmanaged[Stdcall] ThreadEnd; 42 | 43 | /* 44 | * typedef void (JNICALL *jvmtiEventClassFileLoadHook) 45 | (jvmtiEnv *jvmti_env, 46 | JNIEnv* jni_env, 47 | jclass class_being_redefined, 48 | jobject loader, 49 | const char* name, 50 | jobject protection_domain, 51 | jint class_data_len, 52 | const unsigned char* class_data, 53 | jint* new_class_data_len, 54 | unsigned char** new_class_data); 55 | */ 56 | public delegate* unmanaged[Stdcall] ClassFileLoadHook; 57 | 58 | public delegate* unmanaged[Stdcall] ClassLoad; 59 | 60 | /* 61 | * typedef void (JNICALL *jvmtiEventClassPrepare) 62 | (jvmtiEnv *jvmti_env, 63 | JNIEnv* jni_env, 64 | jthread thread, 65 | jclass klass); 66 | */ 67 | public delegate* unmanaged[Stdcall] ClassPrepare; 68 | 69 | /* 70 | * typedef void (JNICALL *jvmtiEventVMStart) 71 | (jvmtiEnv *jvmti_env, 72 | JNIEnv* jni_env); 73 | */ 74 | public delegate* unmanaged[Stdcall] VmStart; 75 | 76 | /* 77 | * typedef void (JNICALL *jvmtiEventException) 78 | (jvmtiEnv *jvmti_env, 79 | JNIEnv* jni_env, 80 | jthread thread, 81 | jmethodID method, 82 | jlocation location, 83 | jobject exception, 84 | jmethodID catch_method, 85 | jlocation catch_location); 86 | */ 87 | public delegate* unmanaged[Stdcall] Exception; 88 | 89 | /* 90 | * typedef void (JNICALL *jvmtiEventExceptionCatch) 91 | (jvmtiEnv *jvmti_env, 92 | JNIEnv* jni_env, 93 | jthread thread, 94 | jmethodID method, 95 | jlocation location, 96 | jobject exception); 97 | */ 98 | public delegate* unmanaged[Stdcall] ExceptionCatch; 99 | 100 | /* 101 | * typedef void (JNICALL *jvmtiEventSingleStep) 102 | (jvmtiEnv *jvmti_env, 103 | JNIEnv* jni_env, 104 | jthread thread, 105 | jmethodID method, 106 | jlocation location); 107 | */ 108 | public delegate* unmanaged[Stdcall] SingleStep; 109 | 110 | /* 111 | * typedef void (JNICALL *jvmtiEventFramePop) 112 | (jvmtiEnv *jvmti_env, 113 | JNIEnv* jni_env, 114 | jthread thread, 115 | jmethodID method, 116 | jboolean was_popped_by_exception); 117 | */ 118 | public delegate* unmanaged[Stdcall] FramePop; 119 | 120 | /* 121 | * typedef void (JNICALL *jvmtiEventBreakpoint) 122 | (jvmtiEnv *jvmti_env, 123 | JNIEnv* jni_env, 124 | jthread thread, 125 | jmethodID method, 126 | jlocation location); 127 | */ 128 | public delegate* unmanaged[Stdcall] BreakPoint; 129 | 130 | /* 131 | * typedef void (JNICALL *jvmtiEventFieldAccess) 132 | (jvmtiEnv *jvmti_env, 133 | JNIEnv* jni_env, 134 | jthread thread, 135 | jmethodID method, 136 | jlocation location, 137 | jclass field_klass, 138 | jobject object, 139 | jfieldID field); 140 | */ 141 | public delegate* unmanaged[Stdcall] FieldAccess; 142 | 143 | /* 144 | * typedef void (JNICALL *jvmtiEventFieldModification) 145 | (jvmtiEnv *jvmti_env, 146 | JNIEnv* jni_env, 147 | jthread thread, 148 | jmethodID method, 149 | jlocation location, 150 | jclass field_klass, 151 | jobject object, 152 | jfieldID field, 153 | char signature_type, 154 | jvalue new_value); 155 | */ 156 | public delegate* unmanaged[Stdcall] FieldModify; 157 | 158 | /* 159 | * typedef void (JNICALL *jvmtiEventMethodEntry) 160 | (jvmtiEnv *jvmti_env, 161 | JNIEnv* jni_env, 162 | jthread thread, 163 | jmethodID method); 164 | */ 165 | public delegate* unmanaged[Stdcall] MethodEntry; 166 | 167 | /* 168 | * typedef void (JNICALL *jvmtiEventMethodExit) 169 | (jvmtiEnv *jvmti_env, 170 | JNIEnv* jni_env, 171 | jthread thread, 172 | jmethodID method, 173 | jboolean was_popped_by_exception, 174 | jvalue return_value); 175 | */ 176 | public delegate* unmanaged[Stdcall] MethodExit; 177 | 178 | /* 179 | * typedef void (JNICALL *jvmtiEventNativeMethodBind) 180 | (jvmtiEnv *jvmti_env, 181 | JNIEnv* jni_env, 182 | jthread thread, 183 | jmethodID method, 184 | void* address, 185 | void** new_address_ptr); 186 | */ 187 | public delegate* unmanaged[Stdcall] NativeMethodBind; 188 | 189 | /* 190 | * typedef void (JNICALL *jvmtiEventCompiledMethodLoad) 191 | (jvmtiEnv *jvmti_env, 192 | jmethodID method, 193 | jint code_size, 194 | const void* code_addr, 195 | jint map_length, 196 | const jvmtiAddrLocationMap* map, 197 | const void* compile_info); 198 | */ 199 | public delegate* unmanaged[Stdcall] CompiledMethodLoad; 200 | 201 | /* 202 | * typedef void (JNICALL *jvmtiEventCompiledMethodUnload) 203 | (jvmtiEnv *jvmti_env, 204 | jmethodID method, 205 | const void* code_addr); 206 | */ 207 | public delegate* unmanaged[Stdcall] CompiledMethodUnload; 208 | 209 | /* 210 | * typedef void (JNICALL *jvmtiEventDynamicCodeGenerated) 211 | (jvmtiEnv *jvmti_env, 212 | const char* name, 213 | const void* address, 214 | jint length); 215 | */ 216 | public delegate* unmanaged[Stdcall] DynamicCodeGenerated; 217 | 218 | /* 219 | * typedef void (JNICALL *jvmtiEventDataDumpRequest) 220 | (jvmtiEnv *jvmti_env); 221 | */ 222 | public delegate* unmanaged[Stdcall] DataDumpRequest; 223 | 224 | public IntPtr Reserved72; 225 | 226 | /* 227 | * typedef void (JNICALL *jvmtiEventMonitorWait) 228 | (jvmtiEnv *jvmti_env, 229 | JNIEnv* jni_env, 230 | jthread thread, 231 | jobject object, 232 | jlong timeout); 233 | */ 234 | public delegate* unmanaged[Stdcall] MonitorWait; 235 | 236 | /* 237 | * typedef void (JNICALL *jvmtiEventMonitorWaited) 238 | (jvmtiEnv *jvmti_env, 239 | JNIEnv* jni_env, 240 | jthread thread, 241 | jobject object, 242 | jboolean timed_out); 243 | */ 244 | public delegate* unmanaged[Stdcall] MonitorWaited; 245 | 246 | /* 247 | * typedef void (JNICALL *jvmtiEventMonitorContendedEnter) 248 | (jvmtiEnv *jvmti_env, 249 | JNIEnv* jni_env, 250 | jthread thread, 251 | jobject object); 252 | */ 253 | public delegate* unmanaged[Stdcall] MonitorContendedEnter; 254 | 255 | /* 256 | * typedef void (JNICALL *jvmtiEventMonitorContendedEntered) 257 | (jvmtiEnv *jvmti_env, 258 | JNIEnv* jni_env, 259 | jthread thread, 260 | jobject object); 261 | */ 262 | public delegate* unmanaged[Stdcall] MonitorContendedEnterer; 263 | 264 | public IntPtr Reserved77; 265 | 266 | public IntPtr Reserved78; 267 | 268 | public IntPtr Reserved79; 269 | 270 | /* 271 | * typedef void (JNICALL *jvmtiEventResourceExhausted) 272 | (jvmtiEnv *jvmti_env, 273 | JNIEnv* jni_env, 274 | jint flags, 275 | const void* reserved, 276 | const char* description); 277 | */ 278 | public delegate* unmanaged[Stdcall] ResourceExhausted; 279 | 280 | /* 281 | * typedef void (JNICALL *jvmtiEventGarbageCollectionStart) 282 | (jvmtiEnv *jvmti_env); 283 | */ 284 | public delegate* unmanaged[Stdcall] GarbageCollectionStart; 285 | 286 | /* 287 | * typedef void (JNICALL *jvmtiEventGarbageCollectionFinish) 288 | (jvmtiEnv *jvmti_env); 289 | */ 290 | public delegate* unmanaged[Stdcall] GarbageCollectionFinish; 291 | 292 | /* 293 | * typedef void (JNICALL *jvmtiEventObjectFree) 294 | (jvmtiEnv *jvmti_env, 295 | jlong tag); 296 | */ 297 | public delegate* unmanaged[Stdcall] ObjectFree; 298 | 299 | /* 300 | * typedef void (JNICALL *jvmtiEventVMObjectAlloc) 301 | (jvmtiEnv *jvmti_env, 302 | JNIEnv* jni_env, 303 | jthread thread, 304 | jobject object, 305 | jclass object_klass, 306 | jlong size); 307 | */ 308 | public delegate* unmanaged[Stdcall] VmObjectAlloc; 309 | } 310 | } 311 | -------------------------------------------------------------------------------- /JNISharp/ToolInterface/JVMTIEnv.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.ToolInterface; 2 | 3 | using System.Runtime.InteropServices; 4 | 5 | [StructLayout(LayoutKind.Sequential)] 6 | internal readonly unsafe struct JVMTIEnv 7 | { 8 | [StructLayout(LayoutKind.Sequential)] 9 | internal readonly struct FunctionTable 10 | { 11 | internal readonly IntPtr Reserved1; 12 | 13 | // jvmtiError (JNICALL *SetEventNotificationMode) (jvmtiEnv* env, jvmtiEventMode mode, jvmtiEvent event_type, jthread event_thread, ...); 14 | internal readonly delegate* unmanaged[Stdcall] SetEventNotificationMode; 15 | 16 | internal readonly IntPtr Reserved3; 17 | 18 | // jvmtiError (JNICALL *GetAllThreads) (jvmtiEnv* env, jint* threads_count_ptr, jthread** threads_ptr); 19 | internal readonly delegate* unmanaged[Stdcall] GetAllThreads; 20 | 21 | // jvmtiError (JNICALL *SuspendThread) (jvmtiEnv* env, jthread thread); 22 | internal readonly delegate* unmanaged[Stdcall] SuspendThread; 23 | 24 | internal readonly IntPtr ResumeThread; 25 | 26 | internal readonly IntPtr StopThread; 27 | 28 | internal readonly IntPtr InterruptThread; 29 | 30 | internal readonly IntPtr GetThreadInfo; 31 | 32 | internal readonly IntPtr GetOwnedMonitorInfo; 33 | 34 | internal readonly IntPtr GetCurrentContendedMonitor; 35 | 36 | internal readonly IntPtr RunAgentThread; 37 | 38 | internal readonly IntPtr GetTopThreadGroups; 39 | 40 | internal readonly IntPtr GetThreadGroupInfo; 41 | 42 | internal readonly IntPtr GetThreadGroupChildren; 43 | 44 | internal readonly IntPtr GetFrameCount; 45 | 46 | internal readonly IntPtr GetThreadState; 47 | 48 | internal readonly IntPtr GetCurrentThread; 49 | 50 | internal readonly IntPtr GetFrameLocation; 51 | 52 | internal readonly IntPtr NotifyFramePop; 53 | 54 | internal readonly IntPtr GetLocalObject; 55 | 56 | internal readonly IntPtr GetLocalInt; 57 | 58 | internal readonly IntPtr GetLocalLong; 59 | 60 | internal readonly IntPtr GetLocalFloat; 61 | 62 | internal readonly IntPtr GetLocalDouble; 63 | 64 | internal readonly IntPtr SetLocalObject; 65 | 66 | internal readonly IntPtr SetLocalInt; 67 | 68 | internal readonly IntPtr SetLocalLong; 69 | 70 | internal readonly IntPtr SetLocalFloat; 71 | 72 | internal readonly IntPtr SetLocalDouble; 73 | 74 | internal readonly IntPtr CreateRawMonitor; 75 | 76 | internal readonly IntPtr DestroyRawMonitor; 77 | 78 | internal readonly IntPtr RawMonitorEnter; 79 | 80 | internal readonly IntPtr RawMonitorExit; 81 | 82 | internal readonly IntPtr RawMonitorWait; 83 | 84 | internal readonly IntPtr RawMonitorNotify; 85 | 86 | internal readonly IntPtr RawMonitorNotifyAll; 87 | 88 | internal readonly IntPtr SetBreakpoint; 89 | 90 | internal readonly IntPtr ClearBreakpoint; 91 | 92 | internal readonly IntPtr Reserved40; 93 | 94 | internal readonly IntPtr SetFieldAccessWatch; 95 | 96 | internal readonly IntPtr ClearFieldAccessWatch; 97 | 98 | internal readonly IntPtr SetFieldModificationWatch; 99 | 100 | internal readonly IntPtr ClearFieldModificationWatch; 101 | 102 | internal readonly IntPtr IsModifiableClass; 103 | 104 | internal readonly IntPtr Allocate; 105 | 106 | // jvmtiError (JNICALL *Deallocate) (jvmtiEnv* env, unsigned char* mem); 107 | internal readonly delegate* unmanaged[Stdcall] Deallocate; 108 | 109 | // jvmtiError (JNICALL *GetClassSignature) (jvmtiEnv* env, jclass klass, char** signature_ptr, char** generic_ptr); 110 | internal readonly delegate* unmanaged[Stdcall] GetClassSignature; 111 | 112 | internal readonly IntPtr GetClassStatus; 113 | 114 | // jvmtiError (JNICALL *GetSourceFileName) (jvmtiEnv* env, jclass klass, char** source_name_ptr); 115 | internal readonly delegate* unmanaged[Stdcall] GetSourceFileName; 116 | 117 | // jvmtiError (JNICALL *GetClassModifiers) (jvmtiEnv* env, jclass klass, jint* modifiers_ptr); 118 | internal readonly delegate* unmanaged[Stdcall] GetClassModifiers; 119 | 120 | // jvmtiError (JNICALL *GetClassMethods) (jvmtiEnv* env, jclass klass, jint* method_count_ptr, jmethodID** methods_ptr); 121 | internal readonly delegate* unmanaged[Stdcall] GetClassMethods; 122 | 123 | // jvmtiError (JNICALL *GetClassFields) (jvmtiEnv* env, jclass klass, jint* field_count_ptr, jfieldID** fields_ptr); 124 | internal readonly delegate* unmanaged[Stdcall] GetClassFields; 125 | 126 | // jvmtiError (JNICALL *GetImplementedInterfaces) (jvmtiEnv* env, jclass klass, jint* interface_count_ptr, jclass** interfaces_ptr); 127 | internal readonly delegate* unmanaged[Stdcall] GetImplementedInterfaces; 128 | 129 | internal readonly IntPtr IsInterface; 130 | 131 | internal readonly IntPtr IsArrayClass; 132 | 133 | internal readonly IntPtr GetClassLoader; 134 | 135 | internal readonly IntPtr GetObjectHashCode; 136 | 137 | internal readonly IntPtr GetObjectMonitorUsage; 138 | 139 | // jvmtiError (JNICALL *GetFieldName) (jvmtiEnv* env, jclass klass, jfieldID field, char** name_ptr, char** signature_ptr, char** generic_ptr); 140 | internal readonly delegate* unmanaged[Stdcall] GetFieldName; 141 | 142 | internal readonly IntPtr GetFieldDeclaringClass; 143 | 144 | // jvmtiError (JNICALL *GetFieldModifiers) (jvmtiEnv* env, jclass klass, jfieldID field, jint* modifiers_ptr); 145 | internal readonly delegate* unmanaged[Stdcall] GetFieldModifiers; 146 | 147 | internal readonly IntPtr IsFieldSynthetic; 148 | 149 | // jvmtiError (JNICALL *GetMethodName) (jvmtiEnv* env, jmethodID method, char** name_ptr, char** signature_ptr, char** generic_ptr); 150 | internal readonly delegate* unmanaged[Stdcall] GetMethodName; 151 | 152 | internal readonly IntPtr GetMethodDeclaringClass; 153 | 154 | // jvmtiError (JNICALL *GetMethodModifiers) (jvmtiEnv* env, jmethodID method, jint* modifiers_ptr); 155 | internal readonly delegate* unmanaged[Stdcall] GetMethodModifiers; 156 | 157 | internal readonly IntPtr Reserved67; 158 | 159 | internal readonly IntPtr GetMaxLocals; 160 | 161 | internal readonly IntPtr GetArgumentsSize; 162 | 163 | internal readonly IntPtr GetLineNumberTable; 164 | 165 | internal readonly IntPtr GetMethodLocation; 166 | 167 | internal readonly IntPtr GetLocalVariableTable; 168 | 169 | internal readonly IntPtr SetNativeMethodPrefix; 170 | 171 | internal readonly IntPtr SetNativeMethodPrefixes; 172 | 173 | // jvmtiError (JNICALL *GetBytecodes) (jvmtiEnv* env, jmethodID method, jint* bytecode_count_ptr, unsigned char** bytecodes_ptr); 174 | internal readonly delegate* unmanaged[Stdcall] GetBytecodes; 175 | 176 | internal readonly IntPtr IsMethodNative; 177 | 178 | internal readonly IntPtr IsMethodSynthetic; 179 | 180 | // jvmtiError (JNICALL *GetLoadedClasses) (jvmtiEnv* env, jint* class_count_ptr, jclass** classes_ptr); 181 | internal readonly delegate* unmanaged[Stdcall] GetLoadedClasses; 182 | 183 | internal readonly IntPtr GetClassLoaderClasses; 184 | 185 | internal readonly IntPtr PopFrame; 186 | 187 | internal readonly IntPtr ForceEarlyReturnObject; 188 | 189 | internal readonly IntPtr ForceEarlyReturnInt; 190 | 191 | internal readonly IntPtr ForceEarlyReturnLong; 192 | 193 | internal readonly IntPtr ForceEarlyReturnFloat; 194 | 195 | internal readonly IntPtr ForceEarlyReturnDouble; 196 | 197 | internal readonly IntPtr ForceEarlyReturnVoid; 198 | 199 | internal readonly IntPtr RedefineClasses; 200 | 201 | internal readonly IntPtr GetVersionNumber; 202 | 203 | // jvmtiError (JNICALL *GetCapabilities) (jvmtiEnv* env, jvmtiCapabilities* capabilities_ptr); 204 | internal readonly delegate* unmanaged[Stdcall] GetCapabilities; 205 | 206 | internal readonly IntPtr GetSourceDebugExtension; 207 | 208 | internal readonly IntPtr IsMethodObsolete; 209 | 210 | internal readonly IntPtr SuspendThreadList; 211 | 212 | internal readonly IntPtr ResumeThreadList; 213 | 214 | internal readonly IntPtr Reserved94; 215 | 216 | internal readonly IntPtr Reserved95; 217 | 218 | internal readonly IntPtr Reserved96; 219 | 220 | internal readonly IntPtr Reserved97; 221 | 222 | internal readonly IntPtr Reserved98; 223 | 224 | internal readonly IntPtr Reserved99; 225 | 226 | internal readonly IntPtr GetAllStackTraces; 227 | 228 | internal readonly IntPtr GetThreadListStackTraces; 229 | 230 | internal readonly IntPtr GetThreadLocalStorage; 231 | 232 | internal readonly IntPtr SetThreadLocalStorage; 233 | 234 | internal readonly IntPtr GetStackTrace; 235 | 236 | internal readonly IntPtr Reserved105; 237 | 238 | internal readonly IntPtr GetTag; 239 | 240 | internal readonly IntPtr SetTag; 241 | 242 | internal readonly IntPtr ForceGarbageCollection; 243 | 244 | internal readonly IntPtr IterateOverObjectsReachableFromObject; 245 | 246 | internal readonly IntPtr IterateOverReachableObjects; 247 | 248 | internal readonly IntPtr IterateOverHeap; 249 | 250 | internal readonly IntPtr IterateOverInstancesOfClass; 251 | 252 | internal readonly IntPtr Reserved113; 253 | 254 | internal readonly IntPtr GetObjectsWithTags; 255 | 256 | internal readonly IntPtr FollowReferences; 257 | 258 | internal readonly IntPtr IterateThroughHeap; 259 | 260 | internal readonly IntPtr Reserved117; 261 | 262 | internal readonly IntPtr Reserved118; 263 | 264 | internal readonly IntPtr Reserved119; 265 | 266 | internal readonly IntPtr SetJNIFunctionTable; 267 | 268 | internal readonly IntPtr GetJNIFunctionTable; 269 | 270 | // jvmtiError (JNICALL *SetEventCallbacks) (jvmtiEnv* env, const jvmtiEventCallbacks* callbacks, jint size_of_callbacks); 271 | internal readonly delegate* unmanaged[Stdcall] SetEventCallbacks; 272 | 273 | internal readonly IntPtr GenerateEvents; 274 | 275 | internal readonly IntPtr GetExtensionFunctions; 276 | 277 | internal readonly IntPtr GetExtensionEvents; 278 | 279 | internal readonly IntPtr SetExtensionEventCallback; 280 | 281 | internal readonly IntPtr DisposeEnvironment; 282 | 283 | internal readonly IntPtr GetErrorName; 284 | 285 | internal readonly IntPtr GetJLocationFormat; 286 | 287 | // jvmtiError (JNICALL *GetSystemProperties) (jvmtiEnv* env, jint* count_ptr,char*** property_ptr); 288 | internal readonly delegate* unmanaged[Stdcall] GetSystemProperties; 289 | 290 | // jvmtiError (JNICALL *GetSystemProperty) (jvmtiEnv* env, const char* property, char** value_ptr); 291 | internal readonly delegate* unmanaged[Stdcall] GetSystemProperty; 292 | 293 | internal readonly IntPtr SetSystemProperty; 294 | 295 | internal readonly IntPtr GetPhase; 296 | 297 | internal readonly IntPtr GetCurrentThreadCpuTimerInfo; 298 | 299 | internal readonly IntPtr GetCurrentThreadCpuTime; 300 | 301 | internal readonly IntPtr GetThreadCpuTimerInfo; 302 | 303 | internal readonly IntPtr GetThreadCpuTime; 304 | 305 | internal readonly IntPtr GetTimerInfo; 306 | 307 | internal readonly IntPtr GetTime; 308 | 309 | // jvmtiError (JNICALL *GetPotentialCapabilities) (jvmtiEnv* env, jvmtiCapabilities* capabilities_ptr); 310 | //internal readonly delegate* unmanaged[Stdcall] GetPotentialCapabilities; 311 | 312 | internal readonly IntPtr Reserved141; 313 | 314 | // jvmtiError (JNICALL *AddCapabilities) (jvmtiEnv* env, const jvmtiCapabilities* capabilities_ptr); 315 | //internal readonly delegate* unmanaged[Stdcall] AddCapabilities; 316 | 317 | internal readonly IntPtr RelinquishCapabilities; 318 | 319 | internal readonly IntPtr GetAvailableProcessors; 320 | 321 | internal readonly IntPtr GetClassVersionNumbers; 322 | 323 | internal readonly IntPtr GetConstantPool; 324 | 325 | internal readonly IntPtr GetEnvironmentLocalStorage; 326 | 327 | internal readonly IntPtr SetEnvironmentLocalStorage; 328 | 329 | internal readonly IntPtr AddToBootstrapClassLoaderSearch; 330 | 331 | internal readonly IntPtr SetVerboseFlag; 332 | 333 | internal readonly IntPtr AddToSystemClassLoaderSearch; 334 | 335 | internal readonly IntPtr RetransformClasses; 336 | 337 | internal readonly IntPtr GetOwnedMonitorStackDepthInfo; 338 | 339 | internal readonly IntPtr GetObjectSize; 340 | 341 | internal readonly IntPtr GetLocalInstance; 342 | } 343 | 344 | internal readonly FunctionTable* Functions; 345 | } 346 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/JNIEnv.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | using System.Runtime.InteropServices; 4 | 5 | /// 6 | /// Represents a JNIEnv C struct, the FunctionTable contains 7 | /// all the JNI functions. 8 | /// 9 | [StructLayout(LayoutKind.Sequential)] 10 | internal readonly unsafe struct JNIEnv 11 | { 12 | [StructLayout(LayoutKind.Sequential)] 13 | internal readonly unsafe struct FunctionTable 14 | { 15 | internal readonly IntPtr Reserved0; 16 | 17 | internal readonly IntPtr Reserved1; 18 | 19 | internal readonly IntPtr Reserved2; 20 | 21 | internal readonly IntPtr Reserved3; 22 | 23 | // jint (JNICALL *GetVersion)(JNIEnv *env); 24 | internal readonly delegate* unmanaged[Stdcall] GetVersion; 25 | 26 | // jclass (JNICALL *DefineClass) (JNIEnv* env, const char* name, jobject loader, const jbyte* buf, jsize len); 27 | internal readonly delegate* unmanaged[Stdcall] DefineClass; 28 | 29 | // jclass (JNICALL *FindClass) (JNIEnv* env, const char* name); 30 | internal readonly delegate* unmanaged[Stdcall] FindClass; 31 | 32 | // jmethodID (JNICALL *FromReflectedMethod) (JNIEnv* env, jobject method); 33 | internal readonly delegate* unmanaged[Stdcall] FromReflectedMethod; 34 | 35 | // jfieldID (JNICALL *FromReflectedField) (JNIEnv* env, jobject field); 36 | internal readonly delegate* unmanaged[Stdcall] FromReflectedField; 37 | 38 | // jobject (JNICALL *ToReflectedMethod) (JNIEnv* env, jclass cls, jmethodID methodID, jboolean isStatic); 39 | internal readonly delegate* unmanaged[Stdcall] ToReflectedMethod; 40 | 41 | // jclass (JNICALL *GetSuperclass) (JNIEnv* env, jclass sub); 42 | internal readonly delegate* unmanaged[Stdcall] GetSuperClass; 43 | 44 | // jboolean (JNICALL *IsAssignableFrom) (JNIEnv* env, jclass sub, jclass sup); 45 | internal readonly delegate* unmanaged[Stdcall] IsAssignableFrom; 46 | 47 | // jobject (JNICALL *ToReflectedField) (JNIEnv* env, jclass cls, jfieldID fieldID, jboolean isStatic); 48 | internal readonly delegate* unmanaged[Stdcall] ToReflectedField; 49 | 50 | // jint (JNICALL *Throw) (JNIEnv* env, jthrowable obj); 51 | internal readonly delegate* unmanaged[Stdcall] Throw; 52 | 53 | // jint (JNICALL *ThrowNew) (JNIEnv* env, jclass clazz, const char* msg); 54 | internal readonly delegate* unmanaged[Stdcall] ThrowNew; 55 | 56 | // jthrowable (JNICALL *ExceptionOccurred) (JNIEnv* env); 57 | internal readonly delegate* unmanaged[Stdcall] ExceptionOccurred; 58 | 59 | // void (JNICALL *ExceptionDescribe) (JNIEnv* env); 60 | internal readonly delegate* unmanaged[Stdcall] ExceptionDescribe; 61 | 62 | // void (JNICALL *ExceptionClear) (JNIEnv* env); 63 | internal readonly delegate* unmanaged[Stdcall] ExceptionClear; 64 | 65 | // void (JNICALL *FatalError) (JNIEnv* env, const char* msg); 66 | internal readonly delegate* unmanaged[Stdcall] FatalError; 67 | 68 | // jint (JNICALL *PushLocalFrame) (JNIEnv* env, jint capacity); 69 | internal readonly delegate* unmanaged[Stdcall] PushLocalFrame; 70 | 71 | // jobject (JNICALL *PopLocalFrame) (JNIEnv* env, jobject result); 72 | internal readonly delegate* unmanaged[Stdcall] PopLocalFrame; 73 | 74 | // jobject (JNICALL *NewGlobalRef) (JNIEnv* env, jobject lobj); 75 | internal readonly delegate* unmanaged[Stdcall] NewGlobalRef; 76 | 77 | // void (JNICALL *DeleteGlobalRef) (JNIEnv* env, jobject gref); 78 | internal readonly delegate* unmanaged[Stdcall] DeleteGlobalRef; 79 | 80 | // void (JNICALL *DeleteLocalRef) (JNIEnv* env, jobject obj); 81 | internal readonly delegate* unmanaged[Stdcall] DeleteLocalRef; 82 | 83 | // jboolean (JNICALL *IsSameObject) (JNIEnv* env, jobject obj1, jobject obj2); 84 | internal readonly delegate* unmanaged[Stdcall] IsSameObject; 85 | 86 | // jobject (JNICALL *NewLocalRef) (JNIEnv* env, jobject ref); 87 | internal readonly delegate* unmanaged[Stdcall] NewLocalRef; 88 | 89 | // jint (JNICALL *EnsureLocalCapacity) (JNIEnv* env, jint capacity); 90 | internal readonly delegate* unmanaged[Stdcall] EnsureLocalCapacity; 91 | 92 | // jobject (JNICALL *AllocObject) (JNIEnv* env, jclass clazz); 93 | internal readonly delegate* unmanaged[Stdcall] AllocObject; 94 | 95 | // jobject (JNICALL *NewObject) (JNIEnv* env, jclass clazz, jmethodID methodID, ...); 96 | internal readonly IntPtr NewObject; 97 | 98 | // jobject (JNICALL *NewObjectV) (JNIEnv* env, jclass clazz, jmethodID methodID, va_list args); 99 | internal readonly IntPtr NewObjectV; 100 | 101 | // jobject (JNICALL *NewObjectA) (JNIEnv* env, jclass clazz, jmethodID methodID, const jvalue* args); 102 | internal readonly delegate* unmanaged[Stdcall] NewObjectA; 103 | 104 | // jclass (JNICALL *GetObjectClass) (JNIEnv* env, jobject obj); 105 | internal readonly delegate* unmanaged[Stdcall] GetObjectClass; 106 | 107 | // jboolean (JNICALL *IsInstanceOf) (JNIEnv* env, jobject obj, jclass clazz); 108 | internal readonly delegate* unmanaged[Stdcall] IsInstanceOf; 109 | 110 | // jmethodID (JNICALL *GetMethodID) (JNIEnv* env, jclass clazz, const char* name, const char* sig); 111 | internal readonly delegate* unmanaged[Stdcall] GetMethodID; 112 | 113 | // jobject (JNICALL *CallObjectMethod) (JNIEnv* env, jobject obj, jmethodID methodID, ...); 114 | internal readonly IntPtr CallObjectMethod; 115 | 116 | // jobject (JNICALL *CallObjectMethodV) (JNIEnv* env, jobject obj, jmethodID methodID, va_list args); 117 | internal readonly IntPtr CallObjectMethodV; 118 | 119 | // jobject (JNICALL *CallObjectMethodA) (JNIEnv* env, jobject obj, jmethodID methodID, const jvalue* args); 120 | internal readonly delegate* unmanaged[Stdcall] CallObjectMethodA; 121 | 122 | // jboolean (JNICALL *CallBooleanMethod) (JNIEnv* env, jobject obj, jmethodID methodID, ...); 123 | internal readonly IntPtr CallBooleanMethod; 124 | 125 | // jboolean (JNICALL *CallBooleanMethodV) (JNIEnv* env, jobject obj, jmethodID methodID, va_list args); 126 | internal readonly IntPtr CallBooleanMethodV; 127 | 128 | // jboolean (JNICALL *CallBooleanMethodA) (JNIEnv* env, jobject obj, jmethodID methodID, const jvalue* args); 129 | internal readonly delegate* unmanaged[Stdcall] CallBooleanMethodA; 130 | 131 | // jbyte (JNICALL *CallByteMethod) (JNIEnv* env, jobject obj, jmethodID methodID, ...); 132 | internal readonly IntPtr CallByteMethod; 133 | 134 | // jbyte (JNICALL *CallByteMethodV) (JNIEnv* env, jobject obj, jmethodID methodID, va_list args); 135 | internal readonly IntPtr CallByteMethodV; 136 | 137 | // jbyte (JNICALL *CallByteMethodA) (JNIEnv* env, jobject obj, jmethodID methodID, const jvalue* args); 138 | internal readonly delegate* unmanaged[Stdcall] CallByteMethodA; 139 | 140 | // jchar (JNICALL *CallCharMethod) (JNIEnv* env, jobject obj, jmethodID methodID, ...); 141 | internal readonly IntPtr CallCharMethod; 142 | 143 | // jchar (JNICALL *CallCharMethodV) (JNIEnv* env, jobject obj, jmethodID methodID, va_list args); 144 | internal readonly IntPtr CallCharMethodV; 145 | 146 | // jchar (JNICALL *CallCharMethodA) (JNIEnv* env, jobject obj, jmethodID methodID, const jvalue* args); 147 | internal readonly delegate* unmanaged[Stdcall] CallCharMethodA; 148 | 149 | // jshort (JNICALL *CallShortMethod) (JNIEnv* env, jobject obj, jmethodID methodID, ...); 150 | internal readonly IntPtr CallShortMethod; 151 | 152 | // jshort (JNICALL *CallShortMethodV) (JNIEnv* env, jobject obj, jmethodID methodID, va_list args); 153 | internal readonly IntPtr CallShortMethodV; 154 | 155 | // jshort (JNICALL *CallShortMethodA) (JNIEnv* env, jobject obj, jmethodID methodID, const jvalue* args); 156 | internal readonly delegate* unmanaged[Stdcall] CallShortMethodA; 157 | 158 | // jint (JNICALL *CallIntMethod) (JNIEnv* env, jobject obj, jmethodID methodID, ...); 159 | internal readonly IntPtr CallIntMethod; 160 | 161 | // jshort (JNICALL *CallShortMethodV) (JNIEnv* env, jobject obj, jmethodID methodID, va_list args); 162 | internal readonly IntPtr CallIntMethodV; 163 | 164 | // jint (JNICALL *CallIntMethodA) (JNIEnv* env, jobject obj, jmethodID methodID, const jvalue* args); 165 | internal readonly delegate* unmanaged[Stdcall] CallIntMethodA; 166 | 167 | // jlong (JNICALL *CallLongMethod) (JNIEnv* env, jobject obj, jmethodID methodID, ...); 168 | internal readonly IntPtr CallLongMethod; 169 | 170 | // jlong (JNICALL *CallLongMethodV) (JNIEnv* env, jobject obj, jmethodID methodID, va_list args); 171 | internal readonly IntPtr CallLongMethodV; 172 | 173 | // jlong (JNICALL *CallLongMethodA) (JNIEnv* env, jobject obj, jmethodID methodID, const jvalue* args); 174 | internal readonly delegate* unmanaged[Stdcall] CallLongMethodA; 175 | 176 | // jfloat (JNICALL *CallFloatMethod) (JNIEnv* env, jobject obj, jmethodID methodID, ...); 177 | internal readonly IntPtr CallFloatMethod; 178 | 179 | // jfloat (JNICALL *CallFloatMethodV) (JNIEnv* env, jobject obj, jmethodID methodID, va_list args); 180 | internal readonly IntPtr CallFloatMethodV; 181 | 182 | // jfloat (JNICALL *CallFloatMethodA) (JNIEnv* env, jobject obj, jmethodID methodID, const jvalue* args); 183 | internal readonly delegate* unmanaged[Stdcall] CallFloatMethodA; 184 | 185 | // jdouble (JNICALL *CallDoubleMethod) (JNIEnv* env, jobject obj, jmethodID methodID, ...); 186 | internal readonly IntPtr CallDoubleMethod; 187 | 188 | // jdouble (JNICALL *CallDoubleMethodV) (JNIEnv* env, jobject obj, jmethodID methodID, va_list args); 189 | internal readonly IntPtr CallDoubleMethodV; 190 | 191 | // jdouble (JNICALL *CallDoubleMethodA) (JNIEnv* env, jobject obj, jmethodID methodID, const jvalue* args); 192 | internal readonly delegate* unmanaged[Stdcall] CallDoubleMethodA; 193 | 194 | // void (JNICALL *CallVoidMethod) (JNIEnv* env, jobject obj, jmethodID methodID, ...); 195 | internal readonly IntPtr CallVoidMethod; 196 | 197 | // void (JNICALL *CallVoidMethodV) (JNIEnv* env, jobject obj, jmethodID methodID, va_list args); 198 | internal readonly IntPtr CallVoidMethodV; 199 | 200 | // void (JNICALL *CallVoidMethodA) (JNIEnv* env, jobject obj, jmethodID methodID, const jvalue* args); 201 | internal readonly delegate* unmanaged[Stdcall] CallVoidMethodA; 202 | 203 | // jobject (JNICALL *CallNonvirtualObjectMethod) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, ...); 204 | internal readonly IntPtr CallNonvirtualObjectMethod; 205 | 206 | // jobject (JNICALL *CallNonvirtualObjectMethodV) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, va_list args); 207 | internal readonly IntPtr CallNonvirtualObjectMethodV; 208 | 209 | // jobject (JNICALL *CallNonvirtualObjectMethodA) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, const jvalue* args); 210 | internal readonly delegate* unmanaged[Stdcall] CallNonvirtualObjectMethodA; 211 | 212 | // jboolean (JNICALL *CallNonvirtualBooleanMethod) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, ...); 213 | internal readonly IntPtr CallNonvirtualBooleanMethod; 214 | 215 | // jboolean (JNICALL *CallNonvirtualBooleanMethodV) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, va_list args); 216 | internal readonly IntPtr CallNonvirtualBooleanMethodV; 217 | 218 | // jboolean (JNICALL *CallNonvirtualBooleanMethodA) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, const jvalue* args); 219 | internal readonly delegate* unmanaged[Stdcall] CallNonvirtualBooleanMethodA; 220 | 221 | // jbyte (JNICALL *CallNonvirtualByteMethod) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, ...); 222 | internal readonly IntPtr CallNonvirtualByteMethod; 223 | 224 | // jbyte (JNICALL *CallNonvirtualByteMethodV) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, va_list args); 225 | internal readonly IntPtr CallNonvirtualByteMethodV; 226 | 227 | // jbyte (JNICALL *CallNonvirtualByteMethodA) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, const jvalue* args); 228 | internal readonly delegate* unmanaged[Stdcall] CallNonvirtualByteMethodA; 229 | 230 | // jchar (JNICALL *CallNonvirtualCharMethod) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, ...); 231 | internal readonly IntPtr CallNonvirtualCharMethod; 232 | 233 | // jchar (JNICALL *CallNonvirtualCharMethodV) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, va_list args); 234 | internal readonly IntPtr CallNonvirtualCharMethodV; 235 | 236 | // jchar (JNICALL *CallNonvirtualCharMethodA) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, const jvalue* args); 237 | internal readonly delegate* unmanaged[Stdcall] CallNonvirtualCharMethodA; 238 | 239 | // jshort (JNICALL *CallNonvirtualShortMethod) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, ...); 240 | internal readonly IntPtr CallNonvirtualShortMethod; 241 | 242 | // jshort (JNICALL *CallNonvirtualShortMethodV) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, va_list args); 243 | internal readonly IntPtr CallNonvirtualShortMethodV; 244 | 245 | // jshort (JNICALL *CallNonvirtualShortMethodA) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, const jvalue* args); 246 | internal readonly delegate* unmanaged[Stdcall] CallNonvirtualShortMethodA; 247 | 248 | // jint (JNICALL *CallNonvirtualIntMethod) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, ...); 249 | internal readonly IntPtr CallNonvirtualIntMethod; 250 | 251 | // jint (JNICALL *CallNonvirtualIntMethodV) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, va_list args); 252 | internal readonly IntPtr CallNonvirtualIntMethodV; 253 | 254 | // jint (JNICALL *CallNonvirtualIntMethodA) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, const jvalue* args); 255 | internal readonly delegate* unmanaged[Stdcall] CallNonvirtualIntMethodA; 256 | 257 | // jlong (JNICALL *CallNonvirtualLongMethod) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, ...); 258 | internal readonly IntPtr CallNonvirtualLongMethod; 259 | 260 | // jlong (JNICALL *CallNonvirtualLongMethodV)(JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, va_list args); 261 | internal readonly IntPtr CallNonvirtualLongMethodV; 262 | 263 | // jlong (JNICALL *CallNonvirtualLongMethodA) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, const jvalue* args); 264 | internal readonly delegate* unmanaged[Stdcall] CallNonvirtualLongMethodA; 265 | 266 | // jfloat (JNICALL *CallNonvirtualFloatMethod) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, ...); 267 | internal readonly IntPtr CallNonvirtualFloatMethod; 268 | 269 | // jfloat (JNICALL *CallNonvirtualFloatMethodV) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, va_list args); 270 | internal readonly IntPtr CallNonvirtualFloatMethodV; 271 | 272 | // jfloat (JNICALL *CallNonvirtualFloatMethodA) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, const jvalue* args); 273 | internal readonly delegate* unmanaged[Stdcall] CallNonvirtualFloatMethodA; 274 | 275 | // jdouble (JNICALL *CallNonvirtualDoubleMethod) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, ...); 276 | internal readonly IntPtr CallNonvirtualDoubleMethod; 277 | 278 | // jdouble (JNICALL *CallNonvirtualDoubleMethodV) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, va_list args); 279 | internal readonly IntPtr CallNonvirtualDoubleMethodV; 280 | 281 | // jdouble (JNICALL *CallNonvirtualDoubleMethodA) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, const jvalue* args); 282 | internal readonly delegate* unmanaged[Stdcall] CallNonvirtualDoubleMethodA; 283 | 284 | // void (JNICALL *CallNonvirtualVoidMethod) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, ...); 285 | internal readonly IntPtr CallNonvirtualVoidMethod; 286 | 287 | // void (JNICALL *CallNonvirtualVoidMethodV) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, va_list args); 288 | internal readonly IntPtr CallNonvirtualVoidMethodV; 289 | 290 | // void (JNICALL *CallNonvirtualVoidMethodA) (JNIEnv* env, jobject obj, jclass clazz, jmethodID methodID, const jvalue* args); 291 | internal readonly delegate* unmanaged[Stdcall] CallNonvirtualVoidMethodA; 292 | 293 | // jfieldID (JNICALL *GetFieldID) (JNIEnv* env, jclass clazz, const char* name, const char* sig); 294 | internal readonly delegate* unmanaged[Stdcall] GetFieldID; 295 | 296 | // jobject (JNICALL *GetObjectField) (JNIEnv* env, jobject obj, jfieldID fieldID); 297 | internal readonly delegate* unmanaged[Stdcall] GetObjectField; 298 | 299 | // jboolean (JNICALL *GetBooleanField) (JNIEnv* env, jobject obj, jfieldID fieldID); 300 | internal readonly delegate* unmanaged[Stdcall] GetBooleanField; 301 | 302 | // jbyte (JNICALL *GetByteField) (JNIEnv* env, jobject obj, jfieldID fieldID); 303 | internal readonly delegate* unmanaged[Stdcall] GetByteField; 304 | 305 | // jchar (JNICALL *GetCharField) (JNIEnv* env, jobject obj, jfieldID fieldID); 306 | internal readonly delegate* unmanaged[Stdcall] GetCharField; 307 | 308 | // jshort (JNICALL *GetShortField) (JNIEnv* env, jobject obj, jfieldID fieldID); 309 | internal readonly delegate* unmanaged[Stdcall] GetShortField; 310 | 311 | // jint (JNICALL *GetIntField) (JNIEnv* env, jobject obj, jfieldID fieldID); 312 | internal readonly delegate* unmanaged[Stdcall] GetIntField; 313 | 314 | // jlong (JNICALL *GetLongField) (JNIEnv* env, jobject obj, jfieldID fieldID); 315 | internal readonly delegate* unmanaged[Stdcall] GetLongField; 316 | 317 | // jfloat (JNICALL *GetFloatField) (JNIEnv* env, jobject obj, jfieldID fieldID); 318 | internal readonly delegate* unmanaged[Stdcall] GetFloatField; 319 | 320 | // jdouble (JNICALL *GetDoubleField) (JNIEnv* env, jobject obj, jfieldID fieldID); 321 | internal readonly delegate* unmanaged[Stdcall] GetDoubleField; 322 | 323 | // void (JNICALL *SetObjectField) (JNIEnv* env, jobject obj, jfieldID fieldID, jobject val); 324 | internal readonly delegate* unmanaged[Stdcall] SetObjectField; 325 | 326 | // void (JNICALL *SetBooleanField) (JNIEnv* env, jobject obj, jfieldID fieldID, jboolean val); 327 | internal readonly delegate* unmanaged[Stdcall] SetBooleanField; 328 | 329 | // void (JNICALL *SetByteField) (JNIEnv* env, jobject obj, jfieldID fieldID, jbyte val); 330 | internal readonly delegate* unmanaged[Stdcall] SetByteField; 331 | 332 | // void (JNICALL *SetCharField) (JNIEnv* env, jobject obj, jfieldID fieldID, jchar val); 333 | internal readonly delegate* unmanaged[Stdcall] SetCharField; 334 | 335 | // void (JNICALL *SetShortField) (JNIEnv* env, jobject obj, jfieldID fieldID, jshort val); 336 | internal readonly delegate* unmanaged[Stdcall] SetShortField; 337 | 338 | // void (JNICALL *SetIntField) (JNIEnv* env, jobject obj, jfieldID fieldID, jint val); 339 | internal readonly delegate* unmanaged[Stdcall] SetIntField; 340 | 341 | // void (JNICALL *SetLongField) (JNIEnv* env, jobject obj, jfieldID fieldID, jlong val); 342 | internal readonly delegate* unmanaged[Stdcall] SetLongField; 343 | 344 | // void (JNICALL *SetFloatField) (JNIEnv* env, jobject obj, jfieldID fieldID, jfloat val); 345 | internal readonly delegate* unmanaged[Stdcall] SetFloatField; 346 | 347 | // void (JNICALL *SetDoubleField) (JNIEnv* env, jobject obj, jfieldID fieldID, jdouble val); 348 | internal readonly delegate* unmanaged[Stdcall] SetDoubleField; 349 | 350 | // jmethodID (JNICALL *GetStaticMethodID) (JNIEnv* env, jclass clazz, const char* name, const char* sig); 351 | internal readonly delegate* unmanaged[Stdcall] GetStaticMethodID; 352 | 353 | // jobject (JNICALL *CallStaticObjectMethod) (JNIEnv* env, jclass clazz, jmethodID methodID, ...); 354 | internal readonly IntPtr CallStaticObjectMethod; 355 | 356 | // jobject (JNICALL *CallStaticObjectMethodV) (JNIEnv* env, jclass clazz, jmethodID methodID, va_list args); 357 | internal readonly IntPtr CallStaticObjectMethodV; 358 | 359 | // jobject (JNICALL *CallStaticObjectMethodA) (JNIEnv* env, jclass clazz, jmethodID methodID, const jvalue* args); 360 | internal readonly delegate* unmanaged[Stdcall] CallStaticObjectMethodA; 361 | 362 | // jboolean (JNICALL *CallStaticBooleanMethod) (JNIEnv* env, jclass clazz, jmethodID methodID, ...); 363 | internal readonly IntPtr CallStaticBooleanMethod; 364 | 365 | // jboolean (JNICALL *CallStaticBooleanMethodV) (JNIEnv* env, jclass clazz, jmethodID methodID, va_list args); 366 | internal readonly IntPtr CallStaticBooleanMethodV; 367 | 368 | // jboolean (JNICALL *CallStaticBooleanMethodA) (JNIEnv* env, jclass clazz, jmethodID methodID, const jvalue* args); 369 | internal readonly delegate* unmanaged[Stdcall] CallStaticBooleanMethodA; 370 | 371 | // jbyte (JNICALL *CallStaticByteMethod)(JNIEnv* env, jclass clazz, jmethodID methodID, ...); 372 | internal readonly IntPtr CallStaticByteMethod; 373 | 374 | // jbyte (JNICALL *CallStaticByteMethodV) (JNIEnv* env, jclass clazz, jmethodID methodID, va_list args); 375 | internal readonly IntPtr CallStaticByteMethodV; 376 | 377 | // jbyte (JNICALL *CallStaticByteMethodA) (JNIEnv* env, jclass clazz, jmethodID methodID, const jvalue* args); 378 | internal readonly delegate* unmanaged[Stdcall] CallStaticByteMethodA; 379 | 380 | // jchar (JNICALL *CallStaticCharMethod) (JNIEnv* env, jclass clazz, jmethodID methodID, ...); 381 | internal readonly IntPtr CallStaticCharMethod; 382 | 383 | // jchar (JNICALL *CallStaticCharMethodV) (JNIEnv* env, jclass clazz, jmethodID methodID, va_list args); 384 | internal readonly IntPtr CallStaticCharMethodV; 385 | 386 | // jchar (JNICALL *CallStaticCharMethodA) (JNIEnv* env, jclass clazz, jmethodID methodID, const jvalue* args); 387 | internal readonly delegate* unmanaged[Stdcall] CallStaticCharMethodA; 388 | 389 | // jshort (JNICALL *CallStaticShortMethod) (JNIEnv* env, jclass clazz, jmethodID methodID, ...); 390 | internal readonly IntPtr CallStaticShortMethod; 391 | 392 | // jshort (JNICALL *CallStaticShortMethodV) (JNIEnv* env, jclass clazz, jmethodID methodID, va_list args); 393 | internal readonly IntPtr CallStaticShortMethodV; 394 | 395 | // jshort (JNICALL *CallStaticShortMethodA) (JNIEnv* env, jclass clazz, jmethodID methodID, const jvalue* args); 396 | internal readonly delegate* unmanaged[Stdcall] CallStaticShortMethodA; 397 | 398 | // jint (JNICALL *CallStaticIntMethod) (JNIEnv* env, jclass clazz, jmethodID methodID, ...); 399 | internal readonly IntPtr CallStaticIntMethod; 400 | 401 | // jint (JNICALL *CallStaticIntMethodV) (JNIEnv* env, jclass clazz, jmethodID methodID, va_list args); 402 | internal readonly IntPtr CallStaticIntMethodV; 403 | 404 | // jint (JNICALL *CallStaticIntMethodA) (JNIEnv* env, jclass clazz, jmethodID methodID, const jvalue* args); 405 | internal readonly delegate* unmanaged[Stdcall] CallStaticIntMethodA; 406 | 407 | // jlong (JNICALL *CallStaticLongMethod) (JNIEnv* env, jclass clazz, jmethodID methodID, ...); 408 | internal readonly IntPtr CallStaticLongMethod; 409 | 410 | // jlong (JNICALL *CallStaticLongMethodV) (JNIEnv* env, jclass clazz, jmethodID methodID, va_list args); 411 | internal readonly IntPtr CallStaticLongMethodV; 412 | 413 | // jlong (JNICALL *CallStaticLongMethodA) (JNIEnv* env, jclass clazz, jmethodID methodID, const jvalue* args); 414 | internal readonly delegate* unmanaged[Stdcall] CallStaticLongMethodA; 415 | 416 | // jfloat (JNICALL *CallStaticFloatMethod) (JNIEnv* env, jclass clazz, jmethodID methodID, ...); 417 | internal readonly IntPtr CallStaticFloatMethod; 418 | 419 | // jfloat (JNICALL *CallStaticFloatMethodV) (JNIEnv* env, jclass clazz, jmethodID methodID, va_list args); 420 | internal readonly IntPtr CallStaticFloatMethodV; 421 | 422 | // jfloat (JNICALL *CallStaticFloatMethodA) (JNIEnv* env, jclass clazz, jmethodID methodID, const jvalue* args); 423 | internal readonly delegate* unmanaged[Stdcall] CallStaticFloatMethodA; 424 | 425 | // jdouble (JNICALL *CallStaticDoubleMethod) (JNIEnv* env, jclass clazz, jmethodID methodID, ...); 426 | internal readonly IntPtr CallStaticDoubleMethod; 427 | 428 | // jdouble (JNICALL *CallStaticDoubleMethodV) (JNIEnv* env, jclass clazz, jmethodID methodID, va_list args); 429 | internal readonly IntPtr CallStaticDoubleMethodV; 430 | 431 | // jdouble (JNICALL *CallStaticDoubleMethodA) (JNIEnv* env, jclass clazz, jmethodID methodID, const jvalue* args); 432 | internal readonly delegate* unmanaged[Stdcall] CallStaticDoubleMethodA; 433 | 434 | // void (JNICALL *CallStaticVoidMethod) (JNIEnv* env, jclass cls, jmethodID methodID, ...); 435 | internal readonly IntPtr CallStaticVoidMethod; 436 | 437 | // void (JNICALL *CallStaticVoidMethodV) (JNIEnv* env, jclass cls, jmethodID methodID, va_list args); 438 | internal readonly IntPtr CallStaticVoidMethodV; 439 | 440 | // void (JNICALL *CallStaticVoidMethodA) (JNIEnv* env, jclass cls, jmethodID methodID, const jvalue* args); 441 | internal readonly delegate* unmanaged[Stdcall] CallStaticVoidMethodA; 442 | 443 | // jfieldID (JNICALL *GetStaticFieldID) (JNIEnv* env, jclass clazz, const char* name, const char* sig); 444 | internal readonly delegate* unmanaged[Stdcall] GetStaticFieldID; 445 | 446 | // jobject (JNICALL *GetStaticObjectField) (JNIEnv* env, jclass clazz, jfieldID fieldID); 447 | internal readonly delegate* unmanaged[Stdcall] GetStaticObjectField; 448 | 449 | // jboolean (JNICALL *GetStaticBooleanField) (JNIEnv* env, jclass clazz, jfieldID fieldID); 450 | internal readonly delegate* unmanaged[Stdcall] GetStaticBooleanField; 451 | 452 | // jbyte (JNICALL *GetStaticByteField) (JNIEnv* env, jclass clazz, jfieldID fieldID); 453 | internal readonly delegate* unmanaged[Stdcall] GetStaticByteField; 454 | 455 | // jchar (JNICALL *GetStaticCharField) (JNIEnv* env, jclass clazz, jfieldID fieldID); 456 | internal readonly delegate* unmanaged[Stdcall] GetStaticCharField; 457 | 458 | // jshort (JNICALL *GetStaticShortField) (JNIEnv* env, jclass clazz, jfieldID fieldID); 459 | internal readonly delegate* unmanaged[Stdcall] GetStaticShortField; 460 | 461 | // jint (JNICALL *GetStaticIntField) (JNIEnv* env, jclass clazz, jfieldID fieldID); 462 | internal readonly delegate* unmanaged[Stdcall] GetStaticIntField; 463 | 464 | // jlong (JNICALL *GetStaticLongField) (JNIEnv* env, jclass clazz, jfieldID fieldID); 465 | internal readonly delegate* unmanaged[Stdcall] GetStaticLongField; 466 | 467 | // jfloat (JNICALL *GetStaticFloatField) (JNIEnv* env, jclass clazz, jfieldID fieldID); 468 | internal readonly delegate* unmanaged[Stdcall] GetStaticFloatField; 469 | 470 | // jdouble (JNICALL *GetStaticDoubleField) (JNIEnv* env, jclass clazz, jfieldID fieldID); 471 | internal readonly delegate* unmanaged[Stdcall] GetStaticDoubleField; 472 | 473 | // void (JNICALL *SetStaticObjectField) (JNIEnv* env, jclass clazz, jfieldID fieldID, jobject value); 474 | internal readonly delegate* unmanaged[Stdcall] SetStaticObjectField; 475 | 476 | // void (JNICALL *SetStaticBooleanField) (JNIEnv* env, jclass clazz, jfieldID fieldID, jboolean value); 477 | internal readonly delegate* unmanaged[Stdcall] SetStaticBooleanField; 478 | 479 | // void (JNICALL *SetStaticByteField) (JNIEnv* env, jclass clazz, jfieldID fieldID, jbyte value); 480 | internal readonly delegate* unmanaged[Stdcall] SetStaticByteField; 481 | 482 | // void (JNICALL *SetStaticCharField) (JNIEnv* env, jclass clazz, jfieldID fieldID, jchar value); 483 | internal readonly delegate* unmanaged[Stdcall] SetStaticCharField; 484 | 485 | // void (JNICALL *SetStaticShortField) (JNIEnv* env, jclass clazz, jfieldID fieldID, jshort value); 486 | internal readonly delegate* unmanaged[Stdcall] SetStaticShortField; 487 | 488 | // void (JNICALL *SetStaticIntField) (JNIEnv* env, jclass clazz, jfieldID fieldID, jint value); 489 | internal readonly delegate* unmanaged[Stdcall] SetStaticIntField; 490 | 491 | // void (JNICALL *SetStaticLongField) (JNIEnv* env, jclass clazz, jfieldID fieldID, jlong value); 492 | internal readonly delegate* unmanaged[Stdcall] SetStaticLongField; 493 | 494 | // void (JNICALL *SetStaticFloatField) (JNIEnv* env, jclass clazz, jfieldID fieldID, jfloat value); 495 | internal readonly delegate* unmanaged[Stdcall] SetStaticFloatField; 496 | 497 | // void (JNICALL *SetStaticDoubleField) (JNIEnv* env, jclass clazz, jfieldID fieldID, jdouble value); 498 | internal readonly delegate* unmanaged[Stdcall] SetStaticDoubleField; 499 | 500 | // jstring (JNICALL *NewString) (JNIEnv* env, const jchar* unicode, jsize len); 501 | internal readonly delegate* unmanaged[Stdcall] NewString; 502 | 503 | // jsize (JNICALL *GetStringLength) (JNIEnv* env, jstring str); 504 | internal readonly delegate* unmanaged[Stdcall] GetStringLength; 505 | 506 | // const jchar *(JNICALL *GetStringChars) (JNIEnv* env, jstring str, jboolean* isCopy); 507 | internal readonly delegate* unmanaged[Stdcall] GetStringChars; 508 | 509 | // void (JNICALL *ReleaseStringChars) (JNIEnv* env, jstring str, const jchar* chars); 510 | internal readonly delegate* unmanaged[Stdcall] ReleaseStringChars; 511 | 512 | // jstring (JNICALL *NewStringUTF) (JNIEnv* env, const char* utf); 513 | internal readonly delegate* unmanaged[Stdcall] NewStringUTF; 514 | 515 | // jsize (JNICALL *GetStringUTFLength) (JNIEnv* env, jstring str); 516 | internal readonly delegate* unmanaged[Stdcall] GetStringUTFLength; 517 | 518 | // const char* (JNICALL *GetStringUTFChars) (JNIEnv* env, jstring str, jboolean* isCopy); 519 | internal readonly delegate* unmanaged[Stdcall] GetStringUTFChars; 520 | 521 | // void (JNICALL *ReleaseStringUTFChars) (JNIEnv* env, jstring str, const char* chars); 522 | internal readonly delegate* unmanaged[Stdcall] ReleaseStringUTFChars; 523 | 524 | // jsize (JNICALL *GetArrayLength) (JNIEnv* env, jarray array); 525 | internal readonly delegate* unmanaged[Stdcall] GetArrayLength; 526 | 527 | // jobjectArray (JNICALL *NewObjectArray) (JNIEnv* env, jsize len, jclass clazz, jobject init); 528 | internal readonly delegate* unmanaged[Stdcall] NewObjectArray; 529 | 530 | // jobject (JNICALL *GetObjectArrayElement) (JNIEnv* env, jobjectArray array, jsize index); 531 | internal readonly delegate* unmanaged[Stdcall] GetObjectArrayElement; 532 | 533 | // void (JNICALL *SetObjectArrayElement) (JNIEnv* env, jobjectArray array, jsize index, jobject val); 534 | internal readonly delegate* unmanaged[Stdcall] SetObjectArrayElement; 535 | 536 | // jbooleanArray (JNICALL *NewBooleanArray) (JNIEnv* env, jsize len); 537 | internal readonly delegate* unmanaged[Stdcall] NewBooleanArray; 538 | 539 | // jbyteArray (JNICALL *NewByteArray) (JNIEnv* env, jsize len); 540 | internal readonly delegate* unmanaged[Stdcall] NewByteArray; 541 | 542 | // jcharArray (JNICALL *NewCharArray) (JNIEnv* env, jsize len); 543 | internal readonly delegate* unmanaged[Stdcall] NewCharArray; 544 | 545 | // jshortArray (JNICALL *NewShortArray) (JNIEnv* env, jsize len); 546 | internal readonly delegate* unmanaged[Stdcall] NewShortArray; 547 | 548 | // jintArray (JNICALL *NewIntArray) (JNIEnv* env, jsize len); 549 | internal readonly delegate* unmanaged[Stdcall] NewIntArray; 550 | 551 | // jlongArray (JNICALL *NewLongArray) (JNIEnv* env, jsize len); 552 | internal readonly delegate* unmanaged[Stdcall] NewLongArray; 553 | 554 | // jfloatArray (JNICALL *NewFloatArray) (JNIEnv* env, jsize len); 555 | internal readonly delegate* unmanaged[Stdcall] NewFloatArray; 556 | 557 | // jdoubleArray (JNICALL *NewDoubleArray) (JNIEnv* env, jsize len); 558 | internal readonly delegate* unmanaged[Stdcall] NewDoubleArray; 559 | 560 | // jboolean * (JNICALL *GetBooleanArrayElements) (JNIEnv* env, jbooleanArray array, jboolean* isCopy); 561 | internal readonly delegate* unmanaged[Stdcall] GetBooleanArrayElements; 562 | 563 | // jbyte * (JNICALL *GetByteArrayElements) (JNIEnv* env, jbyteArray array, jboolean* isCopy); 564 | internal readonly delegate* unmanaged[Stdcall] GetByteArrayElements; 565 | 566 | // jchar * (JNICALL *GetCharArrayElements) (JNIEnv* env, jcharArray array, jboolean* isCopy); 567 | internal readonly delegate* unmanaged[Stdcall] GetCharArrayElements; 568 | 569 | // jshort * (JNICALL *GetShortArrayElements) (JNIEnv* env, jshortArray array, jboolean* isCopy); 570 | internal readonly delegate* unmanaged[Stdcall] GetShortArrayElements; 571 | 572 | // jint * (JNICALL *GetIntArrayElements) (JNIEnv* env, jintArray array, jboolean* isCopy); 573 | internal readonly delegate* unmanaged[Stdcall] GetIntArrayElements; 574 | 575 | // jlong * (JNICALL *GetLongArrayElements) (JNIEnv* env, jlongArray array, jboolean* isCopy); 576 | internal readonly delegate* unmanaged[Stdcall] GetLongArrayElements; 577 | 578 | // jfloat * (JNICALL *GetFloatArrayElements) (JNIEnv* env, jfloatArray array, jboolean* isCopy); 579 | internal readonly delegate* unmanaged[Stdcall] GetFloatArrayElements; 580 | 581 | // jdouble * (JNICALL *GetDoubleArrayElements) (JNIEnv* env, jdoubleArray array, jboolean* isCopy); 582 | internal readonly delegate* unmanaged[Stdcall] GetDoubleArrayElements; 583 | 584 | // void (JNICALL *ReleaseBooleanArrayElements) (JNIEnv* env, jbooleanArray array, jboolean* elems, jint mode); 585 | internal readonly delegate* unmanaged[Stdcall] ReleaseBooleanArrayElements; 586 | 587 | // void (JNICALL *ReleaseByteArrayElements) (JNIEnv* env, jbyteArray array, jbyte* elems, jint mode); 588 | internal readonly delegate* unmanaged[Stdcall] ReleaseByteArrayElements; 589 | 590 | // void (JNICALL *ReleaseCharArrayElements) (JNIEnv* env, jcharArray array, jchar* elems, jint mode); 591 | internal readonly delegate* unmanaged[Stdcall] ReleaseCharArrayElements; 592 | 593 | // void (JNICALL *ReleaseShortArrayElements) (JNIEnv* env, jshortArray array, jshort* elems, jint mode); 594 | internal readonly delegate* unmanaged[Stdcall] ReleaseShortArrayElements; 595 | 596 | // void (JNICALL *ReleaseIntArrayElements) (JNIEnv* env, jintArray array, jint* elems, jint mode); 597 | internal readonly delegate* unmanaged[Stdcall] ReleaseIntArrayElements; 598 | 599 | // void (JNICALL *ReleaseLongArrayElements) (JNIEnv* env, jlongArray array, jlong* elems, jint mode); 600 | internal readonly delegate* unmanaged[Stdcall] ReleaseLongArrayElements; 601 | 602 | // void (JNICALL *ReleaseFloatArrayElements) (JNIEnv* env, jfloatArray array, jfloat* elems, jint mode); 603 | internal readonly delegate* unmanaged[Stdcall] ReleaseFloatArrayElements; 604 | 605 | // void (JNICALL *ReleaseDoubleArrayElements) (JNIEnv* env, jdoubleArray array, jdouble* elems, jint mode); 606 | internal readonly delegate* unmanaged[Stdcall] ReleaseDoubleArrayElements; 607 | 608 | // void (JNICALL *GetBooleanArrayRegion) (JNIEnv* env, jbooleanArray array, jsize start, jsize l, jboolean* buf); 609 | internal readonly delegate* unmanaged[Stdcall] GetBooleanArrayRegion; 610 | 611 | // void (JNICALL *GetByteArrayRegion) (JNIEnv* env, jbyteArray array, jsize start, jsize len, jbyte* buf); 612 | internal readonly delegate* unmanaged[Stdcall] GetByteArrayRegion; 613 | 614 | // void (JNICALL *GetCharArrayRegion) (JNIEnv* env, jcharArray array, jsize start, jsize len, jchar* buf); 615 | internal readonly delegate* unmanaged[Stdcall] GetCharArrayRegion; 616 | 617 | // void (JNICALL *GetShortArrayRegion) (JNIEnv* env, jshortArray array, jsize start, jsize len, jshort* buf); 618 | internal readonly delegate* unmanaged[Stdcall] GetShortArrayRegion; 619 | 620 | // void (JNICALL *GetIntArrayRegion) (JNIEnv* env, jintArray array, jsize start, jsize len, jint* buf); 621 | internal readonly delegate* unmanaged[Stdcall] GetIntArrayRegion; 622 | 623 | // void (JNICALL *GetLongArrayRegion) (JNIEnv* env, jlongArray array, jsize start, jsize len, jlong* buf); 624 | internal readonly delegate* unmanaged[Stdcall] GetLongArrayRegion; 625 | 626 | // void (JNICALL *GetFloatArrayRegion) (JNIEnv* env, jfloatArray array, jsize start, jsize len, jfloat* buf); 627 | internal readonly delegate* unmanaged[Stdcall] GetFloatArrayRegion; 628 | 629 | // void (JNICALL *GetDoubleArrayRegion) (JNIEnv* env, jdoubleArray array, jsize start, jsize len, jdouble* buf); 630 | internal readonly delegate* unmanaged[Stdcall] GetDoubleArrayRegion; 631 | 632 | // void (JNICALL *SetBooleanArrayRegion) (JNIEnv* env, jbooleanArray array, jsize start, jsize l, const jboolean* buf); 633 | internal readonly delegate* unmanaged[Stdcall] SetBooleanArrayRegion; 634 | 635 | // void (JNICALL *SetByteArrayRegion) (JNIEnv* env, jbyteArray array, jsize start, jsize len, const jbyte* buf); 636 | internal readonly delegate* unmanaged[Stdcall] SetByteArrayRegion; 637 | 638 | // void (JNICALL *SetCharArrayRegion) (JNIEnv* env, jcharArray array, jsize start, jsize len, const jchar* buf); 639 | internal readonly delegate* unmanaged[Stdcall] SetCharArrayRegion; 640 | 641 | // void (JNICALL *SetShortArrayRegion) (JNIEnv* env, jshortArray array, jsize start, jsize len, const jshort* buf); 642 | internal readonly delegate* unmanaged[Stdcall] SetShortArrayRegion; 643 | 644 | // void (JNICALL *SetIntArrayRegion) (JNIEnv* env, jintArray array, jsize start, jsize len, const jint* buf); 645 | internal readonly delegate* unmanaged[Stdcall] SetIntArrayRegion; 646 | 647 | // void (JNICALL *SetLongArrayRegion) (JNIEnv* env, jlongArray array, jsize start, jsize len, const jlong* buf); 648 | internal readonly delegate* unmanaged[Stdcall] SetLongArrayRegion; 649 | 650 | // void (JNICALL *SetFloatArrayRegion) (JNIEnv* env, jfloatArray array, jsize start, jsize len, const jfloat* buf); 651 | internal readonly delegate* unmanaged[Stdcall] SetFloatArrayRegion; 652 | 653 | // void (JNICALL *SetDoubleArrayRegion) (JNIEnv* env, jdoubleArray array, jsize start, jsize len, const jdouble* buf); 654 | internal readonly delegate* unmanaged[Stdcall] SetDoubleArrayRegion; 655 | 656 | // jint (JNICALL *RegisterNatives) (JNIEnv* env, jclass clazz, const JNINativeMethod* methods, jint nMethods); 657 | internal readonly delegate* unmanaged[Stdcall] RegisterNatives; 658 | 659 | // jint (JNICALL *UnregisterNatives) (JNIEnv* env, jclass clazz); 660 | internal readonly delegate* unmanaged[Stdcall] UnregisterNatives; 661 | 662 | // jint (JNICALL *MonitorEnter) (JNIEnv* env, jobject obj); 663 | internal readonly delegate* unmanaged[Stdcall] MonitorEnter; 664 | 665 | // jint (JNICALL *MonitorExit) (JNIEnv* env, jobject obj); 666 | internal readonly delegate* unmanaged[Stdcall] MonitorExit; 667 | 668 | // jint (JNICALL *GetJavaVM) (JNIEnv* env, JavaVM** vm); 669 | internal readonly delegate* unmanaged[Stdcall] GetJavaVM; 670 | 671 | // void (JNICALL *GetStringRegion) (JNIEnv* env, jstring str, jsize start, jsize len, jchar* buf); 672 | internal readonly delegate* unmanaged[Stdcall] GetStringRegion; 673 | 674 | // void (JNICALL *GetStringUTFRegion) (JNIEnv* env, jstring str, jsize start, jsize len, char* buf); 675 | internal readonly delegate* unmanaged[Stdcall] GetStringUTFRegion; 676 | 677 | // void * (JNICALL *GetPrimitiveArrayCritical) (JNIEnv* env, jarray array, jboolean* isCopy); 678 | internal readonly delegate* unmanaged[Stdcall] GetPrimitiveArrayCritical; 679 | 680 | // void (JNICALL *ReleasePrimitiveArrayCritical) (JNIEnv* env, jarray array, void* carray, jint mode); 681 | internal readonly delegate* unmanaged[Stdcall] ReleasePrimitiveArrayCritical; 682 | 683 | // const jchar * (JNICALL *GetStringCritical) (JNIEnv* env, jstring string, jboolean* isCopy); 684 | internal readonly delegate* unmanaged[Stdcall] GetStringCritical; 685 | 686 | // void (JNICALL *ReleaseStringCritical) (JNIEnv* env, jstring string, const jchar* cstring); 687 | internal readonly delegate* unmanaged[Stdcall] ReleaseStringCritical; 688 | 689 | // jweak (JNICALL *NewWeakGlobalRef) (JNIEnv* env, jobject obj); 690 | internal readonly delegate* unmanaged[Stdcall] NewWeakGlobalRef; 691 | 692 | // void (JNICALL *DeleteWeakGlobalRef) (JNIEnv* env, jweak ref); 693 | internal readonly delegate* unmanaged[Stdcall] DeleteWeakGlobalRef; 694 | 695 | // jboolean (JNICALL *ExceptionCheck) (JNIEnv* env); 696 | internal readonly delegate* unmanaged[Stdcall] ExceptionCheck; 697 | 698 | // jobject (JNICALL *NewDirectByteBuffer) (JNIEnv* env, void* address, jlong capacity); 699 | internal readonly delegate* unmanaged[Stdcall] NewDirectByteBuffer; 700 | 701 | // void* (JNICALL *GetDirectBufferAddress) (JNIEnv* env, jobject buf); 702 | internal readonly delegate* unmanaged[Stdcall] GetDirectBufferAddress; 703 | 704 | // jlong (JNICALL *GetDirectBufferCapacity) (JNIEnv* env, jobject buf); 705 | internal readonly delegate* unmanaged[Stdcall] GetDirectBufferCapacity; 706 | } 707 | 708 | internal readonly FunctionTable* Functions; 709 | } 710 | -------------------------------------------------------------------------------- /JNISharp/NativeInterface/JNI.cs: -------------------------------------------------------------------------------- 1 | namespace JNISharp.NativeInterface; 2 | 3 | using System.Runtime.InteropServices; 4 | 5 | /// 6 | /// Represents the Java Native Interface 7 | /// 8 | public unsafe static partial class JNI 9 | { 10 | internal static JavaVM* VM; 11 | 12 | [ThreadStatic] 13 | internal static JNIEnv* env; 14 | 15 | internal static JNIEnv* Env 16 | { 17 | get 18 | { 19 | if (env == null) 20 | { 21 | IntPtr temp = IntPtr.Zero; 22 | var res = VM->Functions->AttachCurrentThread(VM, out env, ref temp); 23 | 24 | if (res != JNI.Result.Ok) 25 | throw new JNIResultException(res); 26 | } 27 | 28 | return env; 29 | } 30 | } 31 | 32 | internal static Dictionary ClassCache { get; set; } = new(); 33 | 34 | public static void Initialize(JavaVMInitArgs args) 35 | { 36 | unsafe 37 | { 38 | JNI.Result res = JVMImports.JNI_CreateJavaVM(out VM, out env, &args); 39 | if (res != JNI.Result.Ok) 40 | throw new JNIResultException(res); 41 | } 42 | } 43 | 44 | public static void InitializeFromCreatedVM() 45 | { 46 | unsafe 47 | { 48 | var res = JVMImports.JNI_GetCreatedJavaVMs(out VM, 1, out int _); 49 | 50 | if (res != JNI.Result.Ok) 51 | throw new JNIResultException(res); 52 | 53 | IntPtr temp = IntPtr.Zero; 54 | res = VM->Functions->AttachCurrentThread(VM, out env, ref temp); 55 | 56 | if (res != JNI.Result.Ok) 57 | throw new JNIResultException(res); 58 | } 59 | } 60 | 61 | public static int GetVersion() 62 | { 63 | unsafe 64 | { 65 | return Env->Functions->GetVersion(Env); 66 | } 67 | } 68 | 69 | public static JClass DefineClass(string name, JObject loader, sbyte[] bytes) 70 | { 71 | unsafe 72 | { 73 | IntPtr bytesPtr = Marshal.UnsafeAddrOfPinnedArrayElement(bytes, 0); 74 | IntPtr nameAnsi = Marshal.StringToHGlobalAnsi(name); 75 | 76 | IntPtr res = Env->Functions->DefineClass(Env, nameAnsi, loader.Handle, bytesPtr, bytes.Length); 77 | 78 | Marshal.FreeHGlobal(nameAnsi); 79 | 80 | using JClass local = new() { Handle = res, ReferenceType = JNI.ReferenceType.Local }; 81 | return NewGlobalRef(local); 82 | } 83 | } 84 | 85 | public static JClass FindClass(string name) 86 | { 87 | unsafe 88 | { 89 | if (ClassCache.TryGetValue(name, out JClass? found)) 90 | { 91 | return found; 92 | } 93 | else 94 | { 95 | IntPtr nameAnsi = Marshal.StringToHGlobalAnsi(name); 96 | IntPtr res = Env->Functions->FindClass(Env, nameAnsi); 97 | 98 | Marshal.FreeHGlobal(nameAnsi); 99 | 100 | using JClass local = new() { Handle = res, ReferenceType = JNI.ReferenceType.Local }; 101 | JClass global = NewGlobalRef(local); 102 | ClassCache.Add(name, global); 103 | return global; 104 | } 105 | } 106 | } 107 | 108 | public static JMethodID FromReflectedMethod(JObject method) 109 | { 110 | unsafe 111 | { 112 | return Env->Functions->FromReflectedMethod(Env, method.Handle); 113 | } 114 | } 115 | 116 | public static JFieldID FromReflectedField(JObject field) 117 | { 118 | unsafe 119 | { 120 | return Env->Functions->FromReflectedField(Env, field.Handle); 121 | } 122 | } 123 | 124 | public static JObject ToReflectedMethod(JClass cls, JMethodID methodID, bool isStatic) 125 | { 126 | unsafe 127 | { 128 | IntPtr res = Env->Functions->ToReflectedMethod(Env, cls.Handle, methodID, Convert.ToByte(isStatic)); 129 | 130 | using JObject local = new() { Handle = res, ReferenceType = ReferenceType.Local }; 131 | return NewGlobalRef(local); 132 | } 133 | } 134 | 135 | public static JClass GetSuperClass(JClass sub) 136 | { 137 | unsafe 138 | { 139 | IntPtr res = Env->Functions->GetSuperClass(Env, sub.Handle); 140 | 141 | using JClass local = new() { Handle = res, ReferenceType = JNI.ReferenceType.Local }; 142 | return NewGlobalRef(local); 143 | } 144 | } 145 | 146 | public static bool IsAssignableFrom(JClass sub, JClass sup) 147 | { 148 | unsafe 149 | { 150 | return Convert.ToBoolean(Env->Functions->IsAssignableFrom(Env, sub.Handle, sup.Handle)); 151 | } 152 | } 153 | 154 | public static JObject ToReflectedField(JClass cls, JFieldID fieldID, bool isStatic) 155 | { 156 | throw new NotImplementedException(); 157 | } 158 | 159 | public static void Throw(JThrowable throwable) 160 | { 161 | unsafe 162 | { 163 | int res = Env->Functions->Throw(Env, throwable.Handle); 164 | } 165 | } 166 | 167 | public static void ThrowNew(JClass cls, string message) 168 | { 169 | unsafe 170 | { 171 | IntPtr messageAnsi = Marshal.StringToHGlobalAnsi(message); 172 | int res = Env->Functions->Throw(Env, messageAnsi); 173 | Marshal.FreeHGlobal(messageAnsi); 174 | } 175 | } 176 | 177 | public static JThrowable ExceptionOccurred() 178 | { 179 | unsafe 180 | { 181 | IntPtr res = Env->Functions->ExceptionOccurred(Env); 182 | 183 | using JThrowable local = new() { Handle = res, ReferenceType = JNI.ReferenceType.Local }; 184 | return NewGlobalRef(local); 185 | } 186 | } 187 | 188 | public static void ExceptionDescribe() 189 | { 190 | unsafe 191 | { 192 | Env->Functions->ExceptionDescribe(Env); 193 | } 194 | } 195 | 196 | public static void ExceptionClear() 197 | { 198 | unsafe 199 | { 200 | Env->Functions->ExceptionClear(Env); 201 | } 202 | } 203 | 204 | public static void FatalError(string message) 205 | { 206 | throw new NotImplementedException(); 207 | } 208 | 209 | public static int PushLocalFrame(int capacity) 210 | { 211 | throw new NotImplementedException(); 212 | } 213 | 214 | public static JObject PopLocalFrame(JObject result) 215 | { 216 | throw new NotImplementedException(); 217 | } 218 | 219 | public static T NewGlobalRef(JObject lobj) where T : JObject, new() 220 | { 221 | unsafe 222 | { 223 | IntPtr res = Env->Functions->NewGlobalRef(Env, lobj.Handle); 224 | return new T() { Handle = res, ReferenceType = JNI.ReferenceType.Global }; 225 | } 226 | } 227 | 228 | public static void DeleteGlobalRef(JObject gref) 229 | { 230 | unsafe 231 | { 232 | if (gref == null) 233 | return; 234 | 235 | if (!gref.Valid()) 236 | return; 237 | 238 | Env->Functions->DeleteGlobalRef(Env, gref.Handle); 239 | } 240 | } 241 | 242 | public static void CheckExceptionAndThrow() 243 | { 244 | if (ExceptionCheck()) 245 | { 246 | JThrowable throwable = ExceptionOccurred(); 247 | ExceptionClear(); 248 | throw new JThrowableException(throwable); 249 | } 250 | } 251 | 252 | public static void DeleteLocalRef(JObject lref) 253 | { 254 | unsafe 255 | { 256 | if (lref == null) 257 | return; 258 | 259 | if (!lref.Valid()) 260 | return; 261 | 262 | Env->Functions->DeleteLocalRef(Env, lref.Handle); 263 | } 264 | } 265 | 266 | public static bool IsSameObject(JObject obj1, JObject obj2) 267 | { 268 | unsafe 269 | { 270 | return Convert.ToBoolean(Env->Functions->IsSameObject(Env, obj1.Handle, obj2.Handle)); 271 | } 272 | } 273 | 274 | public static T NewLocalRef(JObject obj) where T : JObject, new() 275 | { 276 | unsafe 277 | { 278 | IntPtr res = Env->Functions->NewLocalRef(Env, obj.Handle); 279 | return new T() { Handle = res, ReferenceType = JNI.ReferenceType.Local }; 280 | } 281 | } 282 | 283 | public static int EnsureLocalCapacity(int capacity) 284 | { 285 | unsafe 286 | { 287 | return Env->Functions->EnsureLocalCapacity(Env, capacity); 288 | } 289 | } 290 | 291 | public static T AllocObject(JClass cls) where T : JObject, new() 292 | { 293 | unsafe 294 | { 295 | IntPtr res = Env->Functions->AllocObject(Env, cls.Handle); 296 | 297 | using JObject local = new() { Handle = res, ReferenceType = JNI.ReferenceType.Local }; 298 | return NewGlobalRef(local); 299 | } 300 | } 301 | 302 | public static T NewObject(JClass cls, JMethodID methodID, params JValue[] args) where T : JObject, new() 303 | { 304 | unsafe 305 | { 306 | IntPtr argsPtr = Marshal.UnsafeAddrOfPinnedArrayElement(args, 0); 307 | IntPtr res = Env->Functions->NewObjectA(Env, cls.Handle, methodID, argsPtr); 308 | Console.WriteLine($"Res: {res}"); 309 | JObject local = new() { Handle = res, ReferenceType = JNI.ReferenceType.Local }; 310 | return NewGlobalRef(local); 311 | } 312 | } 313 | 314 | public static JClass GetObjectClass(JObject obj) 315 | { 316 | unsafe 317 | { 318 | IntPtr res = Env->Functions->GetObjectClass(Env, obj.Handle); 319 | 320 | using JClass local = new() { Handle = res, ReferenceType = JNI.ReferenceType.Local }; 321 | return NewGlobalRef(local); 322 | } 323 | } 324 | 325 | public static bool IsInstanceOf(JObject obj, JClass cls) 326 | { 327 | unsafe 328 | { 329 | return Convert.ToBoolean(Env->Functions->IsInstanceOf(Env, obj.Handle, cls.Handle)); 330 | } 331 | } 332 | 333 | public static JMethodID GetMethodID(JClass cls, string name, string sig) 334 | { 335 | unsafe 336 | { 337 | IntPtr nameAnsi = Marshal.StringToHGlobalAnsi(name); 338 | IntPtr sigAnsi = Marshal.StringToHGlobalAnsi(sig); 339 | 340 | JMethodID id = Env->Functions->GetMethodID(Env, cls.Handle, nameAnsi, sigAnsi); 341 | 342 | Marshal.FreeHGlobal(nameAnsi); 343 | Marshal.FreeHGlobal(sigAnsi); 344 | return id; 345 | } 346 | } 347 | 348 | public static T CallObjectMethod(JObject obj, JMethodID methodID, params JValue[] args) where T : JObject, new() 349 | { 350 | unsafe 351 | { 352 | IntPtr argsPtr = Marshal.UnsafeAddrOfPinnedArrayElement(args, 0); 353 | 354 | fixed (JValue* v = args) 355 | { 356 | IntPtr res = Env->Functions->CallObjectMethodA(Env, obj.Handle, methodID, argsPtr); 357 | using JObject local = new() { Handle = res, ReferenceType = JNI.ReferenceType.Local }; 358 | return NewGlobalRef(local); 359 | } 360 | } 361 | } 362 | 363 | public static T CallMethod(JObject obj, JMethodID methodID, params JValue[] args) 364 | { 365 | unsafe 366 | { 367 | Type t = typeof(T); 368 | IntPtr argsPtr = Marshal.UnsafeAddrOfPinnedArrayElement(args, 0); 369 | 370 | if (t == typeof(bool)) 371 | { 372 | return (T)(object)Convert.ToBoolean(Env->Functions->CallBooleanMethodA(Env, obj.Handle, methodID, argsPtr)); 373 | } 374 | else if (t == typeof(sbyte)) 375 | { 376 | return (T)(object)Env->Functions->CallByteMethodA(Env, obj.Handle, methodID, argsPtr); 377 | } 378 | else if (t == typeof(char)) 379 | { 380 | return (T)(object)Env->Functions->CallCharMethodA(Env, obj.Handle, methodID, argsPtr); 381 | } 382 | else if (t == typeof(short)) 383 | { 384 | return (T)(object)Env->Functions->CallShortMethodA(Env, obj.Handle, methodID, argsPtr); 385 | } 386 | else if (t == typeof(int)) 387 | { 388 | return (T)(object)Env->Functions->CallIntMethodA(Env, obj.Handle, methodID, argsPtr); 389 | } 390 | else if (t == typeof(long)) 391 | { 392 | return (T)(object)Env->Functions->CallLongMethodA(Env, obj.Handle, methodID, argsPtr); 393 | } 394 | else if (t == typeof(float)) 395 | { 396 | return (T)(object)Env->Functions->CallFloatMethodA(Env, obj.Handle, methodID, argsPtr); 397 | } 398 | else if (t == typeof(double)) 399 | { 400 | return (T)(object)Env->Functions->CallDoubleMethodA(Env, obj.Handle, methodID, argsPtr); 401 | } 402 | else 403 | { 404 | throw new ArgumentException($"CallMethod Type {t} not supported."); 405 | } 406 | } 407 | } 408 | 409 | public static void CallVoidMethod(JObject obj, JMethodID methodID, params JValue[] args) 410 | { 411 | unsafe 412 | { 413 | IntPtr argsPtr = Marshal.UnsafeAddrOfPinnedArrayElement(args, 0); 414 | Env->Functions->CallVoidMethodA(Env, obj.Handle, methodID, argsPtr); 415 | } 416 | } 417 | 418 | public static T CallNonvirtualObjectMethod(JObject obj, JClass cls, JMethodID methodID, params JValue[] args) where T : JObject, new() 419 | { 420 | unsafe 421 | { 422 | IntPtr argsPtr = Marshal.UnsafeAddrOfPinnedArrayElement(args, 0); 423 | IntPtr res = Env->Functions->CallNonvirtualObjectMethodA(Env, obj.Handle, cls.Handle, methodID, argsPtr); 424 | 425 | using JObject local = new() { Handle = res, ReferenceType = JNI.ReferenceType.Local }; 426 | return NewGlobalRef(local); 427 | } 428 | } 429 | 430 | public static T CallNonvirtualMethod(JObject obj, JClass cls, JMethodID methodID, params JValue[] args) 431 | { 432 | unsafe 433 | { 434 | Type t = typeof(T); 435 | IntPtr argsPtr = Marshal.UnsafeAddrOfPinnedArrayElement(args, 0); 436 | 437 | if (t == typeof(bool)) 438 | { 439 | return (T)(object)Convert.ToBoolean(Env->Functions->CallNonvirtualBooleanMethodA(Env, obj.Handle, cls.Handle, methodID, argsPtr)); 440 | } 441 | else if (t == typeof(sbyte)) 442 | { 443 | return (T)(object)Env->Functions->CallNonvirtualByteMethodA(Env, obj.Handle, cls.Handle, methodID, argsPtr); 444 | } 445 | else if (t == typeof(char)) 446 | { 447 | return (T)(object)Env->Functions->CallNonvirtualCharMethodA(Env, obj.Handle, cls.Handle, methodID, argsPtr); 448 | } 449 | else if (t == typeof(short)) 450 | { 451 | return (T)(object)Env->Functions->CallNonvirtualShortMethodA(Env, obj.Handle, cls.Handle, methodID, argsPtr); 452 | } 453 | else if (t == typeof(int)) 454 | { 455 | return (T)(object)Env->Functions->CallNonvirtualIntMethodA(Env, obj.Handle, cls.Handle, methodID, argsPtr); 456 | } 457 | else if (t == typeof(long)) 458 | { 459 | return (T)(object)Env->Functions->CallNonvirtualLongMethodA(Env, obj.Handle, cls.Handle, methodID, argsPtr); 460 | } 461 | else if (t == typeof(float)) 462 | { 463 | return (T)(object)Env->Functions->CallNonvirtualFloatMethodA(Env, obj.Handle, cls.Handle, methodID, argsPtr); 464 | } 465 | else if (t == typeof(double)) 466 | { 467 | return (T)(object)Env->Functions->CallNonvirtualDoubleMethodA(Env, obj.Handle, cls.Handle, methodID, argsPtr); 468 | } 469 | else 470 | { 471 | throw new ArgumentException($"CallNonvirtualMethod Type {t} not supported."); 472 | } 473 | } 474 | } 475 | 476 | public static void CallNonvirtualVoidMethod(JObject obj, JClass cls, JMethodID methodID, params JValue[] args) 477 | { 478 | unsafe 479 | { 480 | IntPtr argsPtr = Marshal.UnsafeAddrOfPinnedArrayElement(args, 0); 481 | Env->Functions->CallNonvirtualVoidMethodA(Env, obj.Handle, cls.Handle, methodID, argsPtr); 482 | } 483 | } 484 | 485 | public static JFieldID GetFieldID(JClass cls, string name, string sig) 486 | { 487 | unsafe 488 | { 489 | IntPtr nameAnsi = Marshal.StringToHGlobalAnsi(name); 490 | IntPtr sigAnsi = Marshal.StringToHGlobalAnsi(sig); 491 | 492 | JFieldID id = Env->Functions->GetFieldID(Env, cls.Handle, nameAnsi, sigAnsi); 493 | 494 | Marshal.FreeHGlobal(nameAnsi); 495 | Marshal.FreeHGlobal(sigAnsi); 496 | return id; 497 | } 498 | } 499 | 500 | public static T GetObjectField(JObject obj, JFieldID fieldID) where T : JObject, new() 501 | { 502 | unsafe 503 | { 504 | IntPtr res = Env->Functions->GetObjectField(Env, obj.Handle, fieldID); 505 | using JObject local = new() { Handle = res, ReferenceType = JNI.ReferenceType.Local }; 506 | return NewGlobalRef(local); 507 | } 508 | } 509 | 510 | public static T GetField(JObject obj, JFieldID fieldID) 511 | { 512 | unsafe 513 | { 514 | Type t = typeof(T); 515 | 516 | if (t == typeof(bool)) 517 | { 518 | return (T)(object)Convert.ToBoolean(Env->Functions->GetBooleanField(Env, obj.Handle, fieldID)); 519 | } 520 | else if (t == typeof(sbyte)) 521 | { 522 | return (T)(object)Env->Functions->GetByteField(Env, obj.Handle, fieldID); 523 | } 524 | else if (t == typeof(char)) 525 | { 526 | return (T)(object)Env->Functions->GetCharField(Env, obj.Handle, fieldID); 527 | } 528 | else if (t == typeof(short)) 529 | { 530 | return (T)(object)Env->Functions->GetShortField(Env, obj.Handle, fieldID); 531 | } 532 | else if (t == typeof(int)) 533 | { 534 | return (T)(object)Env->Functions->GetIntField(Env, obj.Handle, fieldID); 535 | } 536 | else if (t == typeof(long)) 537 | { 538 | return (T)(object)Env->Functions->GetLongField(Env, obj.Handle, fieldID); 539 | } 540 | else if (t == typeof(float)) 541 | { 542 | return (T)(object)Env->Functions->GetFloatField(Env, obj.Handle, fieldID); 543 | } 544 | else if (t == typeof(double)) 545 | { 546 | return (T)(object)Env->Functions->GetDoubleField(Env, obj.Handle, fieldID); 547 | } 548 | else 549 | { 550 | throw new ArgumentException($"GetField Type {t} not supported."); 551 | } 552 | } 553 | } 554 | 555 | public static void SetObjectField(JObject obj, JFieldID fieldID, JObject val) 556 | { 557 | unsafe 558 | { 559 | Env->Functions->SetObjectField(Env, obj.Handle, fieldID, val.Handle); 560 | } 561 | } 562 | 563 | public static void SetField(JObject obj, JFieldID fieldID, T value) 564 | { 565 | unsafe 566 | { 567 | switch (value) 568 | { 569 | case bool b: 570 | Env->Functions->SetBooleanField(Env, obj.Handle, fieldID, Convert.ToByte(b)); 571 | break; 572 | 573 | case sbyte b: 574 | Env->Functions->SetByteField(Env, obj.Handle, fieldID, b); 575 | break; 576 | 577 | case char c: 578 | Env->Functions->SetCharField(Env, obj.Handle, fieldID, c); 579 | break; 580 | 581 | case short s: 582 | Env->Functions->SetShortField(Env, obj.Handle, fieldID, s); 583 | break; 584 | 585 | case int i: 586 | Env->Functions->SetIntField(Env, obj.Handle, fieldID, i); 587 | break; 588 | 589 | case float f: 590 | Env->Functions->SetFloatField(Env, obj.Handle, fieldID, f); 591 | break; 592 | 593 | case double d: 594 | Env->Functions->SetDoubleField(Env, obj.Handle, fieldID, d); 595 | break; 596 | 597 | default: 598 | throw new ArgumentException($"SetField Type {value?.GetType()} not supported."); 599 | } 600 | } 601 | } 602 | 603 | public static JMethodID GetStaticMethodID(JClass cls, string name, string sig) 604 | { 605 | unsafe 606 | { 607 | IntPtr nameAnsi = Marshal.StringToHGlobalAnsi(name); 608 | IntPtr sigAnsi = Marshal.StringToHGlobalAnsi(sig); 609 | JMethodID id = Env->Functions->GetStaticMethodID(Env, cls.Handle, nameAnsi, sigAnsi); 610 | return id; 611 | } 612 | } 613 | 614 | public static T CallStaticObjectMethod(JClass cls, JMethodID methodID, params JValue[] args) where T : JObject, new() 615 | { 616 | unsafe 617 | { 618 | IntPtr argsPtr = Marshal.UnsafeAddrOfPinnedArrayElement(args, 0); 619 | IntPtr res = Env->Functions->CallStaticObjectMethodA(Env, cls.Handle, methodID.Handle, argsPtr); 620 | using JObject local = new() { Handle = res, ReferenceType = JNI.ReferenceType.Local }; 621 | return NewGlobalRef(local); 622 | } 623 | } 624 | 625 | public static T CallStaticMethod(JClass cls, JMethodID methodID, params JValue[] args) 626 | { 627 | Type t = typeof(T); 628 | IntPtr argsPtr = Marshal.UnsafeAddrOfPinnedArrayElement(args, 0); 629 | 630 | if (t == typeof(bool)) 631 | { 632 | return (T)(object)Convert.ToBoolean(Env->Functions->CallStaticBooleanMethodA(Env, cls.Handle, methodID, argsPtr)); 633 | } 634 | else if (t == typeof(sbyte)) 635 | { 636 | return (T)(object)Env->Functions->CallStaticByteMethodA(Env, cls.Handle, methodID, argsPtr); 637 | } 638 | else if (t == typeof(char)) 639 | { 640 | return (T)(object)Env->Functions->CallStaticCharMethodA(Env, cls.Handle, methodID, argsPtr); 641 | } 642 | else if (t == typeof(short)) 643 | { 644 | return (T)(object)Env->Functions->CallStaticShortMethodA(Env, cls.Handle, methodID, argsPtr); 645 | } 646 | else if (t == typeof(int)) 647 | { 648 | return (T)(object)Env->Functions->CallStaticIntMethodA(Env, cls.Handle, methodID, argsPtr); 649 | } 650 | else if (t == typeof(long)) 651 | { 652 | return (T)(object)Env->Functions->CallStaticLongMethodA(Env, cls.Handle, methodID, argsPtr); 653 | } 654 | else if (t == typeof(float)) 655 | { 656 | return (T)(object)Env->Functions->CallStaticFloatMethodA(Env, cls.Handle, methodID, argsPtr); 657 | } 658 | else if (t == typeof(double)) 659 | { 660 | return (T)(object)Env->Functions->CallStaticDoubleMethodA(Env, cls.Handle, methodID, argsPtr); 661 | } 662 | else 663 | { 664 | throw new ArgumentException($"CallStaticMethod Type {t} not supported."); 665 | } 666 | } 667 | 668 | public static void CallStaticVoidMethod(JClass cls, JMethodID methodID, params JValue[] args) 669 | { 670 | unsafe 671 | { 672 | IntPtr argsPtr = Marshal.UnsafeAddrOfPinnedArrayElement(args, 0); 673 | Env->Functions->CallStaticVoidMethodA(Env, cls.Handle, methodID, argsPtr); 674 | } 675 | } 676 | 677 | public static JFieldID GetStaticFieldID(JClass cls, string name, string sig) 678 | { 679 | unsafe 680 | { 681 | IntPtr nameAnsi = Marshal.StringToHGlobalAnsi(name); 682 | IntPtr sigAnsi = Marshal.StringToHGlobalAnsi(sig); 683 | 684 | JFieldID id = Env->Functions->GetStaticFieldID(Env, cls.Handle, nameAnsi, sigAnsi); 685 | 686 | Marshal.FreeHGlobal(nameAnsi); 687 | Marshal.FreeHGlobal(sigAnsi); 688 | return id; 689 | } 690 | } 691 | 692 | public static T GetStaticObjectField(JClass cls, JFieldID fieldID) where T : JObject, new() 693 | { 694 | unsafe 695 | { 696 | IntPtr res = Env->Functions->GetStaticObjectField(Env, cls.Handle, fieldID); 697 | 698 | using JObject local = new() { Handle = res, ReferenceType = JNI.ReferenceType.Local }; 699 | return NewGlobalRef(local); 700 | } 701 | } 702 | 703 | public static T GetStaticField(JClass cls, JFieldID fieldID) 704 | { 705 | unsafe 706 | { 707 | Type t = typeof(T); 708 | 709 | if (t == typeof(bool)) 710 | { 711 | return (T)(object)Convert.ToBoolean(Env->Functions->GetStaticBooleanField(Env, cls.Handle, fieldID)); 712 | } 713 | else if (t == typeof(sbyte)) 714 | { 715 | return (T)(object)Env->Functions->GetStaticByteField(Env, cls.Handle, fieldID); 716 | } 717 | else if (t == typeof(char)) 718 | { 719 | return (T)(object)Env->Functions->GetStaticCharField(Env, cls.Handle, fieldID); 720 | } 721 | else if (t == typeof(short)) 722 | { 723 | return (T)(object)Env->Functions->GetStaticShortField(Env, cls.Handle, fieldID); 724 | } 725 | else if (t == typeof(int)) 726 | { 727 | return (T)(object)Env->Functions->GetStaticIntField(Env, cls.Handle, fieldID); 728 | } 729 | else if (t == typeof(long)) 730 | { 731 | return (T)(object)Env->Functions->GetStaticLongField(Env, cls.Handle, fieldID); 732 | } 733 | else if (t == typeof(float)) 734 | { 735 | return (T)(object)Env->Functions->GetStaticFloatField(Env, cls.Handle, fieldID); 736 | } 737 | else if (t == typeof(double)) 738 | { 739 | return (T)(object)Env->Functions->GetStaticDoubleField(Env, cls.Handle, fieldID); 740 | } 741 | else 742 | { 743 | throw new ArgumentException($"GetStaticField Type {t} not supported."); 744 | } 745 | } 746 | } 747 | 748 | public static void SetStaticObjectField(JClass cls, JFieldID fieldID, T value) where T : JObject, new() 749 | { 750 | unsafe 751 | { 752 | Env->Functions->SetStaticObjectField(Env, cls.Handle, fieldID, value.Handle); 753 | } 754 | } 755 | 756 | public static void SetStaticField(JClass cls, JFieldID fieldID, T value) 757 | { 758 | unsafe 759 | { 760 | switch (value) 761 | { 762 | case bool b: 763 | Env->Functions->SetStaticBooleanField(Env, cls.Handle, fieldID, Convert.ToByte(b)); 764 | break; 765 | 766 | case sbyte b: 767 | Env->Functions->SetStaticByteField(Env, cls.Handle, fieldID, b); 768 | break; 769 | 770 | case char c: 771 | Env->Functions->SetStaticCharField(Env, cls.Handle, fieldID, c); 772 | break; 773 | 774 | case short s: 775 | Env->Functions->SetStaticShortField(Env, cls.Handle, fieldID, s); 776 | break; 777 | 778 | case int i: 779 | Env->Functions->SetStaticIntField(Env, cls.Handle, fieldID, i); 780 | break; 781 | 782 | case float f: 783 | Env->Functions->SetStaticFloatField(Env, cls.Handle, fieldID, f); 784 | break; 785 | 786 | case double d: 787 | Env->Functions->SetStaticDoubleField(Env, cls.Handle, fieldID, d); 788 | break; 789 | 790 | default: 791 | throw new ArgumentException($"SetField Type {value.GetType()} not supported."); 792 | } 793 | } 794 | } 795 | 796 | public static JString NewString(string str) 797 | { 798 | unsafe 799 | { 800 | IntPtr strUni = Marshal.StringToHGlobalUni(str); 801 | 802 | IntPtr res = Env->Functions->NewString(Env, strUni, str.Length); 803 | 804 | Marshal.FreeHGlobal(strUni); 805 | 806 | using JObject local = new() { Handle = res, ReferenceType = JNI.ReferenceType.Local }; 807 | return NewGlobalRef(local); 808 | } 809 | } 810 | 811 | public static int GetStringLength(JString str) 812 | { 813 | unsafe 814 | { 815 | return Env->Functions->GetStringLength(Env, str.Handle); 816 | } 817 | } 818 | 819 | public static string GetJStringString(JString str) 820 | { 821 | unsafe 822 | { 823 | if (!str.Valid()) 824 | return ""; 825 | 826 | IntPtr res = Env->Functions->GetStringChars(Env, str.Handle, out byte isCopy); 827 | string? resultString = Marshal.PtrToStringUni(res); 828 | Env->Functions->ReleaseStringChars(Env, str.Handle, res); 829 | return resultString ?? ""; 830 | } 831 | } 832 | 833 | private static void ReleaseStringChars(JString str, IntPtr chars) 834 | { 835 | throw new NotImplementedException(); 836 | } 837 | 838 | public static int GetArrayLength(JArray jarray) 839 | { 840 | unsafe 841 | { 842 | return Env->Functions->GetArrayLength(Env, jarray.Handle); 843 | } 844 | } 845 | 846 | public static int GetArrayLength(JObjectArray jarray) where T : JObject, new() 847 | { 848 | unsafe 849 | { 850 | return Env->Functions->GetArrayLength(Env, jarray.Handle); 851 | } 852 | } 853 | 854 | public static T GetObjectArrayElement(JObjectArray array, int index) where T : JObject, new() 855 | { 856 | unsafe 857 | { 858 | IntPtr res = Env->Functions->GetObjectArrayElement(Env, array.Handle, index); 859 | 860 | using JObject local = new() { Handle = res, ReferenceType = JNI.ReferenceType.Local }; 861 | return NewGlobalRef(local); 862 | } 863 | } 864 | 865 | public static void SetObjectArrayElement(JObjectArray array, int index, T value) where T : JObject, new() 866 | { 867 | unsafe 868 | { 869 | Env->Functions->SetObjectArrayElement(Env, array.Handle, index, value.Handle); 870 | } 871 | } 872 | 873 | public static JArray NewArray(int length) 874 | { 875 | unsafe 876 | { 877 | Type t = typeof(T); 878 | IntPtr res; 879 | 880 | if (t == typeof(bool)) 881 | { 882 | res = Env->Functions->NewBooleanArray(Env, length); 883 | } 884 | else if (t == typeof(sbyte)) 885 | { 886 | res = Env->Functions->NewByteArray(Env, length); 887 | } 888 | else if (t == typeof(char)) 889 | { 890 | res = Env->Functions->NewCharArray(Env, length); 891 | } 892 | else if (t == typeof(short)) 893 | { 894 | res = Env->Functions->NewShortArray(Env, length); 895 | } 896 | else if (t == typeof(int)) 897 | { 898 | res = Env->Functions->NewIntArray(Env, length); 899 | } 900 | else if (t == typeof(long)) 901 | { 902 | res = Env->Functions->NewBooleanArray(Env, length); 903 | } 904 | else if (t == typeof(float)) 905 | { 906 | res = Env->Functions->NewBooleanArray(Env, length); 907 | } 908 | else if (t == typeof(double)) 909 | { 910 | res = Env->Functions->NewBooleanArray(Env, length); 911 | } 912 | else 913 | { 914 | throw new ArgumentException($"CallStaticMethod Type {t} not supported."); 915 | } 916 | 917 | using JObject local = new() { Handle = res, ReferenceType = JNI.ReferenceType.Local }; 918 | return NewGlobalRef>(local); 919 | } 920 | } 921 | 922 | public static T[] GetArrayElements(JArray array) 923 | { 924 | unsafe 925 | { 926 | Type t = typeof(T); 927 | int length = GetArrayLength(array); 928 | 929 | if (t == typeof(bool)) 930 | { 931 | byte* arr = Env->Functions->GetBooleanArrayElements(Env, array.Handle, out byte isCopy); 932 | 933 | bool[] buf = new bool[length]; 934 | 935 | for (int i = 0; i < length; i++) 936 | buf[i] = Convert.ToBoolean(arr[i]); 937 | 938 | Env->Functions->ReleaseBooleanArrayElements(Env, array.Handle, arr, (int)JNI.ReleaseMode.Abort); 939 | return (T[])(object)buf; 940 | } 941 | else if (t == typeof(sbyte)) 942 | { 943 | sbyte* arr = Env->Functions->GetByteArrayElements(Env, array.Handle, out byte isCopy); 944 | 945 | sbyte[] buf = new sbyte[length]; 946 | 947 | for (int i = 0; i < length; i++) 948 | buf[i] = arr[i]; 949 | 950 | Env->Functions->ReleaseByteArrayElements(Env, array.Handle, arr, (int)JNI.ReleaseMode.Abort); 951 | return (T[])(object)buf; 952 | } 953 | else if (t == typeof(char)) 954 | { 955 | char* arr = Env->Functions->GetCharArrayElements(Env, array.Handle, out byte isCopy); 956 | 957 | char[] buf = new char[length]; 958 | 959 | for (int i = 0; i < length; i++) 960 | buf[i] = arr[i]; 961 | 962 | Env->Functions->ReleaseCharArrayElements(Env, array.Handle, arr, (int)JNI.ReleaseMode.Abort); 963 | return (T[])(object)buf; 964 | } 965 | else if (t == typeof(short)) 966 | { 967 | short* arr = Env->Functions->GetShortArrayElements(Env, array.Handle, out byte isCopy); 968 | 969 | short[] buf = new short[length]; 970 | 971 | for (int i = 0; i < length; i++) 972 | buf[i] = arr[i]; 973 | 974 | Env->Functions->ReleaseShortArrayElements(Env, array.Handle, arr, (int)JNI.ReleaseMode.Abort); 975 | return (T[])(object)buf; 976 | } 977 | else if (t == typeof(int)) 978 | { 979 | int* arr = Env->Functions->GetIntArrayElements(Env, array.Handle, out byte isCopy); 980 | 981 | int[] buf = new int[length]; 982 | 983 | for (int i = 0; i < length; i++) 984 | buf[i] = arr[i]; 985 | 986 | Env->Functions->ReleaseIntArrayElements(Env, array.Handle, arr, (int)JNI.ReleaseMode.Abort); 987 | return (T[])(object)buf; 988 | } 989 | else if (t == typeof(long)) 990 | { 991 | long* arr = Env->Functions->GetLongArrayElements(Env, array.Handle, out byte isCopy); 992 | 993 | long[] buf = new long[length]; 994 | 995 | for (int i = 0; i < length; i++) 996 | buf[i] = arr[i]; 997 | 998 | Env->Functions->ReleaseLongArrayElements(Env, array.Handle, arr, (int)JNI.ReleaseMode.Abort); 999 | return (T[])(object)buf; 1000 | } 1001 | else if (t == typeof(float)) 1002 | { 1003 | float* arr = Env->Functions->GetFloatArrayElements(Env, array.Handle, out byte isCopy); 1004 | 1005 | float[] buf = new float[length]; 1006 | 1007 | for (int i = 0; i < length; i++) 1008 | buf[i] = arr[i]; 1009 | 1010 | Env->Functions->ReleaseFloatArrayElements(Env, array.Handle, arr, (int)JNI.ReleaseMode.Abort); 1011 | return (T[])(object)buf; 1012 | } 1013 | else if (t == typeof(double)) 1014 | { 1015 | double* arr = Env->Functions->GetDoubleArrayElements(Env, array.Handle, out byte isCopy); 1016 | 1017 | double[] buf = new double[length]; 1018 | 1019 | for (int i = 0; i < length; i++) 1020 | buf[i] = arr[i]; 1021 | 1022 | Env->Functions->ReleaseDoubleArrayElements(Env, array.Handle, arr, (int)JNI.ReleaseMode.Abort); 1023 | return (T[])(object)buf; 1024 | } 1025 | else 1026 | { 1027 | throw new ArgumentException($"GetArrayElements Type {t} not supported."); 1028 | } 1029 | } 1030 | } 1031 | 1032 | public static T[] GetArrayRegion(JArray array, int start, int len) 1033 | { 1034 | unsafe 1035 | { 1036 | Type t = typeof(T); 1037 | 1038 | if (t == typeof(bool)) 1039 | { 1040 | fixed (byte* buf = new byte[len]) 1041 | { 1042 | Env->Functions->GetBooleanArrayRegion(Env, array.Handle, start, len, buf); 1043 | 1044 | bool[] res = new bool[len]; 1045 | for (int i = 0; i < len; i++) 1046 | { 1047 | res[i] = Convert.ToBoolean(buf[i]); 1048 | } 1049 | return (T[])(object)res; 1050 | } 1051 | } 1052 | else if (t == typeof(sbyte)) 1053 | { 1054 | sbyte[] buf = new sbyte[len]; 1055 | 1056 | fixed (sbyte* b = buf) 1057 | { 1058 | Env->Functions->GetByteArrayRegion(Env, array.Handle, start, len, b); 1059 | return (T[])(object)buf; 1060 | } 1061 | } 1062 | else if (t == typeof(char)) 1063 | { 1064 | char[] buf = new char[len]; 1065 | 1066 | fixed (char* b = buf) 1067 | { 1068 | Env->Functions->GetCharArrayRegion(Env, array.Handle, start, len, b); 1069 | return (T[])(object)buf; 1070 | } 1071 | } 1072 | else if (t == typeof(short)) 1073 | { 1074 | short[] buf = new short[len]; 1075 | 1076 | fixed (short* b = buf) 1077 | { 1078 | Env->Functions->GetShortArrayRegion(Env, array.Handle, start, len, b); 1079 | return (T[])(object)buf; 1080 | } 1081 | } 1082 | else if (t == typeof(int)) 1083 | { 1084 | int[] buf = new int[len]; 1085 | 1086 | fixed (int* b = buf) 1087 | { 1088 | Env->Functions->GetIntArrayRegion(Env, array.Handle, start, len, b); 1089 | return (T[])(object)buf; 1090 | } 1091 | } 1092 | else if (t == typeof(long)) 1093 | { 1094 | long[] buf = new long[len]; 1095 | 1096 | fixed (long* b = buf) 1097 | { 1098 | Env->Functions->GetLongArrayRegion(Env, array.Handle, start, len, b); 1099 | return (T[])(object)buf; 1100 | } 1101 | } 1102 | else if (t == typeof(float)) 1103 | { 1104 | float[] buf = new float[len]; 1105 | 1106 | fixed (float* b = buf) 1107 | { 1108 | Env->Functions->GetFloatArrayRegion(Env, array.Handle, start, len, b); 1109 | return (T[])(object)buf; 1110 | } 1111 | } 1112 | else if (t == typeof(double)) 1113 | { 1114 | double[] buf = new double[len]; 1115 | 1116 | fixed (double* b = buf) 1117 | { 1118 | Env->Functions->GetDoubleArrayRegion(Env, array.Handle, start, len, b); 1119 | return (T[])(object)buf; 1120 | } 1121 | } 1122 | else 1123 | { 1124 | throw new ArgumentException($"GetArrayRegion Type {t} not supported."); 1125 | } 1126 | } 1127 | } 1128 | 1129 | public static T GetArrayElement(JArray array, int index) 1130 | { 1131 | Type t = typeof(T); 1132 | 1133 | if (t == typeof(bool)) 1134 | { 1135 | byte b; 1136 | Env->Functions->GetBooleanArrayRegion(Env, array.Handle, index, 1, &b); 1137 | return (T)(object)Convert.ToBoolean(b); 1138 | } 1139 | else if (t == typeof(sbyte)) 1140 | { 1141 | sbyte b; 1142 | Env->Functions->GetByteArrayRegion(Env, array.Handle, index, 1, &b); 1143 | return (T)(object)b; 1144 | } 1145 | else if (t == typeof(char)) 1146 | { 1147 | char c; 1148 | Env->Functions->GetCharArrayRegion(Env, array.Handle, index, 1, &c); 1149 | return (T)(object)c; 1150 | } 1151 | else if (t == typeof(short)) 1152 | { 1153 | short s; 1154 | Env->Functions->GetShortArrayRegion(Env, array.Handle, index, 1, &s); 1155 | return (T)(object)s; 1156 | } 1157 | else if (t == typeof(int)) 1158 | { 1159 | int i; 1160 | Env->Functions->GetIntArrayRegion(Env, array.Handle, index, 1, &i); 1161 | return (T)(object)i; 1162 | } 1163 | else if (t == typeof(long)) 1164 | { 1165 | long l; 1166 | Env->Functions->GetLongArrayRegion(Env, array.Handle, index, 1, &l); 1167 | return (T)(object)l; 1168 | } 1169 | else if (t == typeof(float)) 1170 | { 1171 | float f; 1172 | Env->Functions->GetFloatArrayRegion(Env, array.Handle, index, 1, &f); 1173 | return (T)(object)f; 1174 | } 1175 | else if (t == typeof(double)) 1176 | { 1177 | double d; 1178 | Env->Functions->GetDoubleArrayRegion(Env, array.Handle, index, 1, &d); 1179 | return (T)(object)d; 1180 | } 1181 | else 1182 | { 1183 | throw new ArgumentException($"GetArrayElement Type {t} not supported."); 1184 | } 1185 | } 1186 | 1187 | public static void SetArrayRegion(JArray array, int start, int len, T[] elems) 1188 | { 1189 | unsafe 1190 | { 1191 | Type t = typeof(T); 1192 | 1193 | if (t == typeof(bool)) 1194 | { 1195 | fixed (byte* buf = elems.Select(b => Convert.ToByte(b)).ToArray()) 1196 | { 1197 | Env->Functions->SetBooleanArrayRegion(Env, array.Handle, start, len, null); 1198 | } 1199 | } 1200 | else if (t == typeof(sbyte)) 1201 | { 1202 | fixed (sbyte* buf = (sbyte[])(object)elems) 1203 | { 1204 | Env->Functions->SetByteArrayRegion(Env, array.Handle, start, len, buf); 1205 | } 1206 | } 1207 | else if (t == typeof(char)) 1208 | { 1209 | fixed (char* buf = (char[])(object)elems) 1210 | { 1211 | Env->Functions->SetCharArrayRegion(Env, array.Handle, start, len, buf); 1212 | } 1213 | } 1214 | else if (t == typeof(short)) 1215 | { 1216 | fixed (short* buf = (short[])(object)elems) 1217 | { 1218 | Env->Functions->SetShortArrayRegion(Env, array.Handle, start, len, buf); 1219 | } 1220 | } 1221 | else if (t == typeof(int)) 1222 | { 1223 | fixed (int* buf = (int[])(object)elems) 1224 | { 1225 | Env->Functions->SetIntArrayRegion(Env, array.Handle, start, len, buf); 1226 | } 1227 | } 1228 | else if (t == typeof(long)) 1229 | { 1230 | fixed (long* buf = (long[])(object)elems) 1231 | { 1232 | Env->Functions->SetLongArrayRegion(Env, array.Handle, start, len, buf); 1233 | } 1234 | } 1235 | else if (t == typeof(float)) 1236 | { 1237 | fixed (float* buf = (float[])(object)elems) 1238 | { 1239 | Env->Functions->SetFloatArrayRegion(Env, array.Handle, start, len, buf); 1240 | } 1241 | } 1242 | else if (t == typeof(double)) 1243 | { 1244 | fixed (double* buf = (double[])(object)elems) 1245 | { 1246 | Env->Functions->SetDoubleArrayRegion(Env, array.Handle, start, len, buf); 1247 | } 1248 | } 1249 | else 1250 | { 1251 | throw new ArgumentException($"SetArrayRegion Type {t} not supported."); 1252 | } 1253 | } 1254 | } 1255 | 1256 | public static void SetArrayElement(JArray array, int index, T value) 1257 | { 1258 | Type t = typeof(T); 1259 | 1260 | if (t == typeof(bool)) 1261 | { 1262 | byte b = Convert.ToByte(value); 1263 | Env->Functions->SetBooleanArrayRegion(Env, array.Handle, index, 1, &b); 1264 | } 1265 | else if (t == typeof(sbyte)) 1266 | { 1267 | sbyte b = (sbyte)(object)value; 1268 | Env->Functions->SetByteArrayRegion(Env, array.Handle, index, 1, &b); 1269 | } 1270 | else if (t == typeof(char)) 1271 | { 1272 | char c = (char)(object)value; 1273 | Env->Functions->SetCharArrayRegion(Env, array.Handle, index, 1, &c); 1274 | } 1275 | else if (t == typeof(short)) 1276 | { 1277 | short s = (short)(object)value; 1278 | Env->Functions->SetShortArrayRegion(Env, array.Handle, index, 1, &s); 1279 | } 1280 | else if (t == typeof(int)) 1281 | { 1282 | int c = (int)(object)value; 1283 | Env->Functions->SetIntArrayRegion(Env, array.Handle, index, 1, &c); 1284 | } 1285 | else if (t == typeof(long)) 1286 | { 1287 | long l = (long)(object)value; 1288 | Env->Functions->SetLongArrayRegion(Env, array.Handle, index, 1, &l); 1289 | } 1290 | else if (t == typeof(float)) 1291 | { 1292 | float f = (float)(object)value; 1293 | Env->Functions->SetFloatArrayRegion(Env, array.Handle, index, 1, &f); 1294 | } 1295 | else if (t == typeof(double)) 1296 | { 1297 | double d = (double)(object)value; 1298 | Env->Functions->SetDoubleArrayRegion(Env, array.Handle, index, 1, &d); 1299 | } 1300 | else 1301 | { 1302 | throw new ArgumentException($"SetArrayElement Type {t} not supported."); 1303 | } 1304 | } 1305 | 1306 | private static int RegisterNatives(JClass cls, IntPtr methods, int nmethods) 1307 | { 1308 | throw new NotImplementedException(); 1309 | } 1310 | 1311 | private static int UnregisterNatives(JClass cls) 1312 | { 1313 | throw new NotImplementedException(); 1314 | } 1315 | 1316 | public static int MonitorEnter(JObject obj) 1317 | { 1318 | unsafe 1319 | { 1320 | return Env->Functions->MonitorEnter(Env, obj.Handle); 1321 | } 1322 | } 1323 | 1324 | public static int MonitorExit(JObject obj) 1325 | { 1326 | unsafe 1327 | { 1328 | return Env->Functions->MonitorExit(Env, obj.Handle); 1329 | } 1330 | } 1331 | 1332 | private static JavaVM* GetJavaVM() 1333 | { 1334 | throw new NotImplementedException(); 1335 | } 1336 | 1337 | public static string? GetStringRegion(JString str, int start, int len) 1338 | { 1339 | unsafe 1340 | { 1341 | //Probably need to allocate space? 1342 | IntPtr buf = IntPtr.Zero; 1343 | Env->Functions->GetStringRegion(Env, str.Handle, start, len, buf); 1344 | 1345 | if (buf != IntPtr.Zero) 1346 | { 1347 | return Marshal.PtrToStringUni(buf); 1348 | } 1349 | 1350 | return null; 1351 | } 1352 | } 1353 | 1354 | private static IntPtr GetPrimitiveArrayCritical(JArray array) 1355 | { 1356 | throw new NotImplementedException(); 1357 | } 1358 | 1359 | private static void ReleasePrimitiveArrayCritical(JArray array, IntPtr carray, int mode) 1360 | { 1361 | throw new NotImplementedException(); 1362 | } 1363 | 1364 | private static string GetStringCritical(JString str) 1365 | { 1366 | throw new NotImplementedException(); 1367 | } 1368 | 1369 | private static void ReleaseStringCritical(JString str) 1370 | { 1371 | throw new NotImplementedException(); 1372 | } 1373 | 1374 | public static T NewWeakGlobalRef(JObject obj) where T : JObject, new() 1375 | { 1376 | unsafe 1377 | { 1378 | IntPtr res = Env->Functions->NewWeakGlobalRef(Env, obj.Handle); 1379 | return new T() { Handle = res, ReferenceType = JNI.ReferenceType.WeakGlobal }; 1380 | } 1381 | } 1382 | 1383 | public static void DeleteWeakGlobalRef(JObject obj) 1384 | { 1385 | unsafe 1386 | { 1387 | Env->Functions->DeleteWeakGlobalRef(Env, obj.Handle); 1388 | } 1389 | } 1390 | 1391 | public static bool ExceptionCheck() 1392 | { 1393 | unsafe 1394 | { 1395 | return Convert.ToBoolean(Env->Functions->ExceptionCheck(Env)); 1396 | } 1397 | } 1398 | 1399 | private static JObject NewDirectByteBuffer(IntPtr address, int capacity) 1400 | { 1401 | throw new NotImplementedException(); 1402 | } 1403 | 1404 | private static IntPtr GetDirectBufferAddress(JObject buf) 1405 | { 1406 | throw new NotImplementedException(); 1407 | } 1408 | 1409 | private static int GetDirectBufferCapacity(JObject obj) 1410 | { 1411 | throw new NotImplementedException(); 1412 | } 1413 | } 1414 | --------------------------------------------------------------------------------