在C#访问的内存地址 [英] Access memory address in c#

查看:361
本文介绍了在C#访问的内存地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与一个ActiveX组件,给了我一个内存地址和字节数接口。

I am interfacing with an ActiveX component that gives me a memory address and the number of bytes.

我怎么能写将要访问的字节开始在给定的内存地址的C#程序?有没有办法做到这一点本身,还是我将不得不接口到C ++?请问ActiveX组件和我的程序共享同一内存/地址空间?

How can I write a C# program that will access the bytes starting at a given memory address? Is there a way to do it natively, or am I going to have to interface to C++? Does the ActiveX component and my program share the same memory/address space?

推荐答案

我强烈建议你使用一个IntPtr和元帅。复制。这里是让你开始一些代码。
memAddr是你给出的内存地址,而由bufSize的大小。

I highly suggest you use an IntPtr and Marshal.Copy. Here is some code to get you started. memAddr is the memory address you are given, and bufSize is the size.

IntPtr bufPtr = new IntPtr(memAddr);
byte[] data = new byte[bufSize];
Marshal.Copy(bufPtr, data, 0, bufSize);

这不要求你使用它需要的/不安全编译器选项,而不是不安全的代码由CLR验证。

This doesn't require you to use unsafe code which requires the the /unsafe compiler option and is not verifiable by the CLR.

如果你需要的不是字节以外的东西数组,只需更改第二行。 Marshal.Copy有一堆重载的。

If you need an array of something other than bytes, just change the second line. Marshal.Copy has a bunch of overloads.

这篇关于在C#访问的内存地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆