private static unsafe byte[] CPUID0() { byte[] buffer = new byte[12]; if (IntPtr.Size == 4) { IntPtr p = NativeMethods.VirtualAlloc( IntPtr.Zero, new UIntPtr((uint)x86_CPUID0_INSNS.Length), AllocationTypes.Commit | AllocationTypes.Reserve, MemoryProtections.ExecuteReadWrite); try { Marshal.Copy(x86_CPUID0_INSNS, 0, p, x86_CPUID0_INSNS.Length); CPUID0Delegate del = (CPUID0Delegate)Marshal.GetDelegateForFunctionPointer(p, typeof(CPUID0Delegate)); fixed(byte *newBuffer = &buffer[0]) { del(newBuffer); } } finally { NativeMethods.VirtualFree(p, 0, FreeTypes.Release); } } else if (IntPtr.Size == 8) { IntPtr p = NativeMethods.VirtualAlloc( IntPtr.Zero, new UIntPtr((uint)x64_CPUID0_INSNS.Length), AllocationTypes.Commit | AllocationTypes.Reserve, MemoryProtections.ExecuteReadWrite); try { Marshal.Copy(x64_CPUID0_INSNS, 0, p, x64_CPUID0_INSNS.Length); CPUID0Delegate del = (CPUID0Delegate)Marshal.GetDelegateForFunctionPointer(p, typeof(CPUID0Delegate)); fixed(byte *newBuffer = &buffer[0]) { del(newBuffer); } } finally { NativeMethods.VirtualFree(p, 0, FreeTypes.Release); } } return(buffer); }
unsafe static byte[] CPUID0() { byte[] buffer = new byte[12]; // Get the FieldInfo for "_methodPtr" Type delType = typeof(Delegate); FieldInfo _methodPtr = delType.GetField("_methodPtr", BindingFlags.NonPublic | BindingFlags.Instance); if (IntPtr.Size == 4) { IntPtr p = VirtualAlloc(IntPtr.Zero, new UIntPtr((uint)x86_CPUID0_INSNS.Length), AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE); try { Marshal.Copy(x86_CPUID0_INSNS, 0, p, x86_CPUID0_INSNS.Length); CPUID0Delegate del = (CPUID0Delegate)Marshal.GetDelegateForFunctionPointer(p, typeof(CPUID0Delegate)); fixed(byte *newBuffer = &buffer[0]) { del(newBuffer); } } finally { VirtualFree(p, 0, 0x8000); } } else if (IntPtr.Size == 8) { IntPtr p = VirtualAlloc(IntPtr.Zero, new UIntPtr((uint)x64_CPUID0_INSNS.Length), AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE); try { Marshal.Copy(x64_CPUID0_INSNS, 0, p, x64_CPUID0_INSNS.Length); CPUID0Delegate del = (CPUID0Delegate)Marshal.GetDelegateForFunctionPointer(p, typeof(CPUID0Delegate)); fixed(byte *newBuffer = &buffer[0]) { del(newBuffer); } } finally { VirtualFree(p, 0, 0x8000); } } return(buffer); }
unsafe static byte[] CPUID0() { byte[] buffer = new byte[12]; // Get the FieldInfo for "_methodPtr" Type delType = typeof(Delegate); FieldInfo _methodPtr = delType.GetField("_methodPtr", BindingFlags.NonPublic | BindingFlags.Instance); if (IntPtr.Size == 4) { CPUID0Delegate del = new CPUID0Delegate(CPUID0a); IntPtr p = VirtualAlloc(IntPtr.Zero, new UIntPtr((uint)x86_CPUID0_INSNS.Length), AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE); try { Marshal.Copy(x86_CPUID0_INSNS, 0, p, x86_CPUID0_INSNS.Length); _methodPtr.SetValue(del, p); // Set our delegate to our x86 code del(ref buffer); } finally { VirtualFree(p, 0, 0x8000); } } else if (IntPtr.Size == 8) { CPUID0Delegate del = new CPUID0Delegate(CPUID0a); IntPtr p = VirtualAlloc(IntPtr.Zero, new UIntPtr((uint)x64_CPUID0_INSNS.Length), AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE); try { Marshal.Copy(x64_CPUID0_INSNS, 0, p, x64_CPUID0_INSNS.Length); _methodPtr.SetValue(del, p); // Set our delegate to our x86 code del(ref buffer); } finally { VirtualFree(p, 0, 0x8000); } } return(buffer); }