Original https://blog.csdn.net/c5113620/article/details/84061600
// compile nasm -f bin boot.asm - O boot.bin // then compiled [c] administrator privileges to run following behind PHYSICALDRIVE3 number that represents the disk number, you can look right - this computer (My Computer ) - management - disk management, wrote the following, disk 0, disk 1
/ * Read asm compiled bin byte file, write to load the vhd disk sectors ** need administrator privileges to run * / #include <the Windows.h> #include <stdio.h> // store the contents of the sector unsigned char Container [ 512 ]; // parameters: string pointer output, starting position, length // return value: the size of the read DWORD ReadDisk (DWORD start, DWORD size) { the OVERLAPPED over = { 0 }; over.Offset = Start; hANDLE handle = CreateFile (TEXT ( " . \\ PHYSICALDRIVE3 \\\\ " ), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0 , NULL); DWORD Error = ERROR_SUCCESS; IF (handle == INVALID_HANDLE_VALUE) { Error = the GetLastError (); // . 5 refused access, without permission the wprintf (L " ReadDisk error code with a SECTOR failed% D \ n- " , Error) ; return 0 ; } DWORD value of readsize; IF (the ReadFile (handle, Container, size, & value of readsize, & over) == 0 ) { the CloseHandle (handle); return 0 ; } // Note that the memory required to release their own CloseHandle(handle); return readsize; } DWORD WriteDisk( DWORD start, DWORD size) { OVERLAPPED over = { 0 }; over.Offset = start; HANDLE handle = CreateFile(TEXT("\\\\.\\PHYSICALDRIVE3"), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); DWORD Error = ERROR_SUCCESS; if (handle == INVALID_HANDLE_VALUE) { Error = GetLastError(); wprintf(L"WriteDisk sector failed with error code %d\n", Error); return 0; } DWORD writesize; if (WriteFile(handle, container, size, &writesize, &over) == 0) { CloseHandle(handle); return 0; } CloseHandle(handle); return writesize; } void readBin(DWORD start, DWORD size) { OVERLAPPED over = { 0 }; over.Offset = start; HANDLE handle = CreateFile(TEXT("\\\\.\\D:\\print.bin"), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); DWORD Error = ERROR_SUCCESS; if (handle == INVALID_HANDLE_VALUE) { Error = GetLastError(); wprintf(L"readBin failed with error code %d\n", Error); return ; } DWORD readsize; if (ReadFile(handle, container, size, &readsize, &over) == 0) { CloseHandle(handle); return ; } } void printContainer() { for (unsigned int i = 0; i < 512; i++) { printf("%02X ", container[i]); } printf("\n"); } int main() { DWORD len; len = ReadDisk(0, 512); printf("len:%d\n", len); printContainer(); //读取bin字节 readBin(0, 512 ); the printf ( " bin: \ n- " ); printContainer (); getchar (); len = WriteDisk ( 0 , 512 ); // write the number of bytes the printf ( " len:% D \ n- " , len) ; getchar (); return 0 ; }