【发布时间】:2012-02-28 13:12:03
【问题描述】:
如果我错了,请纠正我。我问这个问题是为了澄清我的一些想法。
今天在学校我了解到,当一个进程(程序)执行时,操作系统会给它一个内存空间。以这两个程序为例:
程序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.");
}
}
方案 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);
}
}
所以我知道在程序 2 中我会得到一个错误。我将访问受限内存。到目前为止,我所学到的都是有道理的。
好的,知道这里是没有意义的地方。
有一个非常棒的程序叫做AutoIt 用于自动执行任务。例如它可以发送鼠标点击、移动鼠标、发送按键等。
无论如何,autoit 都带有一个名为 AutoIt Window Info 的程序,该程序使您能够获取窗口上控件的句柄(指针)。例如,我可以通过将查找工具拖动到我希望获取信息的控件来查看窗口控件的句柄:
int this picture 我将查找工具拖到计算器的输入控件中。例如,我也可以将其拖到按钮 7。
因此,如果您在图片中看到,我现在有了该控件的地址。然后我就可以从我的程序中访问它了!!
另一个例子说明如何访问不属于我的程序的内存
步骤 1) 使用 autoit 窗口信息获取任意窗口的指针
第 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);
}
}
然后如果我用我刚刚在 autoit 的帮助下得到的指针调用该方法并将其称为:
SendWndToBack.WindowHandle(new IntPtr(0x00000000000510B2));
那我把那个窗口放到后面
我发布一些例子来说明我的观点。但我的问题是你什么时候可以访问内存的其他部分?如果我将我的变量公开,其他程序将能够访问它吗?为什么我可以从我自己的程序中访问某些窗口控件?
【问题讨论】: