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 86,986 Reputation points
2021-06-30T19:05:02.62+00:00 -
sathish viswanathan 6 Reputation points
2021-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); }