Eject USB disks and CM_Request_Device_Eject

Alex808

New member
Joined
Aug 4, 2010
Messages
1
Programming Experience
1-3
Hello everyone, i have been looking all day for a way for my application to 'safely remove hardware' or to eject the USB key specified, the application detects the removal and arrival of new devices but i am stuck on being able to eject them. So far my research has lead me to using setupapi.dll and CM_Request_Device_Eject and i have declared
VB.NET:
 Private Declare Function CM_Request_Device_Eject Lib "setupapi.dll" Alias "CM_Request_Device_EjectW" (ByVal dnDevInst As IntPtr, _ ByRef pVetoType As PNP_VETO_TYPE, ByVal pszVetoName As IntPtr,ByVal ulNameLength As Integer, ByVal ulFlags As Integer) As Integer

I usully code in asp for web application and have no idea how to use these types of unmanaged functions :(

I made my function that takes the drive letter to eject by parameter

VB.NET:
 Public Function EjectDevice(USBDriveLetter as string) As Integer
      
        'TODO USE CM_Request_Device_Eject() to eject the drive

End Function

I found pinvoke.net: cm_request_device_eject (setupapi) this info on pinvoke for CM_Request_Device_Eject() but i am clueless on how it works and the vb.net section looks prolly documented...anyways if anyone can help me with a code snipet or just guide me in the right direction it would be very appreciated.:eek:

Thx

Alex
 
Last edited:
Interesting... after 10+ years, there's still no solution to this?
 
Back
Top