c# - 什么时候读写内存合法?

标签 c# pointers memory

如有错误请指正。我问这个问题是为了澄清我的一些想法。

今天在学校我了解到,当一个进程(程序)执行时,操作系统会在内存中为其分配一个空间。以这两个程序为例:

程序 1:

    static void Main(string[] args)
    {

        unsafe // in debug options on the properties enable unsafe code
        {

            int a = 2;

            int* pointer_1 = &a; // create pointer1 to point to the address of variable a

            // breakpoint in here !!!!!!!!!!!!!!!!!

            // in the debug I should be able to see the address of pointer1. Copy it and 
            // type it in the console

            string hexValue = Console.ReadLine();

            // convert the hex value to base 10
            int decValue = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

            // create a new pointer that point to the address that I typed in the console
            IntPtr pointer_2 = new IntPtr(decValue);

            Console.WriteLine("The address of a: {0} Value {1}", ((int)&a), a);

            try
            {
                Console.WriteLine("The address of {0} points to value {1}", (int)&pointer_1, (int)pointer_2);

                // different approach of acomplishing the same
                Console.WriteLine(Marshal.ReadInt32(pointer_2));
            }
            catch
            {
                Console.WriteLine(@"you are supposed to be debuging this program.");                    
            }

        }

方案二

    static void Main(string[] args)
    {            
        unsafe
        {
            IntPtr pointer_0 = new IntPtr(95151860); // see address of variable from program1
            int* pointer_1 = (int*)pointer_0;
            // try to access program's 1 object
            Console.WriteLine("addrees of {0} points to value {1} ", (int)&pointer_1, *pointer_1); 
        }
    }

所以我明白在程序 2 中我会得到一个错误。我将访问受限内存。到目前为止,我所学到的是有道理的。

好吧,这里是没有意义的地方。

有一个非常好的程序叫做 AutoIt用于自动执行任务。例如它可以发送鼠标点击、移动鼠标​​、发送击键等。

无论如何,autoit 附带一个名为 AutoIt Window Info 的程序,该程序使您能够获取窗口控件的句柄(指针)。例如,我可以通过将查找器工具拖动到我希望获取信息的控件来查看窗口控件的句柄:

enter image description here

在这张图片中,我正在将查找器工具拖到计算器的输入控件中。例如,我也可以将它拖到按钮 7。

所以如果你在图片中看到我现在有那个控件的地址。然后我就可以从我的程序中访问它了!!


又一个如何访问不属于我的程序的内存的例子

第 1 步)使用 autoit 窗口信息获取任何窗口的指针

第 2 步)在我的计算机中,指针是:

enter image description here

那是谷歌浏览器的窗口,就是我输入这个问题的地方。

这个类会向后发送一个窗口:

    public static class SendWndToBack
    {
        [DllImport("user32.dll")]
        static extern bool SetWindowPos(
            IntPtr hWnd,
            IntPtr hWndInsertAfter,
            int X,
            int Y,
            int cx,
            int cy,
            uint uFlags);

        const UInt32 SWP_NOSIZE = 0x0001;
        const UInt32 SWP_NOMOVE = 0x0002;
        const UInt32 SWP_NOACTIVATE = 0x0010;

        static readonly IntPtr HWND_BOTTOM = new IntPtr(1);

        static readonly IntPtr k = new IntPtr(12);

        public static void WindowHandle(IntPtr windowHandle)
        {
            SetWindowPos(windowHandle, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
        }            
    }

然后如果我使用我刚刚在 autoit 的帮助下获得的 poniter 调用该方法并将其称为:

            SendWndToBack.WindowHandle(new IntPtr(0x00000000000510B2));

那我就把那个窗口放到后面


我张贴了一些例子来说明我的观点。但我的问题是什么时候可以访问内存的其他部分?如果我公开我的变量,其他程序将能够访问它吗?为什么我可以从我自己的程序访问窗口的某些控件?

最佳答案

您混淆了“句柄”和“指针”。 仅仅因为它看起来像一个地址,它可能不是。 Windows 使用大量句柄,即使您没有创建句柄,操作系统也可能会让您处理句柄。 您可以将 Handle 表示为 IntPtr,但如果您实际上将其直接视为指针,您会(可能)崩溃。

关于c# - 什么时候读写内存合法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9170650/

相关文章:

c# - 访问 Adob​​e 打印首选项输出文件夹

c# - free memory 不清除内存块

C# 对 WPF 应用程序进行屏幕截图

c - 试图将函数指针传递给 pthread

ios - Cocos2d 2.0 : I get a higher memory allocation peak setting default pixel format to kCCTexture2DPixelFormat_RGBA4444

c++ - 如何在 C++ 中释放已分配的内存?

c# - 使用 EPPLus 自动调整长字符串

c++ - 数组作为指针传递的不明确重载

pointers - 为什么打印指针与打印取消引用的指针打印相同的内容?

python - 我应该使用类作为全局变量的容器吗