private static unsafe byte[] CPUID1() { byte[] buffer = new byte[12]; if (IntPtr.Size == 4) { IntPtr p = NativeMethods.VirtualAlloc( IntPtr.Zero, new UIntPtr((uint)x86_CPUID1_INSNS.Length), AllocationTypes.Commit | AllocationTypes.Reserve, MemoryProtections.ExecuteReadWrite); try { Marshal.Copy(x86_CPUID1_INSNS, 0, p, x86_CPUID1_INSNS.Length); CPUID1Delegate del = (CPUID1Delegate)Marshal.GetDelegateForFunctionPointer(p, typeof(CPUID1Delegate)); 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_CPUID1_INSNS.Length), AllocationTypes.Commit | AllocationTypes.Reserve, MemoryProtections.ExecuteReadWrite); try { Marshal.Copy(x64_CPUID1_INSNS, 0, p, x64_CPUID1_INSNS.Length); CPUID1Delegate del = (CPUID1Delegate)Marshal.GetDelegateForFunctionPointer(p, typeof(CPUID1Delegate)); fixed(byte *newBuffer = &buffer[0]) { del(newBuffer); } } finally { NativeMethods.VirtualFree(p, 0, FreeTypes.Release); } } return(buffer); }
unsafe static byte[] CPUID1() { 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_CPUID1_INSNS.Length), AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE); try { Marshal.Copy(x86_CPUID1_INSNS, 0, p, x86_CPUID1_INSNS.Length); CPUID1Delegate del = (CPUID1Delegate)Marshal.GetDelegateForFunctionPointer(p, typeof(CPUID1Delegate)); 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_CPUID1_INSNS.Length), AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE); try { Marshal.Copy(x64_CPUID1_INSNS, 0, p, x64_CPUID1_INSNS.Length); CPUID1Delegate del = (CPUID1Delegate)Marshal.GetDelegateForFunctionPointer(p, typeof(CPUID1Delegate)); fixed(byte *newBuffer = &buffer[0]) { del(newBuffer); } } finally { VirtualFree(p, 0, 0x8000); } } return(buffer); }
unsafe static byte[] CPUID1() { 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) { CPUID1Delegate del = new CPUID1Delegate(CPUID1a); IntPtr p = VirtualAlloc(IntPtr.Zero, new UIntPtr((uint)x86_CPUID1_INSNS.Length), AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE); try { Marshal.Copy(x86_CPUID1_INSNS, 0, p, x86_CPUID1_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) { CPUID1Delegate del = new CPUID1Delegate(CPUID1a); IntPtr p = VirtualAlloc(IntPtr.Zero, new UIntPtr((uint)x64_CPUID1_INSNS.Length), AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE); try { Marshal.Copy(x64_CPUID1_INSNS, 0, p, x64_CPUID1_INSNS.Length); _methodPtr.SetValue(del, p); // Set our delegate to our x86 code del(ref buffer); } finally { VirtualFree(p, 0, 0x8000); } } return(buffer); }