Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.0 KiB

  1. #include "dfeject.h"
  2. #include <stdio.h>
  3. #include <winioctl.h>
  4. #include "drvfull.h"
  5. #include "dfioctl.h"
  6. #include "dferr.h"
  7. #include "dfhlprs.h"
  8. HRESULT _IOCTLEject(DWORD dwFlags[], LPTSTR pszArg, DWORD cchIndent)
  9. {
  10. HRESULT hres = S_OK;
  11. HANDLE hDevice;
  12. DWORD dwDesiredAccess;
  13. _StartClock();
  14. switch(GetDriveType(pszArg + 4))
  15. {
  16. case DRIVE_REMOVABLE:
  17. dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
  18. break;
  19. case DRIVE_CDROM:
  20. dwDesiredAccess = GENERIC_READ;
  21. break;
  22. default:
  23. hres = E_INVALIDARG;
  24. }
  25. if (SUCCEEDED(hres))
  26. {
  27. DWORD dwDummy;
  28. BOOL b;
  29. hDevice = _GetDeviceHandle(pszArg, dwDesiredAccess);
  30. if (INVALID_HANDLE_VALUE != hDevice)
  31. {
  32. // Lock the volume
  33. b = DeviceIoControl(hDevice,
  34. FSCTL_LOCK_VOLUME,
  35. NULL, 0,
  36. NULL, 0,
  37. &dwDummy,
  38. NULL);
  39. if (b)
  40. {
  41. // Dismount the volume
  42. b = DeviceIoControl(hDevice,
  43. FSCTL_DISMOUNT_VOLUME,
  44. NULL, 0,
  45. NULL, 0,
  46. &dwDummy,
  47. NULL);
  48. if (b)
  49. {
  50. b = DeviceIoControl(hDevice,
  51. IOCTL_STORAGE_EJECT_MEDIA, // dwIoControlCode operation to perform
  52. NULL, // lpInBuffer; must be NULL
  53. 0, // nInBufferSize; must be zero
  54. NULL, // pointer to output buffer
  55. 0, // size of output buffer
  56. &dwDummy, // receives number of bytes returned
  57. NULL);
  58. _StopClock();
  59. _PrintIndent(cchIndent);
  60. if (b)
  61. {
  62. wprintf(TEXT("Device ejected\n"));
  63. hres = S_OK;
  64. }
  65. else
  66. {
  67. wprintf(TEXT("Cannot lock device\n"));
  68. hres = E_FAIL;
  69. }
  70. }
  71. else
  72. {
  73. _PrintIndent(cchIndent);
  74. wprintf(TEXT("Cannot lock device\n"));
  75. hres = E_FAIL;
  76. }
  77. }
  78. else
  79. {
  80. _PrintIndent(cchIndent);
  81. wprintf(TEXT("Cannot lock device\n"));
  82. hres = E_FAIL;
  83. }
  84. CloseHandle(hDevice);
  85. }
  86. else
  87. {
  88. _PrintIndent(cchIndent);
  89. wprintf(TEXT("Cannot open device\n"));
  90. _PrintGetLastError(cchIndent);
  91. hres = E_DF_CANNOTOPENDEVICE;
  92. }
  93. }
  94. return hres;
  95. }