Global access to allocated memory from 2 different programs

Just starting out? Need help? Post your questions and find answers here.
fastbit66
New User
New User
Posts: 9
Joined: Tue Jan 17, 2012 2:05 pm

Global access to allocated memory from 2 different programs

Post by fastbit66 »

Hello PB Lovers!

I have a question regarding accessing allocated memory from 2 different programs - maybe its a silly quation so please excuse me:

If I allocate a memory block on the HEAP with AllocateMemory() Function and I save the adrees in a *MemID variable and I let the program wait
so that this memory will not be freed - is it possible to access this memory an its contents from a second launched PB exe?

Example:

exe1:
<code>
*MemoryID = AllocateMemory(5000)
If *MemoryID
Debug "Startadresse des 5000 Byte Speicherbereichs ist:"
Debug *MemoryID
PokeS(*MemoryID, "Wir speichern diesen String im Speicherbereich")

wait until Keypressed() // pseudo code program - shall wait until a key is pressed

FreeMemory(*MemoryID) ; wird am Ende des Programms auch automatisch erledigt
Else
Debug "Konnte den angeforderten Speicher nicht reservieren!"
EndIf
</code>


Let's assume the *MemoryID is 230440

Now I let this program run an launch a second exe:

exe2:
<code>
*MemoryID = 230440
Text.s = peeks(*MemoryID)
Debug Text
</code>

I don't see the TExt - maybe nothiung or maybe weird characters.

Is this possible at all or isn't it possible beacause of mem protection from the OS?

Thanx a lot for your time!

Greets
andi
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Global access to allocated memory from 2 different progr

Post by IdeasVacuum »

If you are on Windows, you can use API MapViewOfFile_() for this task.
http://www.purebasic.fr/english/viewtopic.php?p=109501
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Global access to allocated memory from 2 different progr

Post by Thorium »

Just for basic understanding of memory managment in modern OS:

Every process has it's very own memory address space, which is called virtual memory. Not to be confused with the swap file which is also called virtual memory on windows.
That means this address space is not representing the physical memory. It's a memory only available to the process and the OS maps physical memory swap file and memory mapped files to it. So any address in the virtual memory address space could have data that is actualy stored in physical memory or in the swap file or any other file or even memory on another device like the graphics card.

Since every process has it's own address space you can't directly access a address from another process. If you do so, you will access the address in the local process and there will be stored something else than in the other process or it could even be not allocated.

To access anothers process virtual address space there are some API functions like IdeasVacuum allready posted. With MapViewOfFile you can tell the OS that you want to share a portion of your address space with another process. Thats great for communication and data transfer between processes.

If the process in question does not use MapViewOfFile (it has to be used in both processes) you can use the API's ReadProcessMemory and WriteProcessMemory.
fastbit66
New User
New User
Posts: 9
Joined: Tue Jan 17, 2012 2:05 pm

Re: Global access to allocated memory from 2 different progr

Post by fastbit66 »

@IdeasVacuum
@Thorium

Thanx a lot for your time and precise explanation!
That clarifies things up!

So I will try out your suggestions!

Best regards!
Andi
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: Global access to allocated memory from 2 different progr

Post by said »

fastbit66 wrote: ... is it possible to access this memory an its contents from a second launched PB exe?
Since you are talking about PB exe and in case one of the program is calling the other, then you may want to consult RunProgram() & Co as it offers good and easy communication between the two programs, no api needed and is crossplatfrom :)
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Re: Global access to allocated memory from 2 different progr

Post by sec »

One other idea: using nanomsg :D
Post Reply