devxlogo

Extract null-delimited strings

Extract null-delimited strings

Most API function that return a string require that you pass a buffer where they can place the result as a null-terminated ANSI string (a.k.a. ASCIIZ string). The calling code must then extract the string by taking all the characters up to the first Chr$(0) character, if there is one. For example, this is the code that returns the caption of any window:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _    hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _    lParam As Any) As LongPrivate Const WM_GETTEXT = &HDDim buffer As String, i As Long, winText As Stringbuffer = Space$(512)SendMessage hWnd, WM_GETTEXT, Len(buffer), ByVal bufferi = Instr(buffer, vbNullChar)If i Then    ' trim extra chars    winText = Left$(winText, i - 1)Else    ' an error has occurred    winText = ""End If

Here is a more concise version that ensures that the InStr function doesn’t fail to find a null character, and eliminates the need for the i and buffer local variables:

Dim winText As String          ' no need for the "i" variablewinText = String$(512, 0)      ' fill with null charsSendMessage hWnd, WM_GETTEXT, Len(winText), ByVal winTextwinText = Left$(winText, Instr(winText & vbNullChar, vbNullChar) - 1)

Many API functions don’t even require that you search for the null-character, because they return the actual number of valid characters. For example, it seems that this is the best way to determine the Windows’ SYSTEM directory:

Private Declare Function GetSystemDirectory Lib "kernel32" Alias _    "GetSystemDirectoryA" (ByVal lpBuffer As String, _    ByVal nSize As Long) As LongDim sysDir As StringsysDir = Space$(260)GetSystemDirectory sysDir, Len(sysDir)sysDir = Left$(sysDir, InStr(sysDir & vbNullChar, vbNullChar) - 1)

However, the SDK docs tell that the GetSystemDirectory function returns the number of the characters in the result. This lets you get rid of the InStr function. And you can also get rid of the Space$ function by re-inserting a fixed-length local variable:

Dim sysDir As String, length As Long, buffer As String * 260' no need to fill the buffer with spaceslength = GetSystemDirectory(buffer, Len(buffer))sysDir = Left$(buffer, length)

Finally, here’s the shortest version possible, that doesn’t even need the length local variable and pass the result of the API function directly to the 2nd argument of the Left function. I don’t claim that this is a good programming style, because it makes the code rather obscure even to those that know how the API works, and also because it might not work in other versions of VB (I tested it only under VB6). However, it’s good to know that you can sometimes write extremely concise code even in VB:

Dim sysDir As String, buffer As String * 260sysDir = Left$(buffer, GetSystemDirectory(buffer, Len(buffer)))

devx-admin

Share the Post: