我正在看这个问题:
How to implement multiplication without using multiplication operator in .NET并且实际上有很多乐趣试图想法如何在不使用*的情况下成倍增加.
但是我对这个答案感到很头疼.我不知道这段代码发生了什么.
有人可以向我解释一下吗?
using System; using System.Runtime.InteropServices; delegate uint BinaryOp(uint a,uint b); static class Program { [DllImport("kernel32.dll",SetLastError = true)] static extern bool VirtualProtect( IntPtr address,IntPtr size,uint protect,out uint oldProtect); static void Main() { var bytes = IntPtr.Size == sizeof(int) ? //32-bit? It's slower BTW new byte[] {0x8B,0x44,0x24,0x04,0x0F,0xAF,0x08,0xC3} : new byte[] {0x0F,0xCA,0x8B,0xC1,0xC3}; var handle = GCHandle.Alloc(bytes,GCHandleType.Pinned); try { uint old; VirtualProtect(handle.AddrOfPinnedobject(),(IntPtr)bytes.Length,0x40,out old); var action = (BinaryOp)Marshal.GetDelegateForFunctionPointer( handle.AddrOfPinnedobject(),typeof(BinaryOp)); var temp = action(3,2); //6! } finally { handle.Free(); } } }
这段代码归功于Mehrdad.
解决方法
原文地址:https://www.jb51.cc/csharp/98224.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。