Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my program requires the ejection of a usb DEVICE ,I GOOGLE this and found sth useful ,but when I finished the code , I found out it can not eject the device I wanted to , i debug found that the computer found the devnode ,but eject failed . I used CM_Request_Device_Eject API in my code.Here is the code:
HDEVINFO hDevInfo;
		SP_DEVINFO_DATA  DeviceInfoData;
		DWORD i;
		hDevInfo = SetupDiGetClassDevs(&guidHID1,0,0,DIGCF_PRESENT|DIGCF_DEVICEINTERFACE);
		if(hDevInfo ==INVALID_HANDLE_VALUE)
		{
			MessageBox(_T("SetupDiGetClassDevs failed"));
		}
		DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
		for(i=0;SetupDiEnumDeviceInfo(hDevInfo,i,&DeviceInfoData);i++)
		{
			LPTSTR buffer = NULL;
			PVOID buffer2 = NULL;
			DWORD buffersize = 0;
			CONFIGRET cr;
			ULONG len;
			PNP_VETO_TYPE pnpvietotype;
			CHAR vetoname[MAX_PATH];
			ULONG ulStatus;
			ULONG ulProblemNumber;
			cr = CM_Get_DevNode_Status(&ulStatus,&ulProblemNumber,DeviceInfoData.DevInst,0);
			if(CR_SUCCESS == cr)
			{
				MessageBox(_T("the device node is found!"));
			}
			else
			{
				DWORD err;
				err = GetLastError();
				MessageBox((LPCTSTR(err)));
				MessageBox(_T("the deice node is not found "));
			}
			 if((DN_DISABLEABLE&ulStatus)!=0)
				 MessageBox(LPCWSTR(DN_DISABLEABLE&ulStatus));
			 if((DN_REMOVABLE&ulStatus)!=0)
				 MessageBox(LPCWSTR(DN_REMOVABLE&ulStatus));
			len = MAX_PATH;
			for(int i = 0;i<=3;i++)
			{
			 cr = CM_Request_Device_Eject(DeviceInfoData.DevInst,&pnpvietotype,(LPWSTR)vetoname,len,0);
			 if(CR_SUCCESS == cr)
			 {
				 DWORD err;
				err = GetLastError();
				MessageBox((LPCTSTR(err)));
				MessageBox(_T("Eject device successfully!"));
			  }
			  else
			  {
				DWORD err;
				err = GetLastError();
				MessageBox((LPCTSTR(err)));
				MessageBox(_T("Eject device failed."));
			   }
			
			
		}
		SetupDiDestroyDeviceInfoList(hDevInfo);   
}

please look at the codes and help me ,really appriciate all of you.
and the compile environment is VS2010+WIN7
Posted
Updated 13-Jul-11 23:36pm
v2

Hi,
Well first of all - what is the value of err after attempting to eject the disk??
Second of all, have you tried to display the text-message of this error code? I see no call to FormatMessage anywhere in your code.

If you haven't, and are not familiar with the method to do so, you may wish to pay particular attention to the use of GetLastError and FormatMessage in this [^]link.

Hope this helps.
 
Share this answer
 
Comments
markfilan 15-Jul-11 1:53am    
i used the way you told me, it's strange the error message it gave back is the operation complete successful.but the return value of cr is not CR_SUCCESS.
CM_Request_Device_Eject not supported after vista !
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900