Declare Function GetClipCursor Lib "user32.dll" (lprc As RECT) As Long
lprc
. You can safely ignore the value returned.lprc | Receives the upper-left and lower-right corners of the confinement rectange. |
'Print the corners of the confinement rectangle.
Dim r As RECT
x = GetClipCursor(r)
Form1.Print r.Left; r.Top 'upper-left (x,y) pair
Form1.Print r.Right; r.Bottom 'lower-right (x,y) pair
Declare Function GetComputerName Lib "kernel32.dll" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
lpBuffer | A fixed-length string large enough to hold the returned name. It recieves the computer name. The name is followed by vbNullChar .
|
nSize | The length in characters of lpBuffer .
|
'Read the computer's name
Dim compname As String * 255, x As Long
x = GetComputerName(compname, 255)
compname = Trim(compname)
compname = Left(compname, Len(compname) - 1)
Debug.Print compname
Declare Function GetCursorPos Lib "user32.dll" (ByVal lpPoint As POINTAPI) As Long
lpPoint
, and the returned value can safely be ignored.lpPoint | Receives the x and y coordinates of the mouse. |
'Display the coordinates of the mouse
Dim coord As POINTAPI
x = GetCursorPos(coord)
Debug.Print coord.x; coord.y
Declare Function GetDC Lib "user32.dll" (ByVal hWnd As Long) As Long
hWnd | The handle of the object. |
'The two statements below will return the same value
Debug.Print Form1.hDC
x = GetDC(Form1.hWnd)
Debug.Print x
Declare Function GetDesktopWindow Lib "user32.dll" () As Long
desktophWnd = GetDesktopWindow()
Declare Function GetDiskFreeSpace Lib "kernel32.dll" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters As Long) As Long
lpSectorsPerCluster * lpBytesPerSector
. You get the drive's free space by multiplying the cluster size by lpNumberOfFreeClusters
, and the drive's total space by multiplying the cluster size by lpTotalNumberOfClusters
. You can safely ignore the value returned.lpRootPathName | The root directory of the drive to get information on, such as c:\ or a:\. |
lpSectorsPerCluster | Receives the number of sectors in a cluster on the disk. |
lpBytesPerSector | Receives the number of bytes in a sector on the disk. |
lpNumberOfFreeClusters | Receives the number of unused, empty clusters on the disk. |
lpTotalNumberOfClusters | Receives the total number of clusters, used and unused, on the disk. |
'Find the free space on the hard drive C:
x = GetDriveFreeSpace("c:\", SectorsPerCluster, BytesPerSector, FreeClusters, TotalClusters)
Form1.Print "Free space on C:"; SectorsPerCluster * BytesPerSector * FreeClusters; "bytes"
Declare Function GetDriveType Lib "kernel32.dll" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
nDrive | The root directory of the drive to check, such as c:\ .
|
'Check to see what type of drive C: is
x = GetDriveType("c:\")
'x = DRIVE_FIXED, or hard drive
Declare Function GetFileAttributes Lib "kernel32.dll" Alias "GetFileAttributesA& (ByVal lpFileName As String) As Long
lpFileName | The full name of the directory or file to check the attributes of. This means that you must include the path. |
'Check the attributes of c:\windows\sol.exe (Solitaire)
attribs = GetFileAttributes("C:\Windows\sol.exe")
If (attribs And FILE_ATTRIBUTES_ARCHIVE) <> 0 Then Form1.Print "Archive "
If (attribs And FILE_ATTRIBUTES_READONLY) <> 0 Then Form1.Print "Read-only "
'and so on....
Declare Sub GetLocalTime Lib "kernel32.dll" (lpSystemTime As SYSTEMTIME)
lpSystemTime | Receives the computer's date and time in absolute format. |
'Print the date in mm-dd-yyyy format.
Dim loctime As SYSTEMTIME
GetLocalTime loctime
Debug.Print loctime.wMonth; "-"; loctime.wDay; "-"; loctime.wYear
Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOPENFILENAME As OPENFILENAME) As Long
pOPENFILENAME | Holds the parameters needed to open the dialog box. Also holds the returned filename(s). |
'Call the Open File dialog box and read the filename
Dim file As OPENFILENAME, x As Long, filename As String
file.hwndOwner = Form1.hWnd 'Calling form's handle
file.lpstrTitle = "Open File" 'Title bar
'Set the File Type drop-box values
file.lpstrFilter = "Text Files" & vbNullChar & "*.txt" & vbNullChar & vbNullChar
file.lpstrFile = Space(255) 'Path and file buffer
file.nMaxFile = 255 'Length of buffer
file.lpstrFileTitle = Space(255) 'File name buffer
file.nMaxFileTitle = 255 'Length of buffer
'Only existing files, and hide read-only check box
file.flags = OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY
file.lStructSize = Len(file) 'Variable's size
x = GetOpenFileName(file)
If x = 0 Then Exit Sub 'Abort if user hit Cancel
'Extract the filename
temp = Trim(file.lpstrFile)
filename = Left(temp, Len(temp) - 1)
Declare Function GetParent Lib "user32.dll" (ByVal hwnd As Long) As Long
hwnd | The handle of the object you want to find the parent of. |
'This will work if button Command1 sits "on" form Form1
Form1.Print GetParent(Command1.hWnd)
Form1.Print Form1.hWnd 'should be the same value
Declare Function GetPrivateProfileInt Lib "kernel32.dll" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
lpApplicationName | The header of the INI file section the value is in. The heading is the string in brackets at the top of a section of the file. Do not put the brackets into this string. |
lpKeyName | The name of the value to read. This is the string on the left side of the = sign in the INI file. |
nDefault | If the function fails to read a valid value, this is the value returned. Make it something that wouldn't be returned if successful, such as -1. |
lpFileName | The filename of the INI file to read from. |
'Read the value for "type" under the [keyboard] section of SYSTEM.INI
'(This example assumes Windows is in the C:\Windows directory)
returned = GetPrivateProfileInt("keyboard", "type", -1, "C:\Windows\system.ini")
If returned = -1 Then
Form1.Print "Function call failed."
Else
Form1.Print returned
End If
Declare Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
lpReturnedString
. If the function was successful, this data will be the string data read from the INI file. If it failed (because the specified file and/or section and/or value doesn't exist), the given default string is used.lpApplicationName | The header of the section that the value in the INI file is in. This is the name enclosed in brackets in the INI file. Do not include the brackets with this parameter. |
lpKeyName | The name of the value in the specified section of the INI file to read. This is the name on the left side of the = sign. |
lpDefault | The string to put into lpReturnedString if the function fails to find the specified value.
|
lpReturnedString | Pass a fixed-length string as this. The function will put the returned value into it. |
nSize | The length in characters of lpReturnedString .
|
lpFileName | The filename of the INI file to read from. |
'This example reads the "scrnsave.exe" value from the [boot] section of SYSTEM.INI.
'(This example assumes the Windows directory is C:\Windows)
Dim buffer As String * 255
x = GetPrivateProfileString("boot", "scrnsave.exe", "(not found)", buffer, 255, "c:\windows\system.ini")
If buffer = "(not found)" Then
Form1.Print "Screen saver not found."
Else
Form1.Print "The screen saver is "; Left(buffer, x)
End If
Declare Function GetProfileInt Lib "kernel32.dll" Alias "GetProfileIntA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal nDefault As Long) As Long
lpAppName | The header of the WINDOWS.INI section the value is in. The heading is the string in brackets at the top of a section of the file. Do not put the brackets into this string. |
lpKeyName | The name of the value to read. This is the string on the left side of the = sign in WIN.INI. |
nDefault | If the function fails to read a valid value, this is the value returned. Make it something that wouldn't be returned if successful, such as -1. |
'Read the value for "WallpaperStyle" under the [Desktop] section of WIN.INI
returned = GetProfileInt("Desktop", "WallpaperStyle", -1)
If returned = -1 Then
Form1.Print "Function call failed."
Else
Form1.Print returned
End If
Declare Function GetProfileString Lib "kernel32.dll" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
lpReturnedString
. If the function was successful, this data will be the string data read from WIN.INI. If it failed (because the specified file and/or section and/or value doesn't exist), the given default string is used. This function is basically a watered-down version of GetPrivateProfileString becuae, unlike that function, GetProfileString only works with WIN.INI.lpAppName | The header of the section that the value in the INI file is in. This is the name enclosed in brackets in the INI file. Do not include the brackets with this parameter. |
lpKeyName | The name of the value in the specified section of the INI file to read. This is the name on the left side of the = sign. |
lpDefault | The string to put into lpReturnedString if the function fails to find the specified value.
|
lpReturnedString | Pass a fixed-length string as this. The function will put the returned value into it. |
nSize | The length in characters of lpReturnedString .
|
'This example reads the "Wallpaper" value from the [Desktop] section of WIN.INI.
Dim buffer As String * 255
x = GetProfileString("Desktop", "Wallpaper", "(not found)", buffer, 255)
If buffer = "(not found)" Then
Form1.Print "Wallpaper not found."
Else
Form1.Print "The wallpaper is "; Left(buffer, x)
End If
Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOPENFILENAME As OPENFILENAME) As Long
pOPENFILENAME | Holds the parameters needed to save the dialog box. Also holds the returned filename. |
'Call the Save File dialog box and read the filename
Dim file As OPENFILENAME, x As Long, filename As String
file.hwndOwner = Form1.hWnd 'Calling form's handle
file.lpstrTitle = "Save File As" 'Title bar
'Set the File Type drop-box values
file.lpstrFilter = "Text Files" & vbNullChar & "*.txt" & vbNullChar & vbNullChar
file.lpstrFile = Space(255) 'Path and file buffer
file.nMaxFile = 255 'Length of buffer
file.lpstrFileTitle = Space(255) 'File name buffer
file.nMaxFileTitle = 255 'Length of buffer
file.lpstrDefExt = "txt" 'Default file extension
'Only existing paths, warn if already exists, and hide read-only check box
file.flags = OFN_OVERWRITEPROMPT Or OFN_PATHMUSTEXIST Or OFN_HIDEREADONLY
file.lStructSize = Len(file) 'Variable's size
x = GetSaveFileName(file)
If x = 0 Then Exit Sub 'Abort if user hit Cancel
'Extract the filename
temp = Trim(file.lpstrFile)
filename = Left(temp, Len(temp) - 1)
Declare Function GetSystemDirectory Lib "kernel32.dll" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
lpBuffer
. lpBuffer
must be a fixed-length string.lpBuffer | A fixed-length string which will receive the path. Make sure it is sufficiently long. |
nSize | The length in characters of lpBuffer .
|
'Get the system directory and extract it to a variable
Dim buffer As String * 255, syspath As String
n = GetSystemDirectory(buffer, Len(buffer))
syspath = Left(buffer, n)
Declare Sub GetSystemTime Lib "kernel32.dll" (lpSystemTime As SYSTEMTIME)
lpSystemTime | Receives the computer's date and time in absolute format. |
'Print the date in mm-dd-yyyy format.
'(Since this is UTC time, the date may be different!!!)
Dim systime As SYSTEMTIME
GetSystemTime systime
Debug.Print systime.wMonth; "-"; systime.wDay; "-"; systime.wYear
Declare Function GetTempFileName Lib "kernel32.dll" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
lpTempFileName
. Yes, you actually don't need to pass the length of the string like you normally do! The format of the generated filename is path\xxxuuuu.TMP
. path is the path specified. You should use the default Temp directory, gotten from the GetTempPath() API function. xxx is a specified three-character string. uuuu is a hexadecimal number that depends on wUnique
. If wUnique
is non-zero, uuuu is the rightmost four digits of that number in hexadecimal, and the file is not created. Note that this will work even if a file with that name already exists. If it is zero, Windows generates a hex-string for it guaranteed not to be already used. In this case, Windows also creates the file for you. The file always has a .TMP extension. The function returns the value used for uuuu, in decimal. The example demonstrates how to extract the data from lpTempFileName
. One final note: please remember to delete the temporary file when your program is done using it. The Temp directory is always clogged with outdated files left behind by programs.lpszPath | The path to put the file into. You should use the path gotten from GetTempPath(). |
lpPrefixString | The first three characters of this string are used as the first three characters of the filename. |
wUnique | If nonzero, the last four characters of the filename are this number's hexadecimal representation, and the file is not created. If zero, the last four characters are generated by Windows, and the file is created. |
lpTempFileName | A fixed-length string that receives the path and filename of the temporary file. |
'Display a randomly generated temporary filename
Dim temppathx As String * 255, tempfilex As String * 255
x = GetTempPath(255, temppath) 'get Windows's Temp directory
temppath = Left(temppathx, x) 'extract useful data from it
x = GetTempFileName(temppath, "API", 0, tempfilex
'**The next line extracts the useful data from the string**
tempfile = Left$(Trim$(tempfilex), Len(Trim$(tempfilex)) - 1)
Form1.Print "Temporary filename is:"
Form1.Print tempfile
'Filename will be in format (path)\API????.TMP
'This file also now exists in that path!
Declare Function GetTempPath Lib "kernel32.dll" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
nBufferLength | The length in characters of lpBuffer .
|
lpBuffer | A fixed-length string that will receive the path of the Temp directory. |
'Display the Temp directory
Dim tempdir As String * 255 'more than enough room!
x = GetTempPath(255, tempdir) 'get the directory
Form1.Print "The Temp directory is:"
Form1.Print Left(tempdir, x) 'extract useful information
Declare Function GetTimeZoneInformation Lib "kernel32.dll" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long
lpTimeZoneInformation | The variable which receives the information about the time zone. |
'Read the name of the standard-time time zone's name
Dim tzi As TIME_ZONE_INFORMATION, c As Integer, x As Long
x = GetTimeZoneInformation(tzi)
For c = 0 To 32
If tzi.StandardName(c) = 0 Then Exit For
Debug.Print Chr$(tzi.StandardName(c));
Next c
Declare Function GetVersionEx Lib "kernel32.dll" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
lpVersionInformation | Receives the version information. Set the .dwOSVersionInfoSize member to the length of the variable, or Len(lpVersionInformation).
|
'Read the version number of Windows
Dim os As OSVERSIONINFO
os.dwOSVersionInfoSize = Len(os) 'Set size of variable
x = GetVersionEx(os)
Form1.Print os.dwMajorVersion; "."; os.dwMinorVersion
'For Windows 95, may print 4 . 0
Declare Function GetWindowRect Lib "user32.dll" (ByVal hwnd As Long, lpRect As RECT) As Long
hwnd | The handle of the window to read the position and width of. |
lpRect | A RECT variable that will receive the coordinates of the upper-left and lower-right corners of the window. |
'Find the width and height of Form1 using the GetWindowRect function
Dim r As RECT
x = GetWindowRect(Form1.hWnd, r)
Form1.Print "Width ="; r.Right - r.Left
Form1.Print "Height ="; r.Bottom - r.Top
Declare Function GetWindowsDirectory Lib "kernel32.dll" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
lpBuffer
. lpBuffer
must be a fixed-length string.lpBuffer | A fixed-length string which will receive the path. Make sure it is sufficiently long. |
nSize | The length in characters of lpBuffer .
|
'Get the Windows directory and extract it to a variable
Dim buffer As String * 255, winpath As String
n = GetWindowsDirectory(buffer, Len(buffer))
winpath = Left(buffer, n)
Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
lpString
.hwnd | The handle of the window to read the .Caption property of. |
lpString | A fixed-length string that will receive the .Caption property value. |
cch | The length in characters of lpString .
|
'Read the number of characters in the .Caption of Form1
n = GetWindowTextLength(Form1.hWnd)
'Create a string of n+1, to allow for the vbNullChar at the end
buffer = Space$(n + 1)
'Read and display the .Caption property
x = GetWindowText(Form1.hWnd, buffer, n + 1)
Form1.Print Left$(buffer, x)
Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
vbNullChar
to the end of the returned string.hwnd | The handle of the window to read the length of the .Caption property of. |
'Read the number of characters in the .Caption of Form1
n = GetWindowTextLength(Form1.hWnd)
'Create a string of n+1, to allow for the vbNullChar at the end
buffer = Space$(n + 1)
'Read and display the .Caption property
x = GetWindowText(Form1.hWnd, buffer, n + 1)
Form1.Print Left$(buffer, x)
Paul Kuliniewicz
E-mail: Borg953@aol.com
All material presented on these pages is Copyright © Paul Kuliniewicz, except for other copyrighted material.
http://members.aol.com/Borg953/api/g.html