如何用程序停用设备id对应的系统设备

xiaoxin0809 2009-06-19 05:24:32
如题所示,有建设性的指点给高分。

设备ID:在windows设备管理器中选择一个设备后点“属性”在“详细信息”页中的“设备范例id”。





...全文
259 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
mark 学习了
xiaoxin0809 2009-06-21
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 Optione 的回复:]
结帖率:0.00% ??怎么混的
[/Quote]

这是我发的第一个帖子,在CSDN很多年,以前遇到的问题都能找到,这一次遇到了别人也没遇到的问题了。
Hemingway718 2009-06-21
  • 打赏
  • 举报
回复
UP
Optione 2009-06-21
  • 打赏
  • 举报
回复
结帖率:0.00% ??怎么混的
xiaoxin0809 2009-06-21
  • 打赏
  • 举报
回复
管用,谢谢,遇到高手了
Optione 2009-06-21
  • 打赏
  • 举报
回复
Win32.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace SystemDevices
{
[StructLayout(LayoutKind.Sequential)]
public struct SP_BROADCAST_HANDLE
{
public int dbch_size;
public int dbch_devicetype;
public int dbch_reserved;
public IntPtr dbch_handle;
public IntPtr dbch_hdevnotify;
public Guid dbch_eventguid;
public long dbch_nameoffset;
public byte dbch_data;
public byte dbch_data1;
}

[StructLayout(LayoutKind.Sequential)]
public class DEV_BROADCAST_DEVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
}

[StructLayout(LayoutKind.Sequential)]
public class SP_DEVINFO_DATA
{
public int cbSize;
public Guid classGuid;
public int devInst;
public IntPtr reserved;
};

[StructLayout(LayoutKind.Sequential)]
public class SP_DEVINSTALL_PARAMS
{
public int cbSize;
public int Flags;
public int FlagsEx;
public IntPtr hwndParent;
public IntPtr InstallMsgHandler;
public IntPtr InstallMsgHandlerContext;
public IntPtr FileQueue;
public IntPtr ClassInstallReserved;
public int Reserved;
[MarshalAs(UnmanagedType.LPTStr)]
public string DriverPath;
};

[StructLayout(LayoutKind.Sequential)]
public class SP_PROPCHANGE_PARAMS
{
public SP_CLASSINSTALL_HEADER ClassInstallHeader = new SP_CLASSINSTALL_HEADER();
public int StateChange;
public int Scope;
public int HwProfile;
};

[StructLayout(LayoutKind.Sequential)]
public class SP_CLASSINSTALL_HEADER
{
public int cbSize;
public int InstallFunction;
};

public class Win32
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient, DEV_BROADCAST_DEVICEINTERFACE NotificationFilter, UInt32 Flags);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern UInt32 UnregisterDeviceNotification(IntPtr hHandle);

[DllImport("setupapi.dll", SetLastError = true)]
public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid,
[MarshalAs(UnmanagedType.LPStr)]String Enumerator, IntPtr hwndParent, Int32 Flags);

[DllImport("setupapi.dll")]
public static extern IntPtr SetupDiGetClassDevsEx(ref Guid ClassGuid,
[MarshalAs(UnmanagedType.LPStr)]String Enumerator,
IntPtr hwndParent, Int32 Flags, IntPtr DeviceInfoSet,
[MarshalAs(UnmanagedType.LPStr)]String MachineName,
IntPtr Reserved);

[DllImport("setupapi.dll", SetLastError = true)]
public static extern Int32 SetupDiDestroyDeviceInfoList(IntPtr lpInfoSet);

[DllImport("setupapi.dll", SetLastError = true)]
public static extern Boolean SetupDiEnumDeviceInfo(IntPtr lpInfoSet, Int32 dwIndex, SP_DEVINFO_DATA devInfoData);

[DllImport("setupapi.dll", SetLastError = true)]
public static extern Boolean SetupDiGetDeviceRegistryProperty(IntPtr lpInfoSet, SP_DEVINFO_DATA DeviceInfoData, UInt32 Property,
UInt32 PropertyRegDataType, StringBuilder PropertyBuffer, UInt32 PropertyBufferSize, IntPtr RequiredSize);

[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern Boolean SetupDiSetClassInstallParams(IntPtr DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, SP_PROPCHANGE_PARAMS ClassInstallParams, int ClassInstallParamsSize);

[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
public static extern Boolean SetupDiCallClassInstaller(UInt32 InstallFunction, IntPtr DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData);

[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
public static extern Boolean SetupDiClassNameFromGuid(ref Guid ClassGuid, StringBuilder className, Int32 ClassNameSize, ref Int32 RequiredSize);

[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
public static extern Boolean SetupDiGetClassDescription(ref Guid ClassGuid, StringBuilder classDescription, Int32 ClassDescriptionSize, ref Int32 RequiredSize);

[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
public static extern Boolean SetupDiGetDeviceInstanceId(IntPtr DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, StringBuilder DeviceInstanceId, Int32 DeviceInstanceIdSize, ref Int32 RequiredSize);

public const int DIGCF_ALLCLASSES = (0x00000004);
public const int DIGCF_PRESENT = (0x00000002);
public const int INVALID_HANDLE_VALUE = -1;
public const int SPDRP_DEVICEDESC = (0x00000000);
public const int MAX_DEV_LEN = 200;
public const int DEVICE_NOTIFY_WINDOW_HANDLE = (0x00000000);
public const int DEVICE_NOTIFY_SERVICE_HANDLE = (0x00000001);
public const int DEVICE_NOTIFY_ALL_INTERFACE_CLASSES = (0x00000004);
public const int DBT_DEVTYP_DEVICEINTERFACE = (0x00000005);
public const int DBT_DEVNODES_CHANGED = (0x0007);
public const int WM_DEVICECHANGE = (0x0219);
public const int DIF_PROPERTYCHANGE = (0x00000012);
public const int DICS_FLAG_GLOBAL = (0x00000001);
public const int DICS_FLAG_CONFIGSPECIFIC = (0x00000002);
public const int DICS_ENABLE = (0x00000001);
public const int DICS_DISABLE = (0x00000002);

public static bool StateChange(bool Enable, int SelectedItem, IntPtr DevInfo)
{
bool result = false;
SP_DEVINFO_DATA devInfoData = new SP_DEVINFO_DATA(); ;
devInfoData.cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
if (true == SetupDiEnumDeviceInfo(DevInfo, SelectedItem, devInfoData))
{
SP_PROPCHANGE_PARAMS pcp = new SP_PROPCHANGE_PARAMS(); ;
pcp.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(SP_CLASSINSTALL_HEADER));
pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
pcp.Scope = DICS_FLAG_GLOBAL;
pcp.StateChange = (Enable ? DICS_ENABLE : DICS_DISABLE);
if (true == SetupDiSetClassInstallParams(DevInfo, devInfoData, pcp, Marshal.SizeOf(pcp)))
{
if (true == SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, DevInfo, devInfoData))
{
result = true;
}
}
}
return result;
}

public static String GetClassNameFromGuid(Guid guid)
{
String result = String.Empty;
StringBuilder className = new StringBuilder();
Int32 iRequiredSize = 0;
Int32 iSize = 0;
bool b = SetupDiClassNameFromGuid(ref guid, className, iSize, ref iRequiredSize);

className = new StringBuilder(iRequiredSize);
iSize = iRequiredSize;

b = SetupDiClassNameFromGuid(ref guid, className, iSize, ref iRequiredSize);
if (true == b)
{
result = className.ToString();
}
return result;
}
public static String GetClassDescriptionFromGuid(Guid guid)
{
String result = String.Empty;
StringBuilder classDesc = new StringBuilder(0);
Int32 iRequiredSize = 0;
Int32 iSize = 0;
bool b = SetupDiGetClassDescription(ref guid, classDesc, iSize, ref iRequiredSize);

classDesc = new StringBuilder(iRequiredSize);
iSize = iRequiredSize;

b = SetupDiGetClassDescription(ref guid, classDesc, iSize, ref iRequiredSize);
if (true == b)
{
result = classDesc.ToString();
}
return result;
}

public static String GetDeviceInstanceId(IntPtr DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData)
{
String result = String.Empty;
StringBuilder id = new StringBuilder(0);
Int32 iRequiredSize = 0;
Int32 iSize = 0;
bool b = SetupDiGetDeviceInstanceId(DeviceInfoSet, DeviceInfoData, id, iSize, ref iRequiredSize);

id = new StringBuilder(iRequiredSize);
iSize = iRequiredSize;

b = SetupDiGetDeviceInstanceId(DeviceInfoSet, DeviceInfoData, id, iSize, ref iRequiredSize);
if (true == b)
{
result = id.ToString();
}
return result;
}
}
}
Optione 2009-06-21
  • 打赏
  • 举报
回复
try:
1.SetupDiGetClassDevs获取设备信息结构句柄
2.SetupDiEnumDeviceInfo遍历每个设备,找出你要停止的(获取每个设备的信息函数很多比如设备范例ID:SetupDiGetDeviceInstanceId)
3.通过SetupDiSetClassInstallParams设置安装的属性
4.调用SetupDiCallClassInstaller应用设备属性
5.最后释放句柄调用:SetupDiDestroyDeviceInfoList

Program.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace SystemDevices
{
class Program
{
static void Main(string[] args)
{
Guid classGuid = Guid.Empty;
IntPtr hDevInfo = Win32.SetupDiGetClassDevs(ref classGuid, null, IntPtr.Zero, Win32.DIGCF_ALLCLASSES | Win32.DIGCF_PRESENT);
if (hDevInfo.ToInt32() == Win32.INVALID_HANDLE_VALUE)
{
Console.WriteLine("访问硬件设备失败");
}
else
{
int i = 0;
StringBuilder deviceName = new StringBuilder();
deviceName.Capacity = Win32.MAX_DEV_LEN;
do
{
SP_DEVINFO_DATA devInfoData = new SP_DEVINFO_DATA();
devInfoData.cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
devInfoData.classGuid = Guid.Empty;
devInfoData.devInst = 0;
devInfoData.reserved = IntPtr.Zero;
bool result = Win32.SetupDiEnumDeviceInfo(hDevInfo, i, devInfoData);
if (false == result)
{
break;
}
Console.WriteLine("Device: {0}", i);
Console.WriteLine("\tGuid={0}", devInfoData.classGuid);
Console.WriteLine("\tName={0}", Win32.GetClassNameFromGuid(devInfoData.classGuid));
Console.WriteLine("\tDescription={0}", Win32.GetClassDescriptionFromGuid(devInfoData.classGuid));
Console.WriteLine("\tInstance Id={0}", Win32.GetDeviceInstanceId(hDevInfo, devInfoData));
++i;
} while (true);
Console.WriteLine("*****************************************************");
Console.Write("输入要禁止的设备号(0-{0}):", i-1);
string str = Console.ReadLine();
int id;
if (true == Int32.TryParse(str, out id))
{
if (id >= 0)
{
Console.Write("输入\"Y\"启用该设备:");
str = Console.ReadLine();
Console.WriteLine("{0}设备{1}", (str.ToLower() == "y" ? "启用" : "禁用"), Win32.StateChange(str.ToLower() == "y", id, hDevInfo) ? "成功" : "失败");
}
}
}
Win32.SetupDiDestroyDeviceInfoList(hDevInfo);
Console.Write("按任意键退出");
Console.Read();
}
}
}
aaa_zzz_000 2009-06-20
  • 打赏
  • 举报
回复
ding...
xiaoxin0809 2009-06-20
  • 打赏
  • 举报
回复
还是没人啊,我再顶
xiaoxin0809 2009-06-20
  • 打赏
  • 举报
回复
是有点困难,系统中是可以通过选定一个设备来对其停用或者启用的,莫非采用的是其他的机制。
期待高手出现
悔说话的哑巴 2009-06-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xiaoxin0809 的回复:]
谢谢楼上的,类似的例子我已经下了好多个了,它们实现的功能是禁用启用同类的所有设备,这样不适合我的需求。我只想启用禁用其中的一个。
有没有高手啊,来帮帮忙啊
[/Quote]
单独的一个,比较困难,我以前弄的也是全部,你这次说了,我也回家试看
bao520min 2009-06-20
  • 打赏
  • 举报
回复
编程爱好者请加群88718955,大家一起探讨,学习
xiaoxin0809 2009-06-20
  • 打赏
  • 举报
回复
谢谢楼上的,类似的例子我已经下了好多个了,它们实现的功能是禁用启用同类的所有设备,这样不适合我的需求。我只想启用禁用其中的一个。
有没有高手啊,来帮帮忙啊
ghost5216 2009-06-20
  • 打赏
  • 举报
回复
你可以参考下这个

启用停用设备 设备管理器 C# API

http://download.csdn.net/source/352079
xiaoxin0809 2009-06-19
  • 打赏
  • 举报
回复
看来这个应用很少有人做过啊
mastzou 2009-06-19
  • 打赏
  • 举报
回复
接2楼:
http://msdn.microsoft.com/en-us/library/bb905315.aspx
请关注其中的Windows Management Instrumentation这里100%有你要的答案
mastzou 2009-06-19
  • 打赏
  • 举报
回复
参考MSDN:
线索1:管理(思路来自dotnet提供的管理接口)
http://msdn.microsoft.com/zh-cn/library/ms184418.aspx
线索2:互操作性(思路来自调用Win32API实现)
http://msdn.microsoft.com/zh-cn/library/ms172270.aspx
线索3:吃饭先,一会儿补充
xiaoxin0809 2009-06-19
  • 打赏
  • 举报
回复
自己顶一下~~~

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧