It is the same question as Eject SSD from c++ code
The OP said that the method with CM_Request_Device_Eject did not work, but the one I posted with HotPlugEjectDevice worked
(P/Invoke in C#)
Eject SSD drive from C# code

I have been trying to remove an SSD drive from C# code. I can eject USB drive but doesn't with SSD drive
Found a WinAPI call CM_Request_Device_Eject, however I am having trouble getting it to work.
Please find the code snippet
Unicode implemetation :
[DllImport("setupapi.dll")]
static extern int CM_Request_Device_Eject(
int dnDevInst,
out PNP_VETO_TYPE pVetoType,
StringBuilder pszVetoName,
int ulNameLength,
int ulFlags);
[DllImport("setupapi.dll", EntryPoint = "CM_Request_Device_Eject")]
static extern int CM_Request_Device_Eject_NoUi(
int dnDevInst,
IntPtr pVetoType,
StringBuilder pszVetoName,
int ulNameLength,
int ulFlags);
Method call :
int r = CM_Request_Device_Eject_NoUi(DevInstParent, IntPtr.Zero, null, 0, 0);
If anyone can help me with a code snippet or guide me in the right direction it would be very appreciated.
2 answers
Sort by: Most helpful
-
Castorix31 68,856 Reputation points
2021-06-30T19:05:02.62+00:00 sathish viswanathan 1 Reputation point2021-07-04T17:11:02.997+00:00 Thanks for the reply. I tried above mentioned link, I’m able to run successfully in C++ but doesn't with C#. can you please suggest what I missing here.
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate object HPED(IntPtr hWnd, string DeviceInstanceId, uint dwFlags);static void Main(string[] args) { IntPtr hDLL = LoadLibrary(@"c:\\Windows\\System32\\HotPlug.dll"); HPED HotPlugEjectDevice = null; if (hDLL != null) { IntPtr s =GetProcAddress(hDLL, "HotPlugEjectDevice"); if (HotPlugEjectDevice != null) { object obj= HotPlugEjectDevice(IntPtr.Zero, @"SCSI\\DISK&VEN_&PROD_MTFDDAK256TDL\\4&3118ADCB&0&010000", 0); } } bool result = FreeLibrary(hDLL); Console.WriteLine(result); }
Thank you very much. It works fine with C# language. I need another help from you.
How to get the device instance path/parent property value based on the drive letter? (Example:- E drive is SSD)?
@sathish viswanathan
We usually only answer one question in one thread, because this can make the topic of the thread more clear, and people who encounter the same problem in the future will be able to find the same thread with his problem more quickly, so it would be better to post a new thread to ask this question.
@sathish viswanathan
If you just want to get whether the hard disk is HDD or SSD, please refer to this link:
What is the easiest way in C# to check if hard disk is SSD without writing any file on hard disk?
If you want more information, please follow the suggestions in my previous comment.
Hi Castorix
I try to use above code for SSD and USB. It works fine for SSD but doesn't USB drive
If I pass USB Device instance path value, code doesn't eject the drive. Instead if I pass Parent value. USB drive eject successful.
uint cr = HotPlugEjectDevice(IntPtr.Zero, "USBSTOR\DISK&VEN_&PROD_USB_FLASH_DRIVE&REV_PMAP\07103A22B115CF27&0", 0);
Yes, like the VB code with CM_Get_Parent I had posted in this thread : VB code to eject Portable drive
Excellent. Thanks Castorix. It works fine for both USB and SSD. But your tool for USB drive it not return parent value. Do you have above VB code in C# with drive letters
Sign in to comment