Microsoft KB Archive/291664

= How To Detect Safe Mode Startup with Visual Basic =

Article ID: 291664

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-



This article was previously published under Q291664



SUMMARY
For some applications, it may be useful to detect if the local system was started in Safe Mode in order to run appropriately in this mode or to avoid running in Safe Mode. This article describes how to use a Win32 application programming interface (API) to determine the startup mode of the local system.



MORE INFORMATION
You can use the GetSystemMetrics function with the SM_CLEANBOOT index specified to detect the startup mode of the system. The SM_CLEANBOOT index returns one of three values, based on how the system was started:

Step-by-Step Example
The following Visual Basic code demonstrates how to detect Safe Mode:  Start a new Visual Basic standard EXE project. Form1 is created by default. Add a new command button (Command1) to Form1.  Paste the following code into the code window of Form1: Private Declare Function GetSystemMetrics Lib &quot;user32&quot; _ (ByVal nIndex As Long) As Long

Const SM_CLEANBOOT& = 67

Private Sub Command1_Click Dim result As Long result = GetSystemMetrics(SM_CLEANBOOT) Select Case result Case 0 MsgBox &quot;System started in normal mode.&quot; Case 1 MsgBox &quot;System started in safe mode.&quot; Case 2 MsgBox &quot;System started in safe mode with networking.&quot; Case Else MsgBox &quot;Unknown value returned from GetSystemMetrics.&quot; End Select End Sub  Click Run, or press the F5 key, to run the project. Click Command1. Notice that a message appears that reflects the startup mode of your system.

