Inter-communicate applications with Atoms!

Published on
8,399 Points
1 Endorsement
Last Modified:
Luis Pérez
Software Architect in .Net C#, VB & ASP. Lover of Star Wars, MCU and Rock music. My greatest achievement in life: my 2 daughters.
Using "atoms" is an old and good technique than I started to use a long time ago (probably, with vb4 or vb5...). It's an excellent way to inter-communicate separate processes without the need to write or read physical files on disk.  You can learn a lot about atoms, of course, in MSDN: http://msdn.microsoft.com/en-us/library/ms648708(VS.85).aspx

An atom table is a system-defined table that holds a series of strings and, for each one of them, an associated 16-bit integer (in vb.net, Short or Int16 data type). So, think in them as a collection of strings, each string with a unique integer ID. Each application can create a local atom table, but there is also a global atom table that can be accesed system-wide from within any application, so inter-process communication is so easy this way.  This means that one application can create values in this global table and other application in a complete separate execution process can read them (or delete them).

One typical scenario of this approach is to communicate from one process to another, for example, when a job has finished, writing a value in this global table. The second process keeps monitoring the global atom table and, when it finds the specific value, it knows that the job is done.

The best of all is that writing, reading and deleting values in the global atom table is extremely easy!!  With the use of only 4 Win32 APIs we'll do all the work.

Well, let's go to the code.  The way that atoms work is that we create new string values and the functions automatically assign corresponding IDs.  Then, we can search the existence in the atom table for both the integer ID value or for the string value.  Finally, when we don't need an atom anymore, we must delete it in order to free assigned memory.

What I've done here is a Vb.net class that declares as private the Win32 APIs needed for the Atom magic and provides static and easy to use methods with the basic functionality to create, find (by string or by ID) and delete atoms.

Hope that helps in your applications.
Public Class AtomManagement
    'The API declarations to do the magic
    Private Declare Function GlobalAddAtom Lib "kernel32.dll" Alias "GlobalAddAtomA" (ByVal lpString As String) As Short
    Private Declare Function GlobalFindAtom Lib "kernel32.dll" Alias "GlobalFindAtomA" (ByVal lpString As String) As Short
    Private Declare Function GlobalDeleteAtom Lib "kernel32.dll" (ByVal nAtom As Short) As Short
    Private Declare Function GlobalGetAtomName Lib "kernel32.dll" Alias "GlobalGetAtomNameA" (ByVal nAtom As Short, ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
    'Create a new atom and return the ID associated. If failed, returns 0.
    Public Shared Function AddAtom(ByVal TheString As String) As Short
        'GlobalAddAtom function requires the string is null-terminated
        TheString &= Chr(0)
        Return GlobalAddAtom(TheString)
    End Function
    'Find an existing atom. If found, returns associated ID. If not found, returns 0.
    Public Shared Function FindAtom(ByVal TheString As String) As Short
        'GlobalFindAtom also requires null-termination of the string
        TheString &= Chr(0)
        Return GlobalFindAtom(TheString)
    End Function
    'Delete an existing atom. This frees a little memory.
    Public Shared Sub DeleteAtom(ByVal TheAtom As Short)
        Call GlobalDeleteAtom(TheAtom)
    End Sub
    'Get an atom string given associated atom ID
    Public Shared Function GetAtomName(ByVal AtomID As Short) As String
        Dim sBuffer As String = Space(255) 'As 255 is the maximum string length for an atom
        Dim iLength As Integer = sBuffer.Length
        Dim iRet As Integer = GlobalGetAtomName(AtomID, sBuffer, iLength)
        If iRet = 0 Then
            Return ""
            Return sBuffer.Substring(0, iRet)
        End If
    End Function
End Class

Open in new window

Author:Luis Pérez
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free