Eject SSD from c++ code

Francesco Cuzzocrea 1 Reputation point
2021-04-12T08:47:06.977+00:00

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

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,412 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,519 questions
{count} votes

8 answers

Sort by: Most helpful
  1. Castorix31 81,461 Reputation points
    2021-04-12T15:09:22.597+00:00

    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 people found this answer helpful.

  2. Castorix31 81,461 Reputation points
    2021-04-12T08:56:32.23+00:00

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

    1 person found this answer helpful.
    0 comments No comments

  3. Francesco Cuzzocrea 1 Reputation point
    2021-04-12T13:54:50.703+00:00

    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).


  4. Francesco Cuzzocrea 1 Reputation point
    2021-04-12T16:34:54.873+00:00

    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.

    0 comments No comments

  5. Francesco Cuzzocrea 1 Reputation point
    2021-04-12T16:57:30.82+00:00

    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>>}") ?