LockServiceDatabase function (winsvc.h)

[As of Windows Vista, this function is provided for application compatibility and has no effect on the database.]

Requests ownership of the service control manager (SCM) database lock. Only one process can own the lock at any specified time.


SC_LOCK LockServiceDatabase(
  [in] SC_HANDLE hSCManager


[in] hSCManager

A handle to the SCM database. This handle is returned by the OpenSCManager function, and must have the SC_MANAGER_LOCK access right. For more information, see Service Security and Access Rights.

Return value

If the function succeeds, the return value is a lock to the specified SCM database.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

The following error codes can be set by the SCM. Other error codes can be set by registry functions that are called by the SCM.

Return code Description
The handle does not have the SC_MANAGER_LOCK access right.
The specified handle is not valid.
The database is locked.


A lock is a protocol used by setup and configuration programs and the SCM to serialize access to the service tree in the registry. The only time the SCM requests ownership of the lock is when it is starting a service.

A program that acquires the SCM database lock and fails to release it prevents the SCM from starting other services. Because of the severity of this issue, processes are no longer allowed to lock the database. For compatibility with older applications, the LockServiceDatabase function returns a lock but has no other effect.

Windows Server 2003 and Windows XP:  Acquiring the SCM database lock prevents the SCM from starting a service until the lock is released. For example, a program that must configure several related services before any of them starts could call LockServiceDatabase before configuring the first service. Alternatively, it could ensure that none of the services are started until the configuration has been completed.

A call to the StartService function to start a service in a locked database fails. No other SCM functions are affected by a lock.

The lock is held until the SC_LOCK handle is specified in a subsequent call to the UnlockServiceDatabase function. If a process that owns a lock terminates, the SCM automatically cleans up and releases ownership of the lock.

Failing to release the lock can cause system problems. A process that acquires the lock should release it as soon as possible.


Minimum supported client Windows XP [desktop apps only]
Minimum supported server Windows Server 2003 [desktop apps only]
Target Platform Windows
Header winsvc.h (include Windows.h)
Library Advapi32.lib
DLL Advapi32.dll

See also




Service Configuration

Service Functions