首页 > 代码库 > U盘直接读写(今天用到了)

U盘直接读写(今天用到了)

#ifndef INVALID_SET_FILE_POINTER#define INVALID_SET_FILE_POINTER (DWORD)-1#endif#define FILE_BEGIN           0#define FILE_CURRENT         1#define FILE_END             2HANDLE m_hDisk = NULL;bool Open(char *vol){  char szDrive[10];  sprintf(szDrive, "\\\\.\\%c:", vol[0]);  //sprintf(szDrive, "\\\\.\\scsi1:");  m_hDisk = ::CreateFile(    szDrive,     GENERIC_READ | GENERIC_WRITE,     FILE_SHARE_READ | FILE_SHARE_WRITE,     NULL,     OPEN_EXISTING,     0,     NULL);  DWORD dwError = ::GetLastError();  return m_hDisk != INVALID_HANDLE_VALUE;}void Close(){  if (m_hDisk != INVALID_HANDLE_VALUE)    ::CloseHandle(m_hDisk);}bool ReadSector (DWORD sector, char *Buffer, int sectorSize){  DWORD read = 0;  if (::SetFilePointer(m_hDisk, sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)    return false;  if (!::ReadFile(m_hDisk, Buffer, sectorSize, &read, NULL))    return false;  return true;}bool WriteSector(DWORD sector, char *Buffer, int sectorSize){  DWORD wrote = 0;    if (::SetFilePointer(m_hDisk, sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)    return false;  if (!::WriteFile(m_hDisk, Buffer, sectorSize, &wrote, NULL))    return false;  return true;}

 

U盘直接读写(今天用到了)