When is it read and written into memory?

Please correct me if I am wrong. I am asking this question to clarify some ideas that I have.

Today at school, I learned that when a process (program) is executed, operating systems give it space in memory. So take, for example, two programs:

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."); } } 

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 1 object Console.WriteLine("addrees of {0} points to value {1} ", (int)&pointer_1, *pointer_1); } } 

So, I understand that in program 2 I will get an error. I am accessing limited memory. So far, what I have learned makes sense.

To know that there is no point.

There is a very good program called AutoIt used to automate tasks. For example, it can send mouse clicks, move the mouse, send strokes of keys, etc.

In any case, autoit comes with a program called AutoIt Window Info, where this program allows you to get the handles (pointers) of controls on windows. For example, I could see the handle of the control window, overlaying the search tool on the control that I want to get:

enter image description here

int this picture I drag the search tool into the calculator input control. For example, I could drag it to button 7.

So, if you see in the picture, I now have the address of this control. Then I could access it from my program!


Another example of how you can access memory that does not belong to my program

Step 1) Get a pointer to any window with information about the autoit window

Step 2) On my computer, this pointer:

enter image description here

This is the google chrome window where I am typing this question.

This class will send the window back:

  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); } } 

and then if I call the method using poniter, which I just got with autoit, and name it as:

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

then I will send this window to the background


I am publishing a few examples to emphasize my point. But my question is when do you allow access to other parts of the memory? If I make my variable public, can other programs access it? Why can I access some window controls from my own program?

+6
source share
1 answer

You are misleading "Pens" and "Pointers." Just because it looks like an address may not be. Windows uses a lot of pens, and the OS can let you do something even if you didn't create them. You can represent Handle as IntPtr, but you should actually treat it directly as a pointer, you are likely to crash.

+5
source

Source: https://habr.com/ru/post/907816/


All Articles