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)))

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may