怎么用C# 把核心代码编译成二进制数据
private void button1_Click(object sender, System.EventArgs e)
{
// native function's compiled code
byte[] proc = new byte[] {
0x55, 0x8B, 0xEC, 0x83, 0xEC, 0x00, 0x53, 0x51,
0x52, 0x57, 0x8B, 0x7D, 0x08, 0x33, 0xC0, 0x0F,
0xA2, 0x89, 0x07, 0x89, 0x5F, 0x04, 0x89, 0x57,
0x08, 0x89, 0x4F, 0x0C, 0xB8, 0x01, 0x00, 0x00,
0x00, 0x0F, 0xA2, 0x89, 0x47, 0x10, 0x89, 0x57,
0x14, 0xB8, 0x00, 0x00, 0x00, 0x80, 0x0F, 0xA2,
0x3D, 0x00, 0x00, 0x00, 0x80, 0x72, 0x0A, 0xB8,
0x01, 0x00, 0x00, 0x80, 0x0F, 0xA2, 0x89, 0x57,
0x18, 0x5F, 0x59, 0x5B, 0x5A, 0x8B, 0xE5, 0x5D,
0x33, 0xC0, 0xC2, 0x04, 0x00
};
UInt32 funcAddr = VirtualAlloc(0, (UInt32)proc.Length,
MEM_COMMIT, PAGE_EXECUTE_READWRITE);
Marshal.Copy(proc, 0, (IntPtr)(funcAddr), proc.Length);
IntPtr hThread = IntPtr.Zero;
UInt32 threadId = 0;
// prepare data
PROCESSOR_INFO info = new PROCESSOR_INFO();
IntPtr pinfo =
Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PROCESSOR_INFO)));
Marshal.StructureToPtr(info, pinfo, false);
// execute native code
hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId);
WaitForSingleObject(hThread, 0xFFFFFFFF);
// retrive data
info = (PROCESSOR_INFO)Marshal.PtrToStructure(pinfo,
typeof(PROCESSOR_INFO));
Marshal.FreeHGlobal(pinfo);
CloseHandle(hThread);
VirtualFree((IntPtr)funcAddr, 0, MEM_RELEASE);
}
用本机图像生成器(NGen.exe)可以编译部分本机代码 |