2,644
社区成员




int PopUDisk(CString UName)
{
HANDLE hDevice = NULL;// handle to the drive to be examined
BOOL bResult = FALSE;// results flag
DWORD junk = 0;// discard results
DWORD dwError = 0;
int Result = 0;
CString strTemp = _T("\\\\.\\");
strTemp+=UName;
// Open the volume
//hDevice = CreateFileW( "\\\\.\\I: ",// drive to open
hDevice = CreateFileW(strTemp,// drive to open
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,// share mode
NULL,// default security attributes
OPEN_EXISTING,// disposition
0,// file attributes
NULL);// don 't copy any file 's attributes
if(hDevice == INVALID_HANDLE_VALUE)// can 't open the drive
{
dwError = GetLastError();
//AfxMessageBox( "Fail to create file! ");
return 1;//创建文件设备句柄失败
}
//Dismount the volume
bResult = DeviceIoControl(hDevice,// handle to volume
FSCTL_DISMOUNT_VOLUME,// dwIoControlCode
//IOCTL_STORAGE_EJECT_MEDIA,
NULL,// lpInBuffer
0,// nInBufferSize
NULL,// lpOutBuffer
0,// nOutBufferSize
&junk,// discard count of bytes returned
(LPOVERLAPPED)NULL);// synchronous I/O
if(!bResult)// IOCTL failed
{
dwError = GetLastError();
//AfxMessageBox( "Fail to dismount the volume! ");
Result = 2;//卸载u盘失败!
}
// Close the volume handle
bResult = CloseHandle(hDevice);
if(!bResult)
{
dwError = GetLastError();
//AfxMessageBox( "Fail to close handle! ");
return 3;
}
return Result;//success result is 0
}