<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Inter-communicate applications with Atoms!

Published on
8,044 Points
3,444 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 48

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

HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Join & Write a Comment

Overview of OneDrive and collaboration.
Watch this simple and effective video tutorial to extract attachments from Outlook 2007 and try this easy method by yourself. No need to go anywhere, just watch the video and export attachments from Outlook in few simple steps. To know more, click h…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month