private void Prepare() { byte[] codeBytes; if (IntPtr.Size == 4) { codeBytes = s_x86CodeBytes; } else { codeBytes = s_x64CodeBytes; } m_codePointer = VirtualAlloc( IntPtr.Zero, new UIntPtr((uint)codeBytes.Length), AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE ); Marshal.Copy(codeBytes, 0, m_codePointer, codeBytes.Length); if (IntPtr.Size == 4) { m_cpuIdDelg = (CpuIDDelegate)Marshal.GetDelegateForFunctionPointer(m_codePointer, typeof(CpuIDDelegate)); } else { m_cpuIdDelg64 = (CpuIDDelegate64)Marshal.GetDelegateForFunctionPointer(m_codePointer, typeof(CpuIDDelegate64)); } }
public static byte[] Invoke(int level) { IntPtr codePointer = IntPtr.Zero; try { // compile byte[] codeBytes; if (IntPtr.Size == 4) { codeBytes = x86CodeBytes; } else { codeBytes = x64CodeBytes; } codePointer = VirtualAlloc( IntPtr.Zero, new UIntPtr((uint)codeBytes.Length), AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE ); Marshal.Copy(codeBytes, 0, codePointer, codeBytes.Length); CpuIDDelegate cpuIdDelg = (CpuIDDelegate)Marshal.GetDelegateForFunctionPointer(codePointer, typeof(CpuIDDelegate)); // invoke GCHandle handle = default(GCHandle); var buffer = new byte[16]; try { handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); cpuIdDelg(level, buffer); } finally { if (handle != default(GCHandle)) { handle.Free(); } } return(buffer); } finally { if (codePointer != IntPtr.Zero) { VirtualFree(codePointer, 0, 0x8000); codePointer = IntPtr.Zero; } } }
/// <summary> /// Invoke the code that will read the requested CPUId leaf data and return it to managed memory /// </summary> /// <param name="leaf">Requested processor leaf</param> /// <returns>A CpuId structure containing the EAX, EBX, ECX and EDX register values.</returns> /// <remarks>This class is marked internal to keep it out of the ASCOM Help file. It can be accessed from other Platform assemblies by /// including an "InternalsVisibleToAttribute" for each external component that requires access. At January 2018 this is just Diagnostics.</remarks> internal static CpuIdResult Invoke(int leaf) { IntPtr codePointer = IntPtr.Zero; IntPtr buffer = IntPtr.Zero; CpuIdResult result; try { // Get 32 or 64bit code as necessary byte[] codeBytes; if (IntPtr.Size == 4) { codeBytes = x86CodeBytes; } else { codeBytes = x64CodeBytes; } // Get a pointer to an allocated memory space that will hold the code codePointer = VirtualAlloc(IntPtr.Zero, new UIntPtr((uint)codeBytes.Length), AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE); // Copy the code into the buffer Marshal.Copy(codeBytes, 0, codePointer, codeBytes.Length); // Create a delegate to run the code and return the result CpuIDDelegate cpuIdDelg = (CpuIDDelegate)Marshal.GetDelegateForFunctionPointer(codePointer, typeof(CpuIDDelegate)); // Allocate some memory to hold the returned data, call the delegate and save the resulting information try { buffer = Marshal.AllocHGlobal(16); cpuIdDelg(leaf, buffer); result = (CpuIdResult)Marshal.PtrToStructure(buffer, typeof(CpuIdResult)); } finally // Release the data buffer { if (buffer != IntPtr.Zero) { Marshal.FreeHGlobal(buffer); } } return(result); } finally // Release the memory space holding the code { if (codePointer != IntPtr.Zero) { VirtualFree(codePointer, 0, 0x8000); codePointer = IntPtr.Zero; } } }
public static byte[] Invoke(uint leaf, uint subleaf = 0) { IntPtr codePointer = IntPtr.Zero; try { // compile byte[] codeBytes = Environment.Is64BitProcess ? x64CodeBytes : x86CodeBytes; codePointer = VirtualAlloc( IntPtr.Zero, new UIntPtr((UInt32)codeBytes.Length), AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE ); Marshal.Copy(codeBytes, 0, codePointer, codeBytes.Length); CpuIDDelegate cpuIdDelg = (CpuIDDelegate)Marshal.GetDelegateForFunctionPointer(codePointer, typeof(CpuIDDelegate)); // invoke GCHandle handle = default(GCHandle); var buffer = new byte[16]; try { handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); cpuIdDelg(subleaf, leaf, buffer); } finally { if (handle != default(GCHandle)) { handle.Free(); } } return(buffer); } finally { if (codePointer != IntPtr.Zero) { VirtualFree(codePointer, 0, 0x8000); codePointer = IntPtr.Zero; } } }
public CpuIdAssemblyCode() { byte[] codeBytes = (IntPtr.Size == 4) ? x86CodeBytes : x64CodeBytes; _size = (uint)codeBytes.Length; _codePointer = NativeMethods.Kernel32.VirtualAlloc( IntPtr.Zero, new UIntPtr(_size), AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE ); Marshal.Copy(codeBytes, 0, _codePointer, codeBytes.Length); #if NET40 _delegate = (CpuIDDelegate)Marshal.GetDelegateForFunctionPointer(_codePointer, typeof(CpuIDDelegate)); #else _delegate = Marshal.GetDelegateForFunctionPointer <CpuIDDelegate>(_codePointer); #endif }