Chapter 27: Using the Windows Application Programming Interface
953
Although not strictly necessary, the GetCommandLine wrapper trims leading and trailing spaces
from the string returned by the apiGetCommandLine declaration. Very often, API functions
return strings that are padded with extra characters, and the extra characters should be trimmed
off of the wrapper’s return value.
In Chapter27.accdb, GetCommandLine is used to retrieve the path to MSAccess.exe.
GetWindowsDirectoryA
GetWindowsDirectoryA retrieves the path to Windows and stores it in a string buffer.
Declare Function apiGetWindowsDirectory _
Lib “Kernel32” _
Alias “GetWindowsDirectoryA”( _
ByVal lpszReturnBuffer As String, _
ByVal lpszBuffSize As Long) As Long
The return value is the length of the string copied into the buffer. If the buffer isn’t long enough,
the return value is the length required. But if the function fails, GetWindowsDirectoryA
returns zero.
Here’s a function wrapper that uses the GetWindowsDirectoryA API function:
Function GetWindowsDirectory() As String
Dim WinDir As String * 255
Dim WinDirSize As Long
Dim RetVal As Long
WinDirSize = Len(WinDir)
RetVal = apiGetWindowsDirectory(WinDir, WinDirSize)
If RetVal > 0 Then
GetWindowsDirectory = Left$(WinDir, RetVal)
Else
GetWindowsDirectory = vbNullString
End If
End Function
In the GetWindowsDirectory wrapper, notice that the API function’s return value (RetVal) is
used as an argument to the Left$ function to extract the Windows folder name from the WinDir
string. If RetVal is zero, an empty string (vbNullString) is returned instead.
GetTempPathA
The GetTempPathA function retrieves the path to the directory where temp files are stored and
places it in a string buffer.
Declare Function apiGetTempPath _
Lib “Kernel32” _
Alias “GetTempPathA”( _
ByVal BufferSize As Long, _
ByVal lpszReturnBuffer As String) As Long