Sign in to follow this  
sofakng

Can programs access other programs memory? (re: passing pointers using messages)

Recommended Posts

I would consider myself an intermediate-level programmer but I'm a bit confused on how memory access works and how you can pass a pointer using the Windows API SendMessage(). I thought every program has memory reserved just for it. This memory is used for stack and heap allocations (eg. function addresses, variables, objects, etc, etc), and I thought this memory could ONLY be accessed by the owning program. Well, apparantly you can pass a pointer using the Windows API SendMessage() and the receiving program can access the memory that the pointer is pointing to. How does this work? Can somebody tell me what I'm confused about? :( Thanks!!

Share this post


Link to post
Share on other sites
WM_COPYDATA message. But this is about passing contents as a block of data. While you do handle that as a pointer, one process does not get access to memory of another process - only temporary pointer containing the data sent.

http://msdn2.microsoft.com/en-us/library/ms649011.aspx
http://www.qsl.net/dl4yhf/yhf_comm/yhf_comm_info.htm (a random example)

The topic is Interprocess communication, and there's many ways to achieve it: Pipes, Sockets, Shared memory, files, and I'm probably forgetting a few. They depend heavily on OS used.

Generally yes, one process cannot access another process' memory. So if you do need reliable and efficient interprocess communication, I'd look into pipes.

Share this post


Link to post
Share on other sites
Thanks for the reply...

I'm not trying to create a process to do this; I'm trying to understand how one already exists.

Now that I think of it, the main process is an EXE which loads DLL plug-ins. The plug-ins are the ones sending the message to the main process.

Since the EXE loads the DLLs does this mean they have access to each others memory and thus a pointer would be valid to send via SendMessage?

Share this post


Link to post
Share on other sites
A dynamic link library a module that contains code to be run from within an already loaded process. The process will load the dynamic library into its own memory space. Once this happens, the process (EXE) and the loaded library (DLL) share the same memory space.

Share this post


Link to post
Share on other sites
Quote:
Original post by Deception666
A dynamic link library a module that contains code to be run from within an already loaded process. The process will load the dynamic library into its own memory space. Once this happens, the process (EXE) and the loaded library (DLL) share the same memory space.

This is true by default, but the linker can be told to create shared PE segments. In this way, the DLL can have pages of memory mapped to the same physical addresses as its other instances, so processes may communicate without the need for any kernel-driven conduits (named pipes, semaphores and the likes).

Nobody has mentioned ReadProcessMemory (and WriteProcessMemory yet.

Admiral

Share this post


Link to post
Share on other sites
If you try this experiment (passing a pointer to some data to another process via a windows message), you will find that it only works for certain types of messages and the value of the pointer you get on the receiving side will mostly likely be different than the value on the sending side (although the data it points to will be the same).

What happens is that normally a process can't access the memory of a different process. The kernel however does not have this limitation so it is free to copy memory between processes all it wants. There are also various ways to explicitely share chunks of memory between processes. So, you send the message, down in the guts of the OS somewhere it copies your data (via the kernel, shared memory section, etc, etc) and gives the other process a pointer to the copy.

Conversely, if you send a message that isn't one of the ones that the OS knows about then the OS won't know how to copy the data your pointer points at - it probably won't even know you're giving it a pointer at all. So it passes the pointer value but not the data and you end up with a pointer that points to garbage.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this