Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB4,VB5,VB6
Expertise: Intermediate
Oct 23, 1999

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 Long
Private Const WM_GETTEXT = &HD

Dim buffer As String, i As Long, winText As String
buffer = Space$(512)
SendMessage hWnd, WM_GETTEXT, Len(buffer), ByVal buffer
i = 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" variable
winText = String$(512, 0)      ' fill with null chars
SendMessage hWnd, WM_GETTEXT, Len(winText), ByVal winText
winText = 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 Long

Dim sysDir As String
sysDir = 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 spaces
length = 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 * 260
sysDir = Left$(buffer, GetSystemDirectory(buffer, Len(buffer)))
Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date