网上有两个方法试过都是失败了。debug也没发现什么问题。希望大家帮忙看看代码问题出在哪里:我找了ioctl码发现只有ioctl_storage_media_removal 是可以用来弹出设备的,我的设备是一HID设备,不是U盘。我该怎么弄呢?是不是ioctl码出错了?
代码:
 LPTSTR buffer = NULL;
    PVOID buffer2 = NULL;
DWORD buffersize = 0;
HANDLE hHandle;
    CString Device_name = _T("USB\\VID_1267&PID_6523");//device name

SP_DEVICE_INTERFACE_DATA strtInterfaceData;
strtInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
BOOL usbSuccess,pathSuccess;
DWORD index =0;
CString temp;
CString m_strPath;
    HIDD_ATTRIBUTES strtAttrib;
for(int i =0;i<10;i++)
{
HDEVINFO hDevInfo =SetupDiGetClassDevs(&guidHID,NULL,0,DIGCF_PRESENT|DIGCF_DEVICEINTERFACE);
usbSuccess = SetupDiEnumDeviceInterfaces(hDevInfo,NULL,&guidHID,index,&strtInterfaceData);
index++;
ULONG predictedLength =0;
ULONG requiredLength =0;
SP_DEVICE_INTERFACE_DETAIL_DATA *strtDetailData;
pathSuccess = SetupDiGetDeviceInterfaceDetail(hDevInfo,&strtInterfaceData,NULL,0,&requiredLength,NULL);
predictedLength = requiredLength;
strtDetailData = (PSP_INTERFACE_DEVICE_DETAIL_DATA)::GlobalAlloc(LMEM_ZEROINIT,predictedLength);
strtDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
pathSuccess = SetupDiGetDeviceInterfaceDetail(hDevInfo,&strtInterfaceData,strtDetailData,predictedLength,&requiredLength,NULL);
m_strPath = strtDetailData->DevicePath; //补充需要的信息
    hHandle = CreateFile(m_strPath,0,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,NULL,NULL);
BOOL Result = HidD_GetAttributes(hHandle,&strtAttrib);

    if(hHandle == INVALID_HANDLE_VALUE)
    {
     MessageBox(_T("can not create the handle"));
    }
LPVOID pdg[MAX_PATH];
DWORD dwBytesReturned = 0;
long res = DeviceIoControl(hHandle,IOCTL_STORAGE_MEDIA_REMOVAL,NULL,0,pdg,sizeof(pdg),&dwBytesReturned,(LPOVERLAPPED)NULL);//THE IOCTL codes might be a problem.
if(res)
{
MessageBox(_T("eject successful!"));
}
else
{
DWORD err;
err = GetLastError();
MessageBox((LPCTSTR)err);
MessageBox(_T("eject failed")); }
}
各位帮帮忙啊,谢谢啦。