Imports System
Imports System.Linq
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Imports System.Threading

Public Class Form1
    <DllImport("Coredll.Dll")> _
Public Shared Function DeactivateDevice(ByVal hDevice As IntPtr) As Boolean
    End Function

    <DllImport("coredll.dll", SetLastError:=True)> _
    Private Shared Function ActivateDevice(ByVal lpszDevKey As String, ByVal dwClientInfo As Int32) As IntPtr
    End Function
    Public Shared Function LoadDriver(ByVal strKeyPath As String) As Integer
        Try
            Dim handle As New IntPtr()
            handle = ActivateDevice(strKeyPath, 0)
            Return handle.ToInt32()
        Catch ex As Exception

        End Try
        Return 0
    End Function
    Public Shared Function UninstallDriver(ByVal strKeyPath As String) As Integer
        Dim len As Integer
        Dim rkey As RegistryKey = Registry.LocalMachine
        Dim rkey_sub2 As RegistryKey = rkey.OpenSubKey("Drivers\Active", True)
        Dim names2() As String = rkey_sub2.GetSubKeyNames()
        rkey_sub2.Close()
        len = names2.Length

        Dim ptr As IntPtr = CType(Nothing, IntPtr)
        Do While len > 0
            Dim key As RegistryKey = rkey.OpenSubKey("Drivers\Active\" & names2(len - 1))

            Dim str As String = CStr(key.GetValue("Key"))
            If str IsNot Nothing Then
                If str.IndexOf(strKeyPath) <> -1 Then
                    Dim value As Integer = CInt(Fix(key.GetValue("Hnd")))
                    ptr = New IntPtr(value)
                End If
            End If
            key.Close()
            If ptr <> CType(Nothing, IntPtr) Then
                DeactivateDevice(ptr)
                Exit Do
            End If
            len -= 1
        Loop
        Return 1
    End Function

    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        UninstallDriver("OHCI")
        label1.Text = "Uninstall the USB driver."
        label1.ForeColor = System.Drawing.Color.Red
        Application.DoEvents()
        Thread.Sleep(1000)
        label1.Text = "The driver will be reinstall after 10 seconds."
        label1.ForeColor = System.Drawing.Color.Black
        Application.DoEvents()

        Thread.Sleep(10000)
        LoadDriver("\Drivers\BuiltOut\OHCI\")
        label1.Text = "Reinstall the USB driver."
        label1.ForeColor = System.Drawing.Color.Blue
        Application.DoEvents()
    End Sub
End Class
