Microsoft KB Archive/129388

= INFO: How Null String Pointers Behave in Visual Basic 4.0 =

Article ID: 129388

Article Last Modified on 12/9/2003

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q129388



SUMMARY
Visual Basic version 4.0 recognizes two very different kinds of strings that look the same but act differently. One is a Null pointer string, the other is an empty string. You might code them in a public module as follows: Public sNull As String Public Const sEmpty = "" If you look at these two strings in the Watch window, they look exactly the same; both displayed as "". You can use them in almost the same contexts, but internally they are very different.



MORE INFORMATION
Internally, sNull is a null pointer. It does not point to any memory location and has a value of zero. In C, you would code it as: const char *sNull = NULL; Internally, sEmpty is a pointer to an empty string. It is a valid pointer to some memory location. In C, you would code it as: const char sEmpty[] = ""; All Visual Basic version 4.0 variables are set to zero (0) until initialized. In previous versions of Visual Basic, uninitialized variable-length strings were automatically set to an empty string (""). Therefore, for compatibility with previous versions, you might think that you must initialize string variables to empty strings. However, Visual Basic version 4.0 strings are in the BSTR format where a null pointer is defined to behave exactly as an empty string does. Therefore, in Visual Basic version 4.0, you can leave the initial zero value of an uninitialized strings alone because it will behave as if it were an empty string.

This means that sNull can now be passed to any Windows API function that takes a Null pointer. This is something that was not possible in previous versions of Visual Basic. For example, it can be passed to FindWindow, which gets the handle of a window, given either its class name or its title, or both. However for this to work, sNull must be passed ByVal.

In general, for FindWindow (or any other Windows API) to work, the Declare statement must be written to pass the string ByVal As String or ByVal As Any. If the string is passed by reference, a pointer to a BSTR would then be passed, which is nothing but a pointer to a pointer to char. This will not work as Windows APIs expect strings that are pointers to char.

NOTE: In Visual Basic version 4.0, sNull might also be expected to be equivalent to: Public Const sNull As String = 0& However, Visual Basic does automatic numeric conversion on this and converts it to zero (0), which is neither an empty string nor a null string pointer.

Step-by-Step Example
 Start a new project in Visual Basic. Form1 is created by default.  Add the following code to the General Declarations section of Form1: Const sEmpty = "" Dim sNull As String

Private Declare Function FindWindow Lib "user32" Alias _ "FindWindowA" (ByVal lpClassName As Any, ByVal _        lpWindowName As Any) As Long   In the Form_Click event for Form1, add the following code: Shell "Calc.exe", 1 DoEvents x& = FindWindow(sNull, "Calculator") 'x& = FindWindow(sEmpty, "Calculator") Debug.Print x&                        Press the F5 key to run the program. Click Form1, and view the Debug Window. A non-zero value will be printed. This is the handle of the Calculator program's Window.  As an experiment, change the following line from executed code into a comment: x& = FindWindow(sNull, "Calculator") And change the following line from a comment into executed code: 'x& = FindWindow(sEmpty, "Calculator") Then run the program again. You will see a value of zero (0) printed in the Debug Window, indicating that FindWindow failed. This happens because sEmpty is not a null string pointer. 

Keywords: kbinfo kbprogramming KB129388

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.