Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用VirtualAlloc和GetDelegateForFunctionPointer在C#中运行字节数组时出错_C#_Delegates_Virtualalloc - Fatal编程技术网

使用VirtualAlloc和GetDelegateForFunctionPointer在C#中运行字节数组时出错

使用VirtualAlloc和GetDelegateForFunctionPointer在C#中运行字节数组时出错,c#,delegates,virtualalloc,C#,Delegates,Virtualalloc,我有一个用c#创建的Windows窗体程序,它只是一个窗体和一个按钮。我想在这里实现的是使用VirtualAlloc和委托执行硬编码字节数组。此硬编码字节数组属于wrar.exe安装程序的字节。我只是想试试它是否管用。选择winrar安装程序没有特殊原因。因此,在按钮单击事件中,我有以下代码: private UInt32 MEM_COMMIT = 0x1000; private UInt32 PAGE_EXECUTE_READWRITE = 0x40; private UInt32 MEM_R

我有一个用c#创建的Windows窗体程序,它只是一个窗体和一个按钮。我想在这里实现的是使用VirtualAlloc和委托执行硬编码字节数组。此硬编码字节数组属于wrar.exe安装程序的字节。我只是想试试它是否管用。选择winrar安装程序没有特殊原因。因此,在按钮单击事件中,我有以下代码:

private UInt32 MEM_COMMIT = 0x1000;
private UInt32 PAGE_EXECUTE_READWRITE = 0x40;
private UInt32 MEM_RELEASE = 0x8000;
private delegate void Runner();

[DllImport("kernel32")]
private static extern IntPtr VirtualAlloc(UInt32 lpStartAddr, UInt32 size, UInt32 flAllocationType, UInt32 flProtect);

byte[] body = new byte[1517376] { <actual bytes of the winrar installer EXE>};


private void btnExit_Click(object sender, EventArgs e)
{
        try
        {
            IntPtr buf = VirtualAlloc(0, (UInt32)body.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
            Marshal.Copy(body, 0, (IntPtr)buf, body.Length);
            Runner ptr = (Runner)Marshal.GetDelegateForFunctionPointer(buf, typeof(Runner));
            ptr();
            Application.Exit();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
}
private UInt32 MEM_COMMIT=0x1000;
私有UInt32页\u执行\u读写=0x40;
专用UInt32内存释放=0x8000;
私有委托无效运行程序();
[DllImport(“内核32”)]
专用静态外部IntPtr VirtualAlloc(UInt32 LPSTARTADR、UInt32大小、UInt32 flAllocationType、UInt32 flProtect);
字节[]正文=新字节[1517376]{


我做错了什么?这似乎与内存分配有关。我如何解决这个问题?非常感谢!您编写的代码是用来调用存储在内存中的函数的


您存储的不是函数而是可执行文件。您需要找到可执行文件入口点的偏移量,然后调用它。

因此,您可以加载表示内存中某个exe的字节,然后期望它运行?为什么不进行处理?启动该exe?exe文件的入口点不在字节0处,即使您通过了批准在GetDelegateForFunctionPointer的地址之后,仍然缺少很多东西-您还没有解析可执行文件的任何动态导入,它进行的任何WinAPI或内核调用可能会与“进程”这一事实非常混淆没有合适的HModule。原则上你想做的事情是可能的,但你离做这件事还有很长的路要走,而且这可能是解决问题的错误方法。谢谢你的反馈!还有可能做操作系统加载器做的很多其他事情,比如分配内存、修复跳转地址……我猜你的代码是一个保护程序对于某件事,你的最终目标是什么?如果是代码注入,你的做法正好相反。