Global Memory

Started by
8 comments, last by LessBread 18 years, 4 months ago
Hello, i was wondering if it possible to store something in a specific memory address, so i can refer to it using another language. So for example, in C++, i have int age int main() { cout<<"How old are you"; cin>>age; } and say in VB i want to use age to do say, Print You are (age) years. So basicaly how can i Make Age in this example be used on this level.
Advertisement
This might not be the answer your looking for, but you could save the data to a temp file that both programs are aware of. One program opens the file and saves the input in it and closes it, the other file opens it and uses the input for its own purposes... Not so sure that any other way is safe, but i could be wrong.
You could use a database but this isn't to point to a memory location. Each application has its own stack and memory. You can't access the memory of another application unless you are making some sort of memory search tool for memory hacking. That is why a database, file or even registry entry would solve this issue for you.
- GDKnight
I don't know how this would work under Windows, but in Linux/Unix you can use a couple different types of shared memory... namely, the shm_open() and mmap() functions.

However, then you run into all sorts of fun synchronization and mutex problems, just like a multithreaded application, so.
-----http://alopex.liLet's Program: http://youtube.com/user/icefox192
It is possible, however the problem lies in trying to tell your VB application the exact location of the memory.

Since you mentioned VB, I'll assume that you're looking for a Windows solution, in which case, you can look into the GlobalAlloc() function, as well as the functions related to it.

Once you have allocated global memory, however, you'll have to use SendMessage() to tell your VB application the resulting HGLOBAL handle.

On top of this, you must also think about thread safety, as well as ensuring that the memory is always valid before doing something with it.

In either case, I would advise you to only use these functions for learning purposes.
You can use a file-mapping of the system swap file to create effectively a named shared memory between applications. You must also use proper synchronization methods to prevent any data corruption. Now, how you go about doing that in VB I can't say, though I'm guessing you have to import the relevant Win32 functions involved manually.
.
Problem is the Data is updated every second, and i cant really read and write from a file, its a bit too much.
is it possible though, to specify a memory address to store to, and point to it in VB.

That's the magic of the file-mapped swap file. Most of the time, if write caching is enabled, the data is already in physical memory when it is read so no actual file access is necessary.
.
So, what exactly is this file swapped map file thing, is it simply iofstreaming to and from a file?
Here's a Microsoft example of how to share memory between processes: Creating Named Shared Memory. It shouldn't be too difficult to convert to VB. It's very likely that similar examples in VB can be found via google.

There are other ways to pass data between applications. The topic is named Interprocess Communications. You can find the details at that link.

@Wavarian, GlobalAlloc allocates memory from the heap. That memory can't be shared between processes without the use of other mechanisms, such as those I just mentioned. Outside of using these mechanisms, there's no guarantee that an address in one process points to anything at all in a second process. Under windows every process has it's own address space.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement