Native.cs
上传用户:lyzyl198
上传日期:2008-05-19
资源大小:174k
文件大小:7k
源码类别:

C#编程

开发平台:

C#

  1. // UsbEject version 1.0 March 2006
  2. // written by Simon Mourier <email: simon [underscore] mourier [at] hotmail [dot] com>
  3. using System;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace UsbEject.Library
  7. {
  8. internal sealed class Native
  9. {
  10.         // from winuser.h
  11.         internal const int WM_DEVICECHANGE = 0x0219;
  12. // from winbase.h
  13. internal const int INVALID_HANDLE_VALUE = -1;
  14.         internal const int GENERIC_READ = unchecked((int)0x80000000);
  15.         internal const int FILE_SHARE_READ = 0x00000001;
  16.         internal const int FILE_SHARE_WRITE = 0x00000002;
  17.         internal const int OPEN_EXISTING = 3;
  18.         [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
  19.         internal static extern bool GetVolumeNameForVolumeMountPoint(
  20.             string volumeName,
  21.             StringBuilder uniqueVolumeName,
  22.             int uniqueNameBufferCapacity);
  23.         [DllImport("Kernel32.dll", SetLastError = true)]
  24.         internal static extern IntPtr CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, IntPtr lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
  25.         [DllImport("Kernel32.dll", SetLastError = true)]
  26.         internal static extern bool DeviceIoControl(IntPtr hDevice, int dwIoControlCode, IntPtr lpInBuffer, int nInBufferSize, IntPtr lpOutBuffer, int nOutBufferSize, out int lpBytesReturned, IntPtr lpOverlapped);
  27.         [DllImport("Kernel32.dll", SetLastError = true)]
  28.         internal static extern bool CloseHandle(IntPtr hObject);
  29. // from winerror.h
  30. internal const int ERROR_NO_MORE_ITEMS = 259;
  31. internal const int ERROR_INSUFFICIENT_BUFFER = 122;
  32. internal const int ERROR_INVALID_DATA = 13;
  33. // from winioctl.h
  34. internal const string GUID_DEVINTERFACE_VOLUME = "53f5630d-b6bf-11d0-94f2-00a0c91efb8b";
  35. internal const string GUID_DEVINTERFACE_DISK = "53f56307-b6bf-11d0-94f2-00a0c91efb8b";
  36.         internal const int IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS = 0x00560000;
  37.        
  38.         [StructLayout(LayoutKind.Sequential)]
  39. internal struct DISK_EXTENT 
  40. {
  41. internal int DiskNumber;
  42. internal long StartingOffset;
  43. internal long ExtentLength;
  44. }
  45.         // from cfg.h
  46.         internal enum PNP_VETO_TYPE
  47.         {
  48.             Ok,
  49.             TypeUnknown,
  50.             LegacyDevice,
  51.             PendingClose,
  52.             WindowsApp,
  53.             WindowsService,
  54.             OutstandingOpen,
  55.             Device,
  56.             Driver,
  57.             IllegalDeviceRequest,
  58.             InsufficientPower,
  59.             NonDisableable,
  60.             LegacyDriver,
  61.         }
  62.         // from cfgmgr32.h
  63.         [DllImport("setupapi.dll")]
  64.         internal static extern int CM_Get_Parent(
  65.             ref int pdnDevInst,
  66.             int dnDevInst,
  67.             int ulFlags);
  68.         [DllImport("setupapi.dll")]
  69.         internal static extern int CM_Get_Device_ID(
  70.             int dnDevInst,
  71.             StringBuilder buffer,
  72.             int bufferLen,
  73.             int ulFlags);
  74.         [DllImport("setupapi.dll")]
  75.         internal static extern int CM_Request_Device_Eject(
  76.             int dnDevInst,
  77.             out PNP_VETO_TYPE pVetoType,
  78.             StringBuilder pszVetoName,
  79.             int ulNameLength,
  80.             int ulFlags
  81.             );
  82.         [DllImport("setupapi.dll", EntryPoint = "CM_Request_Device_Eject")]
  83.         internal static extern int CM_Request_Device_Eject_NoUi(
  84.             int dnDevInst,
  85.             IntPtr pVetoType,
  86.             StringBuilder pszVetoName,
  87.             int ulNameLength,
  88.             int ulFlags
  89.             );
  90.         // from setupapi.h
  91.         internal const int DIGCF_PRESENT = (0x00000002);
  92.         internal const int DIGCF_DEVICEINTERFACE = (0x00000010);
  93.         internal const int SPDRP_DEVICEDESC = 0x00000000;
  94.         internal const int SPDRP_CAPABILITIES = 0x0000000F;
  95.         internal const int SPDRP_CLASS = 0x00000007;
  96.         internal const int SPDRP_CLASSGUID = 0x00000008;
  97.         internal const int SPDRP_FRIENDLYNAME = 0x0000000C;
  98.         [StructLayout(LayoutKind.Sequential)]
  99. internal class SP_DEVINFO_DATA
  100. {
  101. internal int cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
  102. internal Guid classGuid = Guid.Empty; // temp
  103. internal int devInst = 0; // dumy
  104. internal int reserved = 0;
  105. }
  106. [StructLayout(LayoutKind.Sequential, Pack = 2)]
  107. internal struct SP_DEVICE_INTERFACE_DETAIL_DATA
  108. {
  109. internal int cbSize;
  110.             internal short devicePath;
  111. }
  112. [StructLayout(LayoutKind.Sequential)]
  113. internal class SP_DEVICE_INTERFACE_DATA 
  114. {
  115. internal int cbSize = Marshal.SizeOf(typeof(SP_DEVICE_INTERFACE_DATA));
  116. internal Guid interfaceClassGuid = Guid.Empty; // temp
  117. internal int flags = 0;
  118. internal int reserved = 0;
  119. }
  120.         [DllImport("setupapi.dll")]
  121. internal static extern IntPtr SetupDiGetClassDevs(
  122. ref Guid classGuid,
  123. int enumerator,
  124. IntPtr hwndParent,
  125. int flags);
  126.         [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
  127.         internal static extern bool SetupDiEnumDeviceInterfaces(
  128.             IntPtr deviceInfoSet,
  129.             SP_DEVINFO_DATA deviceInfoData,
  130.             ref Guid interfaceClassGuid,
  131.             int memberIndex,
  132.             SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
  133.         [DllImport("setupapi.dll")]
  134.         internal static extern bool SetupDiOpenDeviceInfo(
  135.             IntPtr deviceInfoSet,
  136.             string deviceInstanceId,
  137.             IntPtr hwndParent,
  138.             int openFlags,
  139.             SP_DEVINFO_DATA deviceInfoData
  140.             );
  141.         [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
  142.         internal static extern bool SetupDiGetDeviceInterfaceDetail(
  143.             IntPtr deviceInfoSet,
  144.             SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
  145.             IntPtr deviceInterfaceDetailData,
  146.             int deviceInterfaceDetailDataSize,
  147.             ref int requiredSize,
  148.             SP_DEVINFO_DATA deviceInfoData);
  149.         [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
  150.         internal static extern bool SetupDiGetDeviceRegistryProperty(
  151.             IntPtr deviceInfoSet,
  152.             SP_DEVINFO_DATA deviceInfoData,
  153.             int property,
  154.             out int propertyRegDataType,
  155.             IntPtr propertyBuffer,
  156.             int propertyBufferSize,
  157.             out int requiredSize
  158.             );
  159.         
  160.         [DllImport("setupapi.dll")]
  161. internal static extern uint SetupDiDestroyDeviceInfoList(
  162. IntPtr deviceInfoSet);
  163.         private Native()
  164. {
  165. }
  166. }
  167. }