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
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
Author
Commented: