Microsoft KB Archive/162622

= How To Pass a NULL String to a Windows API from Visual Basic =

Article ID: 162622

Article Last Modified on 7/14/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Control Creation Edition
 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Enterprise Edition

-



This article was previously published under Q162622



SUMMARY
This article describes how to pass NULL as a parameter to any Windows API function that requires a pointer to a string from a Visual Basic 4.0 or higher application.



MORE INFORMATION
Many Windows APIs have pointers to strings (for example, LPSTR or LPCSTR) in their parameter lists. The documentation often indicates that special processing occurs if a NULL is passed (instead of a pointer to a string). With earlier versions of Visual Basic, you could accomplish this by using 0& for the parameter. However, Visual Basic versions 4.0 and higher include a special constant, vbNullString, that you can use when you need to pass NULL to a Windows API.

For example, here's the declaration for the Win32 function FindWindow that accepts two pointers to strings:

HWND FindWindow(         LPCTSTR lpClassName,   // pointer to class name          LPCTSTR lpWindowName   // pointer to window name          );

If NULL is passed as the second parameter, FindWindow locates any window of the specified class name. In a C or C++ program, the call might look like this:

hRet = FindWindow("MyWindowClass", NULL);

If you make the same call from Visual Basic, it looks like this:

hRet = FindWindow("MyWindowClass", vbNullString)

Note that using vbNullString is equivalent to passing NULL as the parameter. This is not the same as passing an empty string ("").

