Several Questions, main one is about protected memory

Here are some of my questions:

1. How do you get around protected memory? As in, access another program's memory. Like how a trainer for a game would. I know how to set a pointer to that address, but the OS keeps it from being able to read and write to it without crashing my program.

2.(relates to #1) I have the offsets for the values in a game's dll. Now, how would I find the memory address of the dll. I got the offsets from a memory reading program, but I would like to be able to access this with my own program.

3. How would you go about reading pixels from the screen? Mainly, a DirectX program.

4. How can you send out button presses, as if you were using a controller, through DirectInput? Is it done in a similar way as sending messages out to windows?

Sorry if I asked any stupid questions, I just started learning C programming about a month ago. I think I got the basics down, and I'm working on learning Windows API now. # 3 and 4, I don't know much about. And I searched...I don't think these questions were asked before...at least questions #1 and 2 anyway.

...not really a trainer...that was more of an example. I'm trying to make a program...like an AI, that you can play against in the game. Only way I can think of it being able to know what's going on is to read the memory from the game. It would be like the second player.

>> 1. How do you get around protected memory? As in, access another program's memory. Like how a trainer for a game would. I know how to set a pointer to that address, but the OS keeps it from being able to read and write to it without crashing my program. <<

Each process has a seperate address space. You can read and write from another process' memory using the ReadProcessMemory and WriteProcessMemory functions. Examples are available on the board and on the web.

>> 2.(relates to #1) I have the offsets for the values in a game's dll. Now, how would I find the memory address of the dll. I got the offsets from a memory reading program, but I would like to be able to access this with my own program. <<

You can use the EnumProcessModules function or CreateToolhelp32Snapshot with TH32CS_SNAPMODULE. Note that the HMODULE or HINSTANCE of a DLL is its base address. Many DLL/EXE combinations will use constant base addresses so you may be able to skip this step. A tool such as Dependency Walker will tell you the preferred and actual base addresses of the DLLs loaded into a process.

>> 3. How would you go about reading pixels from the screen? Mainly, a DirectX program. <<