Friday, March 16, 2012

Use API To See If Windows Started In Safe Mode

Visual Basic gives us access to the Windows API which allows us to do virtually anything. In this source sample we will use the GetSystemMetrics API located in the user32 dll to find out how Windows was started. This is helpful if you want to do something different with your application based on if Windows started in Safe Mode, Safe Mode with Network Support, or Normal mode.

The easiest way to call Windows API's is to declared them in a module so to try this out create a new VB project. Then go to the Project Menu and click Add Module. Inside this module add the following code.

Declare Function GetSystemMetrics Lib "user32" _        (ByVal nIndex As Long) As LongPublic Const SM_CLEANBOOT = 67

No go to your main form, add a command button, double click on the button to go to its click event handler, and add the following code.

Select Case GetSystemMetrics(SM_CLEANBOOT)    Case 1: MsgBox "Windows was Started in Safe Mode."                "Windows was Started in Safe Mode with Network support."    Case Else: MsgBox "Windows is running normally."

Once you've added the source code run your application and click on the button. You should see a message box that corresponds to the way you started Windows.

Obviously this just barely scratches the surface on what you can do with the Windows API. Check out the links below to find out more.


View the original article here

No comments:

Post a Comment