Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. using System.Runtime.InteropServices;
  2.  
  3. [DllImport("winmm.dll")]
  4. static extern Int32 mciSendString(String command, StringBuilder buffer, Int32 bufferSize, IntPtr hwndCallback);
  5.  
  6. // To open the door
  7. mciSendString("set CDAudio door open", null, 0, IntPtr.Zero);
  8.  
  9. // To close the door
  10. mciSendString("set CDAudio door closed", null, 0, IntPtr.Zero);
  11.  
  12. using System;
  13. using System.IO;
  14. using System.Runtime.InteropServices;
  15.  
  16. class Test
  17. {
  18. const int OPEN_EXISTING = 3;
  19. const uint GENERIC_READ = 0x80000000;
  20. const uint GENERIC_WRITE = 0x40000000;
  21. const uint IOCTL_STORAGE_EJECT_MEDIA = 2967560;
  22.  
  23. [DllImport("kernel32")]
  24. private static extern IntPtr CreateFile
  25. (string filename, uint desiredAccess,
  26. uint shareMode, IntPtr securityAttributes,
  27. int creationDisposition, int flagsAndAttributes,
  28. IntPtr templateFile);
  29.  
  30. [DllImport("kernel32")]
  31. private static extern int DeviceIoControl
  32. (IntPtr deviceHandle, uint ioControlCode,
  33. IntPtr inBuffer, int inBufferSize,
  34. IntPtr outBuffer, int outBufferSize,
  35. ref int bytesReturned, IntPtr overlapped);
  36.  
  37. [DllImport("kernel32")]
  38. private static extern int CloseHandle(IntPtr handle);
  39.  
  40. static void EjectMedia(char driveLetter)
  41. {
  42. string path = "\\.\" + driveLetter + ":";
  43. IntPtr handle = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0,
  44. IntPtr.Zero, OPEN_EXISTING, 0,
  45. IntPtr.Zero);
  46. if ((long) handle == -1)
  47. {
  48. throw new IOException("Unable to open drive " + driveLetter);
  49. }
  50. int dummy = 0;
  51. DeviceIoControl(handle, IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0,
  52. IntPtr.Zero, 0, ref dummy, IntPtr.Zero);
  53. CloseHandle(handle);
  54. }
  55.  
  56. static void Main()
  57. {
  58. EjectMedia('f');
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement