Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Extract null-delimited strings

Total Hit ( 3758)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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:

Click here to copy the following block
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:

Click here to copy the following block
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:

Click here to copy the following block
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:

Click here to copy the following block
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:

Click here to copy the following block
Dim sysDir As String, buffer As String * 260
sysDir = Left$(buffer, GetSystemDirectory(buffer, Len(buffer)))


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.