主题:有个关于磁盘擦除的代码调试问题
BOOL CDirectaccessDlg::WriteSectors(BYTE bDrive, DWORD dwStartSector, WORD wSectors, LPBYTE lpSectBuff)
{
UpdateData(TRUE);
// 对磁盘扇区数据的写入
{
if (bDrive == 0) return 0;
/* char devName[] = "\\\\.\\I:";
devName[4] ='I' + bDrive - 1;*/
CString temp=m_DiskLetter;
CString DiskLetter="\\\\.\\"+temp;
char *devName=DiskLetter.GetBuffer(DiskLetter.GetLength());
DiskLetter.ReleaseBuffer();
// char devName[] = "\\\\.\\I:";
// devName[4] ='I' + bDrive - 1;
devName[4]=temp[0]+bDrive - 1;
HANDLE hDev = CreateFile(devName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hDev == INVALID_HANDLE_VALUE) return 0;
SetFilePointer(hDev, 512 * dwStartSector, 0, FILE_BEGIN);
//DWORD dwCB;
BOOL bRet = WriteFile(hDev, lpSectBuff, 512 * wSectors, &dwStartSector, NULL);
CloseHandle(hDev);
return bRet;
}
}
这个写入磁盘的函数,我单步跟踪发现hDev开始定义时有值,值是0xffffff,但到If与INVALID_HANDLE_VALUE就直接返回0,跳出了。
void CDirectaccessDlg::OnZero()
{
// TODO: Add your control notification handler code here
unsigned char bBuf[512];
UINT i = 0;
BOOL bRet = TRUE;
int uDiskID=1;
int m_bAllDisk=100;
CString strType;
BOOL bFlag;
HANDLE hPt;
DWORD dwSysFlags;
TCHAR FileSysNameBuf[FILESYSNAMEBUFSIZE];
//TCHAR FileSystemNameBuffer[10];
UpdateData(TRUE);
{
CString temp=m_DiskLetter;
CString DiskLetter=temp;
char *Buf=DiskLetter.GetBuffer(DiskLetter.GetLength());
bool Bl=GetVolumeInformation( Buf, NULL, 0, NULL, NULL,
&dwSysFlags, FileSysNameBuf,
FILESYSNAMEBUFSIZE);
if(!(strcmp(FileSysNameBuf,"FAT32")))
{
while (m_bAllDisk)
{
memset(bBuf, 0xFF, sizeof(bBuf));
bRet = WriteSectors(uDiskID, i, 1, bBuf);
if (bRet == FALSE)
{
if (i == 0)
MessageBox("所选磁盘分区不存在!", "错误", MB_OK | MB_ICONERROR);//如果只有起点是0,则程序认为磁盘分区不存在
else
MessageBox("磁盘数据擦除完毕!", "错误", MB_OK | MB_ICONERROR);
return;
}
这里我单步跟踪,bBuf中赋值了,但是是乱码
请高手指教
{
UpdateData(TRUE);
// 对磁盘扇区数据的写入
{
if (bDrive == 0) return 0;
/* char devName[] = "\\\\.\\I:";
devName[4] ='I' + bDrive - 1;*/
CString temp=m_DiskLetter;
CString DiskLetter="\\\\.\\"+temp;
char *devName=DiskLetter.GetBuffer(DiskLetter.GetLength());
DiskLetter.ReleaseBuffer();
// char devName[] = "\\\\.\\I:";
// devName[4] ='I' + bDrive - 1;
devName[4]=temp[0]+bDrive - 1;
HANDLE hDev = CreateFile(devName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hDev == INVALID_HANDLE_VALUE) return 0;
SetFilePointer(hDev, 512 * dwStartSector, 0, FILE_BEGIN);
//DWORD dwCB;
BOOL bRet = WriteFile(hDev, lpSectBuff, 512 * wSectors, &dwStartSector, NULL);
CloseHandle(hDev);
return bRet;
}
}
这个写入磁盘的函数,我单步跟踪发现hDev开始定义时有值,值是0xffffff,但到If与INVALID_HANDLE_VALUE就直接返回0,跳出了。
void CDirectaccessDlg::OnZero()
{
// TODO: Add your control notification handler code here
unsigned char bBuf[512];
UINT i = 0;
BOOL bRet = TRUE;
int uDiskID=1;
int m_bAllDisk=100;
CString strType;
BOOL bFlag;
HANDLE hPt;
DWORD dwSysFlags;
TCHAR FileSysNameBuf[FILESYSNAMEBUFSIZE];
//TCHAR FileSystemNameBuffer[10];
UpdateData(TRUE);
{
CString temp=m_DiskLetter;
CString DiskLetter=temp;
char *Buf=DiskLetter.GetBuffer(DiskLetter.GetLength());
bool Bl=GetVolumeInformation( Buf, NULL, 0, NULL, NULL,
&dwSysFlags, FileSysNameBuf,
FILESYSNAMEBUFSIZE);
if(!(strcmp(FileSysNameBuf,"FAT32")))
{
while (m_bAllDisk)
{
memset(bBuf, 0xFF, sizeof(bBuf));
bRet = WriteSectors(uDiskID, i, 1, bBuf);
if (bRet == FALSE)
{
if (i == 0)
MessageBox("所选磁盘分区不存在!", "错误", MB_OK | MB_ICONERROR);//如果只有起点是0,则程序认为磁盘分区不存在
else
MessageBox("磁盘数据擦除完毕!", "错误", MB_OK | MB_ICONERROR);
return;
}
这里我单步跟踪,bBuf中赋值了,但是是乱码
请高手指教