Ton*_*Nam 6 c# memory pointers
如果我错了,请纠正我.我问这个问题是为了澄清我的一些想法.
今天在学校我了解到,当一个进程(程序)执行时,操作系统会给它一个内存空间.例如,这两个程序:
PROGRAM1:
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.");
}
}
Run Code Online (Sandbox Code Playgroud)
计划2
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);
}
}
Run Code Online (Sandbox Code Playgroud)
所以我理解在程序2中我会收到错误.我将访问受限制的内存.到目前为止,我学到的东西是有道理的.
好的,知道这里的事情没有意义.
有一个非常好的程序叫AutoIt,用于自动执行任务.例如,它可以发送鼠标点击,移动鼠标,发送击键等.
无论如何autoit附带一个名为AutoIt Window Info的程序,该程序使您可以获取窗口控件的句柄(指针).例如,我可以通过将取景器工具拖动到我希望获取信息的控件来查看窗口控件的句柄:

在这张图片中我将finder工具拖动到计算器的输入控件.我也可以将它拖到按钮7上.
因此,如果您在图片中看到我现在拥有该控件的地址.然后我就可以从我的程序中访问它!
另一个例子,说明如何访问不属于我的程序的内存
步骤1)获取具有自动窗口信息的任何窗口的指针
步骤2)在我的计算机中指针是:

这是谷歌浏览器的窗口我输入这个问题的地方.
这个类将向后发送一个窗口:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后,如果我在自动调用的帮助下使用poniter调用该方法,并将其命名为:
SendWndToBack.WindowHandle(new IntPtr(0x00000000000510B2));
Run Code Online (Sandbox Code Playgroud)
然后我会将那个窗口发送到后面
我发布了一些例子来说明我的观点.但我的问题是你什么时候可以访问内存的其他部分?如果我将变量公开,其他程序将能够访问它吗?为什么我可以从我自己的程序中访问某些窗口控件?
你在混淆"Handles"和"Pointers".仅仅因为它看起来像一个地址,它可能不是.Windows使用了很多句柄,即使您没有创建它们,操作系统也可以让您处理.您可以将Handle表示为IntPtr,但是您实际上将其直接视为指针,您可能(可能)崩溃.
| 归档时间: |
|
| 查看次数: |
266 次 |
| 最近记录: |