USB Dismount not turning off my USB Drive?

I’m making an app that needs to safely remove USB Drives and SD cards.

When i execute my code,I can’t never get to lock the drive.

If i skipp the lock part,The drive gets dissabled but if i start transferring data to it and execute my code,a msg box pops up saying: USB Cannot copy to “FileName” Device not ready.

And also,when i execute the code the drive stays visible in the pc and can be accessed again without having to replug.

Is that normal?
Am i doing some thing wrong?
Am i missing something?
Here’s my code:

HANDLE File;  
 DWORD BytesReturned;  
 char DrivePath[MAX_PATH];  
 char FullPath[MAX_PATH] = "\\\\.\\";  
 PREVENT_MEDIA_REMOVAL Prevent;  
 Prevent.PreventMediaRemoval = FALSE;  
 m_DriveALetter.GetWindowTextA(DrivePath,sizeof(DrivePath));  
 strncat_s(FullPath,DrivePath,2);  
 File = CreateFile(FullPath,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);  
 DeviceIoControl(File,FSCTL_LOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);  
 if(BytesReturned != 0)  
 {  
 DeviceIoControl(File,FSCTL_DISMOUNT_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);  
 DeviceIoControl(File,IOCTL_STORAGE_MEDIA_REMOVAL,&Prevent,sizeof(PREVENT_MEDIA_REMOVAL),NULL,NULL,&BytesReturned,NULL);  
 DeviceIoControl(File,FSCTL_UNLOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);  
 MessageBox("You can now remove the USB Drive","Removed",MB_OK);  
 }  
 else if (BytesReturned == 0)  
 {  
 MessageBox("The Drive Could Not be Removed,Is being used by an app or has not finished an operation","Note",MB_OK);  
 }  
 CloseHandle(File);  
  

Checking BytesReturned isn’t right way to check success of
DeviceIoControl function. Instead, check return values and call
GetLastError() when FALSE.

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@hotmail.com
Sent: Wednesday, December 10, 2008 12:40 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] USB Dismount not turning off my USB Drive?

I’m making an app that needs to safely remove USB Drives and SD cards.

When i execute my code,I can’t never get to lock the drive.

If i skipp the lock part,The drive gets dissabled but if i
start transferring data to it and execute my code,a msg box
pops up saying: USB Cannot copy to “FileName” Device not ready.

And also,when i execute the code the drive stays visible in
the pc and can be accessed again without having to replug.

Is that normal?
Am i doing some thing wrong?
Am i missing something?
Here’s my code:

HANDLE File;  
DWORD BytesReturned;  
char DrivePath[MAX_PATH];  
char FullPath[MAX_PATH] = "\\\\.\\";  
PREVENT_MEDIA_REMOVAL Prevent;  
Prevent.PreventMediaRemoval = FALSE;  
m_DriveALetter.GetWindowTextA(DrivePath,sizeof(DrivePath));  
strncat_s(FullPath,DrivePath,2);  
File = CreateFile(FullPath,GENERIC_READ |   
GENERIC_WRITE,FILE_SHARE_READ |   
FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);  
  
DeviceIoControl(File,FSCTL_LOCK_VOLUME,NULL,NULL,NULL,NULL,&By  
tesReturned,NULL);  
if(BytesReturned != 0)  
{  
  
DeviceIoControl(File,FSCTL_DISMOUNT_VOLUME,NULL,NULL,NULL,NULL  
,&BytesReturned,NULL);  
  
DeviceIoControl(File,IOCTL_STORAGE_MEDIA_REMOVAL,&Prevent,size  
of(PREVENT_MEDIA_REMOVAL),NULL,NULL,&BytesReturned,NULL);  
  
DeviceIoControl(File,FSCTL_UNLOCK_VOLUME,NULL,NULL,NULL,NULL,&  
BytesReturned,NULL);  
MessageBox("You can now remove the USB Drive","Removed",MB_OK);  
}  
else if (BytesReturned == 0)  
{  
MessageBox("The Drive Could Not be Removed,Is   
being used by an app or has not finished an operation","Note",MB_OK);  
}  
CloseHandle(File);  
  

NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online
at http://www.osronline.com/page.cfm?name=ListServer

xxxxx@hotmail.com wrote:

I’m making an app that needs to safely remove USB Drives and SD cards.

When i execute my code,I can’t never get to lock the drive.

If i skipp the lock part,The drive gets dissabled but if i start transferring data to it and execute my code,a msg box pops up saying: USB Cannot copy to “FileName” Device not ready.

And also,when i execute the code the drive stays visible in the pc and can be accessed again without having to replug.

Is that normal?
Am i doing some thing wrong?

Yes. For one, you never check any error codes.

HANDLE File;
DWORD BytesReturned;
char DrivePath[MAX_PATH];
char FullPath[MAX_PATH] = “\\.\”;
PREVENT_MEDIA_REMOVAL Prevent;
Prevent.PreventMediaRemoval = FALSE;
m_DriveALetter.GetWindowTextA(DrivePath,sizeof(DrivePath));
strncat_s(FullPath,DrivePath,2);
File = CreateFile(FullPath,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
DeviceIoControl(File,FSCTL_LOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);
if(BytesReturned != 0)

You do not provide an output buffer. How do you expect there to have
been any “bytes returned”? You should check whether the DeviceIoControl
function returned non-zero – that’s the “success” indicator.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Wow,I get twice as fast and many as any other forum 0_0.

I appreciate all ur help ^^

I fixed a bit my code with all ur suggestions and now i can see i get no error but i still can’t get the drive to get safely removed,its just stays visible and accessible in the system.

Is it Safe to remove it at that stage?

Wow,I forgot to place my new code >_<

And i don;t find how to edit my post.

Here’s my new Code:

HANDLE File;
DWORD BytesReturned;
int Result = 1;
char DrivePath[MAX_PATH];
char FullPath[MAX_PATH] = “\\.\”;
PREVENT_MEDIA_REMOVAL Prevent;
Prevent.PreventMediaRemoval = FALSE;
m_DriveALetter.GetWindowTextA(DrivePath,sizeof(DrivePath));
strncat_s(FullPath,DrivePath,2);
File = CreateFile(FullPath,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
if(File == INVALID_HANDLE_VALUE)
{
MessageBox(“Device Open Failed”,“”,MB_OK);
}
Result = DeviceIoControl(File,FSCTL_LOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);
if(Result == 0)
{
MessageBox(“Failed to Lock the Device”,“”,MB_OK);
}
Result = DeviceIoControl(File,FSCTL_DISMOUNT_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);
if(Result == 0)
{
MessageBox(“Failed to Dismount the Device”,“”,MB_OK);
}
Result = DeviceIoControl(File,IOCTL_STORAGE_MEDIA_REMOVAL,&Prevent,sizeof(PREVENT_MEDIA_REMOVAL),NULL,NULL,&BytesReturned,NULL);
if(Result == 0)
{
MessageBox(“Failed to Remove the Device”,“”,MB_OK);
}
Result = DeviceIoControl(File,FSCTL_UNLOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);
if(Result == 0)
{
MessageBox(“Failed to Unlock the Device”,“”,MB_OK);
}
CloseHandle(File);

You can hardly edit your post in thousands of maiboxes where it was sent
:wink:

Your code is still wrong. It continues executing next IOCTLs after error
which doesn’t make sense. For example, if lock fails, media can’t be
dismounted.

Well, if you don’t get any error, it should be safe to remove the
device. The problem is that after unlock any app can access driver
letter again which in turn causes automatic mount by filesystem. That’s
why it is accessible even after you execute the code. You’d need to
physically remove device/media before unlock. Or avoid automatic mount
when drive letter is accessed which means to remove it. I’m not sure how
to do it correctly. You should probably examine how safe device removal
exactly works.

BTW, IOCTL_STORAGE_MEDIA_REMOVAL doesn’t remove the device.

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@hotmail.com
Sent: Wednesday, December 10, 2008 1:40 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] USB Dismount not turning off my USB Drive?

Wow,I forgot to place my new code >_<

And i don;t find how to edit my post.

Here’s my new Code:

HANDLE File;
DWORD BytesReturned;
int Result = 1;
char DrivePath[MAX_PATH];
char FullPath[MAX_PATH] = “\\.\”;
PREVENT_MEDIA_REMOVAL Prevent;
Prevent.PreventMediaRemoval = FALSE;
m_DriveALetter.GetWindowTextA(DrivePath,sizeof(DrivePath));
strncat_s(FullPath,DrivePath,2);
File = CreateFile(FullPath,GENERIC_READ |
GENERIC_WRITE,FILE_SHARE_READ |
FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
if(File == INVALID_HANDLE_VALUE)
{
MessageBox(“Device Open Failed”,“”,MB_OK);
}
Result =
DeviceIoControl(File,FSCTL_LOCK_VOLUME,NULL,NULL,NULL,NULL,&By
tesReturned,NULL);
if(Result == 0)
{
MessageBox(“Failed to Lock the Device”,“”,MB_OK);
}
Result =
DeviceIoControl(File,FSCTL_DISMOUNT_VOLUME,NULL,NULL,NULL,NULL
,&BytesReturned,NULL);
if(Result == 0)
{
MessageBox(“Failed to Dismount the Device”,“”,MB_OK);
}
Result =
DeviceIoControl(File,IOCTL_STORAGE_MEDIA_REMOVAL,&Prevent,size
of(PREVENT_MEDIA_REMOVAL),NULL,NULL,&BytesReturned,NULL);
if(Result == 0)
{
MessageBox(“Failed to Remove the Device”,“”,MB_OK);
}
Result =
DeviceIoControl(File,FSCTL_UNLOCK_VOLUME,NULL,NULL,NULL,NULL,&
BytesReturned,NULL);
if(Result == 0)
{
MessageBox(“Failed to Unlock the Device”,“”,MB_OK);
}
CloseHandle(File);


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online
at http://www.osronline.com/page.cfm?name=ListServer

LOL! i know my code keeps executing after an error,that’s just an example of my code to showit to you guys.

I will make it cancel the removal if the lock failed,that means the drive is being used.

So,you mean you need to actually physically remove the drive before unlocking it? I mean,isn’t that dangerous?

I will follow ur advice and research deeper on how safe removal works.

Thanks for all ur help and forgive my stupidities,i have never worked with hardware programatically.

I really really appreciate every ones help and this forum rules ^_^.

> -----Original Message-----

From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@hotmail.com
Sent: Wednesday, December 10, 2008 2:18 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] USB Dismount not turning off my USB Drive?

So,you mean you need to actually physically remove the drive
before unlocking it? I mean,isn’t that dangerous?

If the dismount succeeds, it is OK. At least I believe so (my memory
leaks…). Lock causes nobody else can access the device and dismount
should cause cache flush. You can test it. Write a lot of data to the
device (with cache enabled) and then execute your code. Dismount
shouldn’t return until all data are written. It helps to have USB disk
with LED to see it. You can also use USB analyser to see data
communication.

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]

Works,Thanks for your help bro.

I really appreciate it.