How make a variable available in different exe modules? Could anyone advise a way how two exe-modules (programs) can interchange of their internal values? For example: Prog1.exe has TextBuffer1. How to read value from TextBuffer1 and write to TextBuffer1 of Prog2.exe? In other words to make the TextBuffer1 PUBLIC or GLOBAL VARIABLE… ? (to use the same memory address in both programs) Thanks
Take a look at the tutorial "Memory mapped files: Sharing data between Instances" in the tutorial section at Iczelion's site. An extra note:
In other words to make the TextBuffer1 PUBLIC or GLOBAL VARIABLE… ? (to use the same memory address in both programs)Making a symbol GLOBAL or PUBLIC can easily be done in masm, but it's not the GLOBAL you mean. When you define a symbol public in masm, it can be shared among other source files or object files, but that's different as sharing between programs. Additionally, you can define a variable in 2 or more programs at the same memory address, but in win32, programs do not share memory (well of course they do physically but not in the virtual memory). So that wouldn't work either. Thomas This message was edited by Thomas, on 4/21/2001 3:08:31 PM This message was edited by Thomas, on 4/21/2001 3:09:23 PM
You can also share data between apps in a special shared segment. This is done by creating a segment in your app:
Now to link the exe include
SHARE32 SEGMENT PARA USE32 TextBuffer1 BYTE MAX_PATH DUP(?) SHARE32 ENDS
in the command line. This now means that if _any_ instance of your app modifys the memory under TextBuffer1, the change is reflected in the address space of all instances of your app.
Thats quite interesting to learn... few questions tho, just digging a little deeper: 1) You said any instance of your program, is this the same acutaly program, meaning FUNCT A and FUNCT B must be coded in one program, and using this method of scharing data from A->B of separate instances?? (Kinda convoluted with my example sorry). 2) If not, can i declair this segment and link it in separate programs?? Ie, FunctA in program A, Function B in program B, and a commonly named segment between them? (this would be quite usefull). 3) How is the segment cleaned up? Does it retain indefinitly once defined? Or like Com's does it keep an list of active instances using this segment? Thanx, you've peaked my interest here... NaN
Special THANKS to George, it works perfectly!
a) An instance is the same executable file, loaded from the same image on disk multible times, but running simutainously in its own address space. b) You can declare a shared segment, but for it two be shared between to prgrams, they must be the different instances of the same program (ie, the same exe of disk). You can achive sharing between two programs using a named FileMapping object, with an invalid handle (INVALID_HANDLE_VALUE), and the share flag set. Or you can create a dll with a function to get a pointer the the shared segment memory, and this dll can be loaded in any instance of the program, and will still get the sharing effect with a shared segment. c) The segment is cleaned up normaly i would guess, except windows would have to maintain a list so that the phyiscal memory behind the segment is not freed while there are still programs using it. Please note this just my understanding of these shared segments and instances, through all my practice it hasn't been wrong (yet)
Thanx.. you pretty well cleared up my interest... and suggested a bit more :P (Overlooked the use of the .dll, makes sence now, just didnt think about it..) NaN