question

FrancescoCuzzocrea-7374 avatar image
0 Votes"
FrancescoCuzzocrea-7374 asked xx-9941 commented

Eject SSD from c++ code

Hi
I'm attempting to remove an ejectable SSD from c++ code, but all examples I found are related on how to eject USD disks or CD rom. Not ssd.
Of course those examples works with usb but doesn't with SSD.
Up to now I can dismount and make it offline, but I can't eject. That is using DeviceIoControl I first open drive, lock it (FSCTL_LOCK_VOLUME), unmount
volume (FSCTL_DISMOUNT_VOLUME) and make offline (IOCTL_VOLUME_OFFLINE). It remains offline but its icon is still present and isn't removed.
Please can you suggest a place where I can find code or documentation regarding ?
Thanks

c++windows-api-general
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Did you also try codes that execute FlushFileBuffers, then DeviceIoControl with IOCTL_STORAGE_MEDIA_REMOVAL, then IOCTL_STORAGE_EJECT_MEDIA?

0 Votes 0 ·
Castorix31 avatar image
1 Vote"
Castorix31 answered

Maybe you can try with CM_Request_Device_Eject
(I have no SSD to test)


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

FrancescoCuzzocrea-7374 avatar image
0 Votes"
FrancescoCuzzocrea-7374 answered Castorix31 commented

Thanks for the suggestion, but it doesn't work (VetoType returned tells that device isn't removable, but device is removable, I can remove it right clicking on the tray icon). CM_Request_Device_Eject works fine with usb disks (I use it for ejecting).

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If you don't get other answers, you could trace API calls with a tool like API Monitor
(I just traced with an External USB Device and it calls successfully CM_Request_Device_Eject_ExW when I click Eject on the Tray icon, so probably it uses another way for SSD...)
(in explorer.exe)





0 Votes 0 ·
Castorix31 avatar image
2 Votes"
Castorix31 answered xx-9941 commented

Another test, x64 only because of HotPlug.dll =>
(change the DeviceInstanceId...)

 // #include <Cfgmgr32.h>
 typedef CONFIGRET(WINAPI* HPED)(HWND hWnd, PCWSTR DeviceInstanceId, DWORD dwFlags);
 HPED HotPlugEjectDevice = NULL;
 HMODULE hDLL = LoadLibrary(TEXT("HotPlug.dll"));
 if (hDLL != NULL)
 {
     HotPlugEjectDevice = (HPED)GetProcAddress(hDLL, "HotPlugEjectDevice");
     if (HotPlugEjectDevice)
     {
         CONFIGRET cr = HotPlugEjectDevice(NULL, TEXT("USB\\VID_1058&PID_1001\\574341505734363536363133"), 0);
         // Test cr...
     }
 }

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Excellent answer !

0 Votes 0 ·

thanks for the great answer.
but does anyone know that is it possible to disable the message box when failed to eject?
and how to eject an ISO image like explorer does?

0 Votes 0 ·
FrancescoCuzzocrea-7374 avatar image
0 Votes"
FrancescoCuzzocrea-7374 answered

Thanks for the suggestion. Regarding previous suggestion, using API Monitor I see the call to CM_Request_Device_Eject_ExW when I eject an USB disk, but It isn't called when I eject an SSD. I have a lot of call, but I can't figure out which of these is related to ejecting. I will check HotPlugEjectDevice.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

FrancescoCuzzocrea-7374 avatar image
0 Votes"
FrancescoCuzzocrea-7374 answered Castorix31 commented

HI
I can't find any documentation regarding HotPlugEjectDevice. In case of SSD the text field shouldn't be usb, but what should I insert ? May be the ssd GUID that is TEXT("\\?\\Volume{<<GUID of SSD>>}") ?

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

With my disk, it is the Parent of the HDD (CM_Get_Parent by enumerating devices with SetupDiGetClassDevs),
but I don't know for SSD

From Device Manager :


87027-device-eject3.jpg


0 Votes 0 ·
device-eject3.jpg (82.1 KiB)
FrancescoCuzzocrea-7374 avatar image
0 Votes"
FrancescoCuzzocrea-7374 answered FrancescoCuzzocrea-7374 edited

Solved !! The methods needs the disk path of the ssd. Thanks for the support.
regards

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

sathishviswanathan-5522 avatar image
0 Votes"
sathishviswanathan-5522 answered

Hi Francesco,

Can you please share the code. I'm also trying the same

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

FrancescoCuzzocrea-7374 avatar image
0 Votes"
FrancescoCuzzocrea-7374 answered Castorix31 commented

Hi
I noticed that sometimes HotPlugEjectDevice returns error code 19 after first attempt and a windows messagebox advise that ssd can't be removed.
If I call newsly the method the result is 0 and the SSD is removed. How can I debug ?
What's the meaning of Flags ?
regards

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

19 has no real meaning : from the headers, it is :

 #define CR_FAILURE                  (0x00000013)

Flags don't seem used...

0 Votes 0 ·