[VB .NET] How can I find the right partition?

Discussion in 'Mixed Languages' started by Paky89, Nov 20, 2014.

  1. Paky89

    Paky89 MDL Novice

    Aug 27, 2012
    12
    10
    0
    #1 Paky89, Nov 20, 2014
    Last edited by a moderator: Apr 20, 2017
    Hello everyone,
    I would like to ask for help.
    How can I find the right partition for the grldr injection?
    Tnx 4 All!

    Code:
            Dim objDictionary As Object = CreateObject("Scripting.Dictionary")
            Dim TargetDrive As String = Environment.GetEnvironmentVariable("systemroot").ToCharArray(0, 2)
            Dim isMounted As Boolean = False
            Dim Shell As Object = CreateObject("WScript.Shell")
            Dim objFileSystem As Object = CreateObject("Scripting.FileSystemObject")
    
    
            For Each objDisk In GetObject("winmgmts:\\" & "." & "\root\cimv2").ExecQuery("SELECT * FROM Win32_LogicalDisk")
                objDictionary.Add(objDisk.DeviceID, objDisk.DeviceID)
            Next
            
            'Check to see if SystemVolume is already mounted
            For Each objVol In GetObject("winmgmts:\\" & "." & "\root\cimv2").ExecQuery("SELECT * FROM Win32_Volume Where (SystemVolume='1' and DriveType='3')")
                If objDictionary.Exists(objVol.DriveLetter) Then
                    TargetDrive = objVol.DriveLetter.ToString
                    isMounted = True
                    Exit For
                End If
            Next
    
    
            'find a free drive letter
            If isMounted = False Then
                For a = 67 To 90
                    TargetDrive = Chr(a) & ":"
                    If objDictionary.Exists(TargetDrive) Then
                    Else
                        Exit For
                    End If
                Next
            End If
    
    
            'mount SystemVolume
            If isMounted = False Then
                For Each objVol In GetObject("winmgmts:\\" & "." & "\root\cimv2").ExecQuery("SELECT * FROM Win32_Volume Where (SystemVolume='1' and DriveType='3')")
                    objVol.DriveLetter = TargetDrive
                    objVol.Put_()
                    Exit For
                Next
            End If
    
            '!!!!---grldr injection---!!!!
           IO.File.WriteAllBytes(TargetDrive & "\grldr", My.Resources.grldr)
           Shell.Run(My.Computer.FileSystem.SpecialDirectories.Temp & "\boot\bootinst.exe /nt60 " & TargetDrive, 2, 1)
    
            'make sure we dont unmount C: and then unmount drive
            If isMounted = False Then
                If Not TargetDrive = "C:" Then
                    Shell.Run("mountvol " & TargetDrive & " /D ", 2, 1)
                End If
            End If
    
    based on nononsence code.
     
  2. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,886
    20,275
    180
    #2 Alphawaves, Nov 20, 2014
    Last edited by a moderator: Apr 20, 2017
    Best to use GUID i would think:
    Code:
    Public BootDrive As String
    'Get GUID boot drive
    Public Sub Boot_Drive()
            Dim guid = String.Empty
            For Each Obj As ManagementObject In New ManagementObjectSearcher("root\CIMV2", "SELECT DeviceID FROM Win32_Volume WHERE SystemVolume='True'").Get()
                guid = Obj("DeviceID").Replace("?"c, "."c)
            Next
            BootDrive = guid
        End Sub
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Paky89

    Paky89 MDL Novice

    Aug 27, 2012
    12
    10
    0
    #3 Paky89, Nov 20, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    thanks for the reply, so how should I proceed?
    in my case, if I run the code in an operating system that has a OEM recovery, it activates the OEM recovery partition.
    I hope I explained
     
  4. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,886
    20,275
    180
    If you need help, i have an old app i made for a HP loader (personal use). PM me if you would like it, im sure you could grab something from it to help you :)
    Its written in C#.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...