<

Inter-communicate applications with Atoms!

Published on
7,922 Points
3,322 Views
1 Endorsement
Last Modified:
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 ""
        Else
            Return sBuffer.Substring(0, iRet)
        End If
    End Function
End Class

Open in new window

1
Comment
Author:Luis Pérez
2 Comments
 
LVL 47

Expert Comment

by:aikimark
Question: If using atoms in a VB.Net application, does that part of the application go out into unmanaged code since it directly calls a Windows API?
0
 
LVL 25

Author Comment

by:Luis Pérez
Mmmm... it's a peculiar question. Well, Windows APIs are in fact unmanaged code from .NET's point of view, but here we are encapsulating those functionality into .NET classes (that, in fact, is what the entire Framework does). Really I don't know what is the right answer here. I think it is a matter for purists.
0

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Join & Write a Comment

In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
This is Part-2 of Learning to use the Power of Mailwasher Pro so if you haven't watched Part-1 yet, I urge you to do so before watching this video. Click this link to watch Part-1 (https://www.experts-exchange.com/videos/56638/Learn-to-use-the-POWER…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month