2

Consider, I have two Windows stand alone GUI applications. Whenever I press a command button in the first GUI, other GUI should capture the status of the button and should display ON or OFF in the text box in it. How can I do this with the use of shared memory.

PS: I am using VC++ 2008.

Inquisitive
  • 475
  • 1
  • 5
  • 13
  • by having a look at http://stackoverflow.com/questions/1200998/sharing-memory-between-two-processes-c-windows – mox Mar 17 '13 at 13:53
  • This is like asking "How do I mow my lawn with a chainsaw?" Use the proper tool to get a job done. – Hans Passant Mar 17 '13 at 13:54

1 Answers1

6

Take a look: http://msdn.microsoft.com/en-us/library/windows/desktop/aa366551%28v=vs.85%29.aspx

In process 1 :

CreateFileMapping() : It will create the Shared Memory Block, with the name provided in last parameter, if it is not already present and returns back a handle (you may call it a pointer), if successful.

MapViewOfFile() : It maps (includes) this shared block in the process address space and returns a handle (again u can say a pointer).

With this pointer returned by MapViewOfFile() only you can access that shared block.

In process 2 :

OpenFileMapping() : If the shared memory block is successfully created by CreateFileMapping(), you can use it with the same name (name used to create the shared memory block).

UnmapViewOfFile() : It will unmap (you can remove the shared memory block from that process address space). When you are done using the shared memory (i.e. access, modification etc) call this function .

Closehandle() : finally to detach the shared memory block from process , call this with argument,handle returned by OpenFileMapping() or CreateFileMapping().

Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
  • I did it successfully with the link you provided.Thanks. I have one doubt in it. How we determine the value of BUF_SIZE? (#define BUF_SIZE 256, it is given 256 in the link you provided. ) – Inquisitive Mar 17 '13 at 14:22
  • 1
    I think #define BUF_SIZE 256 is just an example, you can as well take MAX_PATH for that (defined in WinApi32) – Leo Chapiro Mar 17 '13 at 14:25
  • And also I always need to run the exe as administrator. How can I make it available to normal users? – Inquisitive Mar 17 '13 at 14:27
  • 1
    If you are running Vista: This does not work on Microsoft Vista unless the application calling CreateFileMapping is run with Administrative privileges (not the default) under the rules for User Account Control. You will get ACCESS_DENIED (error 5) instead. – Leo Chapiro Mar 17 '13 at 14:29
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26327/discussion-between-inquisitive-and-dude) – Inquisitive Mar 17 '13 at 15:06