System Administration API Reference

This section provides comprehensive documentation for APIs related to system administration tasks in Windows. You can manage services, access the registry, interact with the event log, and handle user accounts programmatically.

Core Concepts

Understanding the underlying principles of Windows system administration is crucial for effective API utilization. This includes knowledge of:

  • Service Control Manager (SCM): Manages Windows services.
  • Windows Registry: A hierarchical database for storing configuration settings.
  • Event Log: Records system, security, and application events.
  • Security Accounts Manager (SAM): Manages local user accounts and groups.

Key APIs

Service Management APIs

Interact with Windows services, allowing you to start, stop, query, and configure them.

StartService(HANDLE hService, DWORD cBufSize, LPCSTR pszServiceArguments)

Starts a specified service.

Parameters:

Name Type Description
hService HANDLE A handle to the service.
cBufSize DWORD The size of the arguments buffer.
pszServiceArguments LPCSTR Optional arguments to pass to the service.

Return Value:

Returns TRUE on success, FALSE on failure.

Use OpenSCManager and OpenService to obtain valid handles.

ControlService(HANDLE hService, DWORD dwControl, LPSERVICE_STATUS lpServiceStatus)

Sends a control code to a specified service.

Parameters:

Name Type Description
hService HANDLE A handle to the service.
dwControl DWORD The control code to send (e.g., SERVICE_CONTROL_STOP, SERVICE_CONTROL_PAUSE).
lpServiceStatus LPSERVICE_STATUS A pointer to a structure that receives status information.

Return Value:

Returns TRUE on success, FALSE on failure.

View more Service Management APIs

Registry Access APIs

Programmatically access and manipulate the Windows Registry for configuration data.

RegOpenKeyEx(HKEY hKey, LPCTSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)

Opens a specified registry key.

Parameters:

Name Type Description
hKey HKEY A handle to an open registry key.
lpSubKey LPCTSTR The name of the subkey to open.
ulOptions DWORD Reserved; must be zero.
samDesired REGSAM An access mask that specifies the desired access rights to the key.
phkResult PHKEY A pointer to a variable that receives the handle to the opened key.

Return Value:

Returns ERROR_SUCCESS on success, or a non-zero error code defined in WinError.h on failure.

RegSetValueEx(HKEY hKey, LPCTSTR lpValueName, DWORD Reserved, DWORD Type, const BYTE* lpData, DWORD cbData)

Sets the data and type of a specified registry value.

Parameters:

Name Type Description
hKey HKEY A handle to an open registry key.
lpValueName LPCTSTR The name of the registry value to set.
Reserved DWORD Reserved; must be zero.
Type DWORD The type of the data to be stored.
lpData const BYTE* A pointer to the buffer that contains the data to be stored.
cbData DWORD The size, in bytes, of the data pointed to by lpData.

Return Value:

Returns ERROR_SUCCESS on success, or a non-zero error code on failure.

View more Registry Access APIs

Event Logging APIs

Write event information to the Windows Event Log and retrieve logged events.

ReportEvent(HANDLE hEventLog, WORD wType, WORD wCategory, DWORD dwEventID, PSID pUserSID, WORD wNumStrings, DWORD dwDataSize, LPCSTR* lpStrings, LPVOID lpRawData)

Writes a string-based event to the specified event log.

Parameters:

Name Type Description
hEventLog HANDLE A handle to the event log.
wType WORD The type of event (e.g., EVENTLOG_ERROR_TYPE, EVENTLOG_WARNING_TYPE).
wCategory WORD A category number that can be used to group events.
dwEventID DWORD The unique identifier for the event.
pUserSID PSID A pointer to the security identifier (SID) of the user.
wNumStrings WORD The number of strings in the lpStrings array.
dwDataSize DWORD The size, in bytes, of the binary data.
lpStrings LPCSTR* An array of null-terminated strings to be placed in the event message.
lpRawData LPVOID Pointer to raw event data.

Return Value:

Returns TRUE on success, FALSE on failure.

View more Event Logging APIs

User and Group Management APIs

Manage user accounts and groups on local systems.

NetUserAdd(LMSTR serverName, DWORD level, LPBYTE buffer, LPDWORD errorReturn)

Adds a new local user account to the user account database.

Parameters:

Name Type Description
serverName LMSTR Pointer to a string that specifies the remote server on which the function is to execute.
level DWORD Information level of the data.
buffer LPBYTE Pointer to a buffer containing the user information.
errorReturn LPDWORD Pointer to a 32-bit value that receives the return status of this function.

Return Value:

If the function succeeds, the return value is NERR_Success. Otherwise, it is an error code.

View more User and Group Management APIs