본문 바로가기

Window Programming/VB

MBR(Master Boot Recoder) 백업 소스

01 : '---------------------------------------------------
02 : 'MBR 백업 소스
03 : '사용법 : MBRBackUp(저장될경로, Binary로 저장)
04 : '---------------------------------------------------
05 : Private Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)
06 : Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
07 : Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long
08 : Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
09 : Private Declare Function GetLastError Lib "kernel32" () As Long
10 : Private Const GENERIC_READ As Long = &H80000000
11 : Private Const GENERIC_WRITE As Long = &H40000000
12 : Private Const FILE_SHARE_READ As Long = &H1&
13 : Private Const FILE_SHARE_WRITE As Long = &H2&
14 : Private Const OPEN_EXISTING As Long = 3&
15 : Private Const INVALID_HANDLE_VALUE As Long = -1&
16 : Private Const ERROR_SUCCESS As Long = 0&
17 : Private Function GetMBRofMainDrive() As Byte()
18 :      On Error Resume Next
19 :      Dim hDriver As Long, bMBR(0 To 511) As Byte, lRet As Long, Dummy As Long
20 :      Call SetLastError(ERROR_SUCCESS)
21 :      hDriver = CreateFile("\\.\PhysicalDrive0", GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
22 :      If hDriver = INVALID_HANDLE_VALUE Then
23 :           MsgBox "0번 물리 드라이브를 여는 데 오류가 발생했습니다. (Error Code: " & GetLastError() & ")", , "Error"
24 :           Exit Function
25 :      Else
26 :           lRet = ReadFile(hDriver, bMBR(0), 512&, Dummy, ByVal 0&)
27 :           If lRet Then
28 :                GetMBRofMainDrive = bMBR()
29 :           Else
30 :                MsgBox "마스터 부트 레코드(MBR)를 읽는 데에 실패했습니다. (Error Code: " & GetLastError() & ")", , "Error"
31 :                CloseHandle hDriver
32 :                Exit Function
33 :           End If
34 :           CloseHandle hDriver
35 :      End If
36 : End Function
37 : Public Function MBRBackUp(ByVal FileURL As String, Optional Hex_Save As Boolean = True) As Boolean
38 : On Error Goto Error
39 :      Dim FeFi As Integer
40 :      Dim Temp() As Byte
41 :      Temp = GetMBRofMainDrive
42 :     
43 :      FeFi = FreeFile
44 :     
45 :      MBRBackUp = True
46 :     
47 :      If Hex_Save = True Then
48 :           Open FileURL For Binary As #FeFi
49 :                Put #FeFi, , Temp
50 :           Close #FeFi
51 :      Else
52 :           Open FileURL For Output As #FeFi
53 :                Dim Str As String
54 :                Dim i As Integer
55 :               
56 :                For i = 0 To (UBound(Temp) - 1)
57 :                     Str = Str & Temp(i)
58 :                Next i
59 :               
60 :                Print #FeFi, Str
61 :           Close #FeFi
62 :      End If
63 :     
64 :      Exit Function
65 : Error:
66 :      MBRBackUp = False
67 : End Function

 

사용법

--------------------------------------------

MBRBackUp(MBR을백업할파일주소, Hex로 저장)

 

MBR을백업할주소 (예: C:\MBR-Backup.txt)

Hex로 저장  true→Hex로 저장,  False→HEX를 TEXT로 저장