从字节数组运行程序 [英] Run Program from byte array
问题描述
我有存储在字节数组的程序。
I have a program stored in byte array.
是否有可能运行它里面的C#?
Is it possible to run it inside C#?
推荐答案
是的。 <一href=\"http://stackoverflow.com/questions/959087/is-it-possible-to-execute-an-x86-assembly-sequence-from-within-c/959144#959144\">This答案表明您可以直接执行字节数组的内容。基本上,你可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/aa366887%28VS.85%29.aspx\"><$c$c>VirtualAlloc$c$c>用一个已知的地址在堆中分配可执行区域(A <一个href=\"http://msdn.microsoft.com/en-us/library/system.intptr%28VS.71%29.aspx\"><$c$c>IntPtr$c$c>).然后,你的字节数组到该地址与 Marshal.Copy $ C $复制C>
。您的指针转换为委托与<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.getdelegateforfunctionpointer.aspx\"><$c$c>GetDelegateForFunctionPointer$c$c>,终于把它作为一个正常的委托。
Yes. This answer shows you can directly execute the contents of a byte array. Basically, you use VirtualAlloc
to allocate an executable region on the heap with a known address (a IntPtr
). You then copy your byte array to that address with Marshal.Copy
. You convert the pointer to a delegate with GetDelegateForFunctionPointer
, and finally call it as a normal delegate.
这篇关于从字节数组运行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!