Category Archives: Microsoft

Manage Windows Services

This code should give you everything your need to start, stop, pause, and get the status of services. Module Option Explicit ‘API ConstantsPublic Const SERVICES_ACTIVE_DATABASE = “ServicesActive”‘ Service ControlPublic Const SERVICE_CONTROL_STOP = &H1Public Const SERVICE_CONTROL_PAUSE = &H2’ Service State – for CurrentStatePublic Const SERVICE_STOPPED = &H1Public Const SERVICE_START_PENDING = &H2Public Const SERVICE_STOP_PENDING = &H3Public Const SERVICE_RUNNING = &H4Public… Read More »

Enumerate Domain or Local Users

Just like the title says, this will enumerate a list of users either on the local machine or one a Windows domain. Module Option Explicit ‘API typesPrivate Type USER_INFO    Name As String    Comment As String    UserComment As String    FullName As StringEnd Type Private Type USER_INFO_API    Name As Long    Comment As Long    UserComment As Long    FullName As LongEnd Type Public UserInfo(0 To 1000) As USER_INFO ‘API callsPrivate… Read More »

Number of Bytes in a Directory

Given a valid path, this code uses Windows API calls to determine the number of bytes in the specified directory, including all subdirectories. Module Option Explicit ‘API constantsPublic Const MAX_PATH = 260Public Const INVALID_HANDLE_VALUE = -1Public Const FILE_ATTRIBUTE_DIRECTORY = &H10 ‘API typesPublic Type FILETIME    dwLowDateTime As Long    dwHighDateTime As LongEnd Type Public Type WIN32_FIND_DATA    dwFileAttributes As Long    ftCreationTime As FILETIME    ftLastAccessTime As FILETIME    ftLastWriteTime… Read More »

Enumerate Users Local or Remote

This is a pair of class modules that allow you to easily enumerate the currently logged in users on either the local machine or a specified remote machine. Class Module: clsGetRemoteLoggedInUsers This class returns the logged in users on a remote or local Workstation when the HostName property is set by your Program. It requires that the Class… Read More »

Save an Error log File

The following code shows how one can record errors to a Errors log file. Create an Errors.log file first and place that file in your apps path. Place the error handling in whatever procedures you want. Place the errLogger procedure in a module as a public procedure or in a form as a private procedure. Module Public Sub… Read More »

Count Files in a Directory

Given a valid path, this code will return the number of files in the specified directory. Module Option Explicit ‘API constantsPublic Const MAX_PATH = 260Public Const INVALID_HANDLE_VALUE = -1Public Const FILE_ATTRIBUTE_DIRECTORY = &H10 ‘API typesPublic Type FILETIME    dwLowDateTime As Long    dwHighDateTime As LongEnd Type Public Type WIN32_FIND_DATA    dwFileAttributes As Long    ftCreationTime As FILETIME    ftLastAccessTime As FILETIME    ftLastWriteTime As FILETIME    nFileSizeHigh As Long    nFileSizeLow As Long    dwReserved0 As… Read More »

Get the Temp Directory

Uses Kernel32 to retrieve the path for the Windows temporary directory. Module Private Declare Function GetTempPath Lib “kernel32.dll” Alias “GetTempPathA” ( _    ByVal nBufferLength As Long, _    ByVal lpBuffer As String _    ) As LongPublic Function GetTempDir() As String’ Returns the temp directory from the OS system    Dim sBuffer As String    Dim HoldBuffer As String    Dim iBuffLen As Long    Dim iReturn As Long    Const BUFFER_LENGTH =… Read More »

Use the Recycle Bin

Demonstration of how to send a file to the Windows Recycle Bin. Module ‘ StructuresPrivate Type SHFILEOPSTRUCT    hwnd As Long    wFunc As Long    pFrom As String    pTo As String    fFlags As Integer    fAnyOperationsAborted As Long    hNameMappings As Long    lpszProgressTitle As LongEnd Type ‘ APIPrivate Declare Function SHFileOperation Lib “shell32.dll” Alias “SHFileOperationA” (lpFileOp As SHFILEOPSTRUCT) As Long ‘ ContantsPrivate Const FO_DELETE = &H3Private Const FOF_ALLOWUNDO = &H40Private… Read More »

Another Way to Check if a File Exists

Using the DIR function may not be the best way to determine if a file exists, especially if you are using VB to compare two directories, and take actions when the files do not match. Module Function FileExist(sTestFile As String) As Boolean    ‘This function does not use DIR since it is possible that you might have    ‘been in the middle… Read More »

Convert Long Filename to Short

Nothing more than converting a long filename to a short 8.3 style filename. Module Private Declare Function GetShortPathName Lib “kernel32.dll” Alias “GetShortPathNameA” ( _    ByVal lpszLongPath As String, _    ByVal lpszShortPath As String, _    ByVal cchBuffer As Long_    ) As LongPublic Function ShortName(LongPath As String) As String’***************************************************************************’ Converts Long FileName to Short FileName’***************************************************************************    Dim ShortPath As String    Dim Ret As Long    Const MAX_PATH = 260… Read More »