Archived Boards > WinBatch Script Exchange

Global Atom UDFs - sharing data between applications in memory

(1/3) > >>

Here are a few basic UDFs that allow you to create, find, read and delete shared "Atoms" in memory between applications... see:

The atoms will persist after the application has closed (unless deliberately deleted), but will be gone on a reboot.

For example,
Try making 2 scripts and create an Atom in one with something like:
and run it.

then in the other, use:
Atom = GlobalFindAtom("testing123")

.... and it should find and display the text associated with the atom you made in the first script!
It's a good idea to read the notes in the UDFs and online about Atoms and about subsequent adds and deletes.

--- Code: Winbatch ---;Each call to GlobalAddAtom should have a corresponding call to GlobalDeleteAtom.
;Do not call GlobalDeleteAtom more times than you call GlobalAddAtom, or you may delete the atom while other clients are using it.
;Applications using Dynamic Data Exchange (DDE) should follow the rules on global atom management to prevent leaks and premature deletion.
#DefineFunction  GlobalAddAtom(Name)
    Ret = DllCall(Kernel32,long:"GlobalAddAtomA",lpstr:Name)
    Return Ret

#DefineFunction  GlobalFindAtom(Name)
    Ret = DllCall(Kernel32,long:"GlobalFindAtomA",lpstr:Name)
    Return Ret

#DefineFunction  GlobalDeleteAtom(Atom)
    Ret = DllCall(Kernel32,long:"GlobalDeleteAtom",long:Atom);(Always returns 0)
    ;To determine whether the function has failed, call SetLastError with ERROR_SUCCESS before calling GlobalDeleteAtom, then call GetLastError.
    ;If the last error code is still ERROR_SUCCESS, GlobalDeleteAtom has succeeded.
    Return Ret ; Always 0

#DefineFunction  GlobalGetAtomName(Atom)
    Name = BinaryAlloc(256)
    Len = DllCall(Kernel32,long:"GlobalGetAtomNameA",long:Atom,lpbinary:Name,long:256)
    Ret = BinaryPeekStr(Name,0,Len)
    Return Ret

Looks interesting and, potentially, very useful.  Thanks.


So atoms are like little variables that have no contents -- the only information carried is the fact that it exists, or does not exist.  Do I have that right?

EDIT: I found this reference:

I guess that's about right... umm.. that's the same link I had in my original post, but no worries... it's got a lot of info there and definitely should be read if you are going to use Atoms in your projects.

Basically as far as I understand it...
There is a global (for each logon session) store of Atoms (a hash table) and it used by underlying system routines to store, swap and manage info between threads etc. ... it is available to all applications running in that session.. it is not private.
It is a finite resource though and you shouldn't go storing thousands of Atoms in there.
There are String (max 255 characters in length) and Numeric Atoms.... read the info on the use of numeric atom numbers and the string representations of them eg: "#1234" - a bit confusing, and I prefer sticking to string Atoms myself.

Also note that each Atom reference can be incremented or decremented by subsequent calls to Add or Delete a specific Atom, so for example, if you Add it twice, you need to Delete it twice before it's gone.
I have used the ANSI functions, but there are Wide (Unicode) versions too - easy to change the UDFs if you want to.

Apart from the Global Atoms table, each application also has its own private one that is independent and available only to that application... you could easily make your own UDFs for those functions based on these.

Again, read the MS docs.


<< Again, read the MS docs >>
Will do.  thanks!


[0] Message Index

[#] Next page

Go to full version