c# – 如何以编程方式禁用系统设备?

前端之家收集整理的这篇文章主要介绍了c# – 如何以编程方式禁用系统设备?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找方法来禁用C#.NET中的系统设备,给定PID& VID或设备名称.

搜索后,我在这里找到这个项目:http://www.codeproject.com/KB/cs/HardwareHelper.aspx

但是我需要一些可以在XP,Vista& Windows 7(x86和x64操作系统)…我链接的项目仅适用于XP和Vista x86 …即使运行具有管理员权限的应用程序.

有没有人知道可以在所有操作系统上运行的解决方案/库?

谢谢.

解决方法

看起来drf的这个版本有两件事给你带来麻烦.您的x64问题是由SP_DEVINFO_DATA引起的,其中有一个需要IntPtr的uint.第二个是SetupDiGetDevicePropertyW函数是Vista,不能在XP上运行.

这是一个修正了这两个问题的版本.我在XPx86和Win7X64上测试为x86和Anycpu.

  1. public static class DisableHardware
  2. {
  3. const uint DIF_PROPERTYCHANGE = 0x12;
  4. const uint DICS_ENABLE = 1;
  5. const uint DICS_DISABLE = 2; // disable device
  6. const uint DICS_FLAG_GLOBAL = 1; // not profile-specific
  7. const uint DIGCF_ALLCLASSES = 4;
  8. const uint DIGCF_PRESENT = 2;
  9. const uint ERROR_INVALID_DATA = 13;
  10. const uint ERROR_NO_MORE_ITEMS = 259;
  11. const uint ERROR_ELEMENT_NOT_FOUND = 1168;
  12.  
  13. static DEVPROPKEY DEVPKEY_Device_DeviceDesc;
  14. static DEVPROPKEY DEVPKEY_Device_HardwareIds;
  15.  
  16. [StructLayout(LayoutKind.Sequential)]
  17. struct SP_CLASSINSTALL_HEADER
  18. {
  19. public UInt32 cbSize;
  20. public UInt32 InstallFunction;
  21. }
  22.  
  23. [StructLayout(LayoutKind.Sequential)]
  24. struct SP_PROPCHANGE_PARAMS
  25. {
  26. public SP_CLASSINSTALL_HEADER ClassInstallHeader;
  27. public UInt32 StateChange;
  28. public UInt32 Scope;
  29. public UInt32 HwProfile;
  30. }
  31.  
  32. [StructLayout(LayoutKind.Sequential)]
  33. struct SP_DEVINFO_DATA
  34. {
  35. public UInt32 cbSize;
  36. public Guid classGuid;
  37. public UInt32 devInst;
  38. public IntPtr reserved; // CHANGE #1 - was UInt32
  39. }
  40.  
  41. [StructLayout(LayoutKind.Sequential)]
  42. struct DEVPROPKEY
  43. {
  44. public Guid fmtid;
  45. public UInt32 pid;
  46. }
  47.  
  48. [DllImport("setupapi.dll",SetLastError = true)]
  49. static extern IntPtr SetupDiGetClassDevsW(
  50. [In] ref Guid ClassGuid,[MarshalAs(UnmanagedType.LPWStr)]
  51. string Enumerator,IntPtr parent,UInt32 flags);
  52.  
  53. [DllImport("setupapi.dll",SetLastError = true)]
  54. static extern bool SetupDiDestroyDeviceInfoList(IntPtr handle);
  55.  
  56. [DllImport("setupapi.dll",SetLastError = true)]
  57. static extern bool SetupDiEnumDeviceInfo(IntPtr deviceInfoSet,UInt32 memberIndex,[Out] out SP_DEVINFO_DATA deviceInfoData);
  58.  
  59. [DllImport("setupapi.dll",SetLastError = true)]
  60. static extern bool SetupDiSetClassInstallParams(
  61. IntPtr deviceInfoSet,[In] ref SP_DEVINFO_DATA deviceInfoData,[In] ref SP_PROPCHANGE_PARAMS classInstallParams,UInt32 ClassInstallParamsSize);
  62.  
  63. [DllImport("setupapi.dll",SetLastError = true)]
  64. static extern bool SetupDiChangeState(
  65. IntPtr deviceInfoSet,[In] ref SP_DEVINFO_DATA deviceInfoData);
  66.  
  67. [DllImport("setupapi.dll",SetLastError = true)]
  68. static extern bool SetupDiGetDevicePropertyW(
  69. IntPtr deviceInfoSet,[In] ref SP_DEVINFO_DATA DeviceInfoData,[In] ref DEVPROPKEY propertyKey,[Out] out UInt32 propertyType,IntPtr propertyBuffer,UInt32 propertyBufferSize,out UInt32 requiredSize,SetLastError = true)]
  70. static extern bool SetupDiGetDeviceRegistryPropertyW(
  71. IntPtr DeviceInfoSet,[In] ref SP_DEVINFO_DATA DeviceInfoData,UInt32 Property,[Out] out UInt32 PropertyRegDataType,IntPtr PropertyBuffer,UInt32 PropertyBufferSize,[In,Out] ref UInt32 requiredSize
  72. );
  73.  
  74. static DisableHardware()
  75. {
  76. DisableHardware.DEVPKEY_Device_DeviceDesc = new DEVPROPKEY();
  77. DEVPKEY_Device_DeviceDesc.fmtid = new Guid(
  78. 0xa45c254e,0xdf1c,0x4efd,0x80,0x20,0x67,0xd1,0x46,0xa8,0x50,0xe0);
  79. DEVPKEY_Device_DeviceDesc.pid = 2;
  80.  
  81. DEVPKEY_Device_HardwareIds = new DEVPROPKEY();
  82. DEVPKEY_Device_HardwareIds.fmtid = new Guid(
  83. 0xa45c254e,0xe0);
  84. DEVPKEY_Device_HardwareIds.pid = 3;
  85. }
  86.  
  87.  
  88.  
  89.  
  90. public static void DisableDevice(Func<string,bool> filter,bool disable = true)
  91. {
  92. IntPtr info = IntPtr.Zero;
  93. Guid NullGuid = Guid.Empty;
  94. try
  95. {
  96. info = SetupDiGetClassDevsW(
  97. ref NullGuid,null,IntPtr.Zero,DIGCF_ALLCLASSES);
  98. CheckError("SetupDiGetClassDevs");
  99.  
  100. SP_DEVINFO_DATA devdata = new SP_DEVINFO_DATA();
  101. devdata.cbSize = (UInt32)Marshal.SizeOf(devdata);
  102.  
  103. // Get first device matching device criterion.
  104. for (uint i = 0; ; i++)
  105. {
  106. SetupDiEnumDeviceInfo(info,i,out devdata);
  107. // if no items match filter,throw
  108. if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS)
  109. CheckError("No device found matching filter.",0xcffff);
  110. CheckError("SetupDiEnumDeviceInfo");
  111.  
  112. string devicepath = GetStringPropertyForDevice(info,devdata,1); // SPDRP_HARDWAREID
  113.  
  114. // Uncomment to print name/path
  115. //Console.WriteLine(GetStringPropertyForDevice(info,// devdata,DEVPKEY_Device_DeviceDesc));
  116. //Console.WriteLine(" {0}",devicepath);
  117. if (devicepath != null && filter(devicepath)) break;
  118.  
  119. }
  120.  
  121. SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER();
  122. header.cbSize = (UInt32)Marshal.SizeOf(header);
  123. header.InstallFunction = DIF_PROPERTYCHANGE;
  124.  
  125. SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS();
  126. propchangeparams.ClassInstallHeader = header;
  127. propchangeparams.StateChange = disable ? DICS_DISABLE : DICS_ENABLE;
  128. propchangeparams.Scope = DICS_FLAG_GLOBAL;
  129. propchangeparams.HwProfile = 0;
  130.  
  131. SetupDiSetClassInstallParams(info,ref devdata,ref propchangeparams,(UInt32)Marshal.SizeOf(propchangeparams));
  132. CheckError("SetupDiSetClassInstallParams");
  133.  
  134. SetupDiChangeState(
  135. info,ref devdata);
  136. CheckError("SetupDiChangeState");
  137. }
  138. finally
  139. {
  140. if (info != IntPtr.Zero)
  141. SetupDiDestroyDeviceInfoList(info);
  142. }
  143. }
  144. private static void CheckError(string message,int lasterror = -1)
  145. {
  146.  
  147. int code = lasterror == -1 ? Marshal.GetLastWin32Error() : lasterror;
  148. if (code != 0)
  149. throw new ApplicationException(
  150. String.Format("Error disabling hardware device (Code {0}): {1}",code,message));
  151. }
  152.  
  153. private static string GetStringPropertyForDevice(IntPtr info,SP_DEVINFO_DATA devdata,uint propId)
  154. {
  155. uint proptype,outsize;
  156. IntPtr buffer = IntPtr.Zero;
  157. try
  158. {
  159. uint buflen = 512;
  160. buffer = Marshal.AllocHGlobal((int)buflen);
  161. outsize=0;
  162. // CHANGE #2 - Use this instead of SetupDiGetDeviceProperty
  163. SetupDiGetDeviceRegistryPropertyW(
  164. info,propId,out proptype,buffer,buflen,ref outsize);
  165. byte[] lbuffer = new byte[outsize];
  166. Marshal.Copy(buffer,lbuffer,(int)outsize);
  167. int errcode = Marshal.GetLastWin32Error();
  168. if (errcode == ERROR_INVALID_DATA) return null;
  169. CheckError("SetupDiGetDeviceProperty",errcode);
  170. return Encoding.Unicode.GetString(lbuffer);
  171. }
  172. finally
  173. {
  174. if (buffer != IntPtr.Zero)
  175. Marshal.FreeHGlobal(buffer);
  176. }
  177. }
  178.  
  179. }

猜你在找的C#相关文章