作成の組み立てのためのX86/X64での使用にC#
質問
付け加えたいと思いcpuid機能を私C#アプリです。見 この 興味深いブログ。いが必要MASM集が
- いつ、どのように始まりますか?
- 思っているのかわからない難しいコンパイルにdllの両方のX86およびX64がんの手がかり方法についてなどもふもふをご堪能ください(したビットを押します。
それ以上大歓迎!
解決
CPUIDは巨大な痛みやまにそのパスが回避できます。CPUID結果が異なIntelやAMDプロセッサ(少なくとも、興味深いもののようにhyperthreadingキャッシュやトポロジーではありませんが特に安定的に異なるプロセッサできます。(新しいインテルi7プロセッサを新たに導入すCPUID値(eax=0xb)supercedesの情報支援CPUID以前のプロセッサ).
場合に逃げることができ、最良のベットが利用でWMIを参照 Win32_Processor 又は GetLogicalProcessorInformation.
それを大幅に簡素化により管理可能な溶液であれば対応プラットフォーム(取得論理的プロセッサー情報から必要とWinXP sp3以降のクライアント側またはWindows Server2008年以降のサーバー側面)。●
またしまうかのようなとCPUID、何を思っていを簡単なスタブでの実行ができるCPUIDの返却の結果を管理コード(必要なさまざまなバージョンは32-bitおよび64ビット)を内のシステムで管理願います。これにより作成のネイティブアプリケーションおよびその抽出のための生命令のバイトマCPUID法をバイト配列で実行することからコンポーネントです。
こうい始めたのは32ビットの支援のみ
using System;
using System.Runtime.InteropServices;
static class Program {
static void Main() {
//Allocate the executable buffer on a distinct page
// rather than just pinning it in place because we
// need to mark the page as executable.
// Failing to do this would cause NX-enabled machines
// to have access violations attempting to execute.
IntPtr pExecutableBuffer = VirtualAlloc(
IntPtr.Zero,
new IntPtr(CPUID_32.Length),
AllocationType.MEM_COMMIT | AllocationType.MEM_RESERVE,
MemoryProtection.PAGE_EXECUTE_READWRITE
);
Marshal.Copy(CPUID_32, 0, pExecutableBuffer, CPUID_32.Length);
CPUID executeHandler = (CPUID)Marshal.GetDelegateForFunctionPointer(
pExecutableBuffer, typeof(CPUID));
CPUID_Args args = new CPUID_Args();
args.eax = 0;
executeHandler(ref args);
Console.WriteLine("eax: {0} ebx: {1} ecx: {2} edx: {3}",
args.eax,
args.ebx,
args.ecx,
args.edx);
VirtualFree(
pExecutableBuffer,
IntPtr.Zero,
FreeType.MEM_RELEASE);
}
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
delegate void CPUID(ref CPUID_Args args);
private static readonly byte[] CPUID_32 = new byte[] {
0x53, // push ebx
0x57, // push edi
0x8B, 0x7C, 0x24, 0x0C, // mov edi,dword ptr [esp+0Ch]
0x8B, 0x07, // mov eax,dword ptr [edi]
0x8B, 0x4F, 0x08, // mov ecx,dword ptr [edi+8]
0x0F, 0xA2, // cpuid
0x89, 0x07, // mov dword ptr [edi],eax
0x89, 0x5F, 0x04, // mov dword ptr [edi+4],ebx
0x89, 0x4F, 0x08 , // movdword ptr [edi+8],ecx
0x89, 0x57, 0x0C , // mov dword ptr [edi+0Ch],edx
0x5F, // pop edi
0x5B, // pop ebx
0xC2, 0x04, 0x00 // ret
};
[Flags]
enum AllocationType {
MEM_COMMIT = 0x1000,
MEM_RESERVE = 0x2000,
}
[Flags]
enum MemoryProtection {
PAGE_EXECUTE_READWRITE = 0x40,
}
[Flags]
enum FreeType {
MEM_RELEASE = 0x8000
}
[DllImport("kernel32.dll")]
static extern IntPtr VirtualAlloc(
IntPtr lpAddress,
IntPtr dwSize,
AllocationType flAllocationType,
MemoryProtection flProtect);
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool VirtualFree(
IntPtr lpAddress,
IntPtr dwSize,
FreeType dwFreeType);
}
[StructLayout(LayoutKind.Sequential)]
struct CPUID_Args {
public uint eax;
public uint ebx;
public uint ecx;
public uint edx;
}
他のヒント
私はあなたがC ++コードのインラインアセンブラ構文(__asm
)を用いたネイティブCLRアセンブリを構築することができると思います。
所属していません StackOverflow