C# 2005 e a Plataforma .NET 2.0 - Ativando e Desativando Device com Win32Device (API)
Programa que demosntra como listar, ativar e desativar um device, através de chamada API win32.
Programa.cs
using
System;using
System.Collections.Generic;using
System.Text;using
System.Runtime.InteropServices;namespace
DeviceInterfaceConsole{
class Program{
public const int DIGCF_PRESENT = 2; public const int DIF_PROPERTYCHANGE = 0x00000012; public const int DICS_FLAG_GLOBAL = 0x00000001; public const int INVALID_HANDLE_VALUE = -1; public const int DICS_ENABLE = 0x00000001; public const int DICS_DISABLE = 0x00000002; public const string USBVIDPID = @"USB\VID_0403&PID_6001\A1000PRA";[
StructLayout(LayoutKind.Sequential)] public struct SP_DEVINFO_DATA{
public int cbSize; public Guid ClassGuid; public int DevInst; public int Reserved;}
[
StructLayout(LayoutKind.Sequential)] public struct SP_CLASSINSTALL_HEADER{
public int cbSize; public int InstallFunction;}
[
StructLayout(LayoutKind.Sequential)] public struct SP_PROPCHANGE_PARAMS{
public SP_CLASSINSTALL_HEADER ClassInstallHeader; public int StateChange; public int Scope; public int HwProfile; public void Init(){
ClassInstallHeader =
new SP_CLASSINSTALL_HEADER();}
}
[
DllImport("setupapi.dll")] internal static extern IntPtr SetupDiGetClassDevs( ref Guid ClassGuid, IntPtr Enumerator, IntPtr hWndParent, int Flags);[
DllImport("setupapi.dll")] public static extern bool SetupDiEnumDeviceInfo( IntPtr DeviceInfoSet, int Supplies, ref SP_DEVINFO_DATA DeviceInfoData);[
DllImport("setupapi.dll")] public static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);[
DllImport("setupapi.dll")] public static extern bool SetupDiSetClassInstallParams( IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, ref SP_CLASSINSTALL_HEADER ClassInstallParams, int ClassInstallParamsSize);[
DllImport("setupapi.dll")] public static extern bool SetupDiCallClassInstaller( int InstallFunction, IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData); private static IntPtr hDevInfo = (IntPtr)0; static void Main(string[] args){
GetAllDeviceClasses(0);
// Desativar o DeviceGetAllDeviceClasses(1);
// Ativar o Device}
static void GetAllDeviceClasses(int opc){
IntPtr classGuid = IntPtr.Zero; String strEnumerator = null; IntPtr hwndParent = IntPtr.Zero; Int32 flags = Win32DeviceMgmt.DIGCF_ALLCLASSES; IntPtr pDevInfoSet = IntPtr.Zero; IntPtr pNewDevInfoSet = IntPtr.Zero; String strMachineName = null;pNewDevInfoSet =
Win32DeviceMgmt.SetupDiGetClassDevsEx(classGuid,strEnumerator,
hwndParent,
flags,
pDevInfoSet,
strMachineName,
IntPtr.Zero); if (pNewDevInfoSet == IntPtr.Zero){
Console.WriteLine("Failed to get device information list"); return;}
Int32 iRet; Int32 iMemberIndex = 0; do{
Win32DeviceMgmt.SP_DEVINFO_DATA devInfoData = new Win32DeviceMgmt.SP_DEVINFO_DATA();devInfoData.cbSize = 28;
devInfoData.ClassGuid =
Guid.Empty;devInfoData.DevInst = 0;
devInfoData.Reserved =
UIntPtr.Zero;iRet =
Win32DeviceMgmt.SetupDiEnumDeviceInfo(pNewDevInfoSet, iMemberIndex, ref devInfoData); if (iRet == 0){
Int32 iLastError = Win32DeviceMgmt.GetLastError(); if (iLastError == Win32DeviceMgmt.ERROR_NO_MORE_FILES){
Console.WriteLine("No more devices in list"); Console.WriteLine("***********************"); break;}
else{
iMemberIndex++;
continue;}
}
Console.WriteLine("Device: {0}", iMemberIndex); Console.WriteLine("\tGuid={0}", devInfoData.ClassGuid); Console.WriteLine("\tName={0}", GetClassNameFromGuid(devInfoData.ClassGuid)); Console.WriteLine("\tDescription={0}", GetClassDescriptionFromGuid(devInfoData.ClassGuid)); Console.WriteLine("\tInstance Id={0}", GetDeviceInstanceId(pNewDevInfoSet, devInfoData)); if (GetDeviceInstanceId(pNewDevInfoSet, devInfoData) == USBVIDPID){
if (opc == 0){
DisableNetAdapter(pNewDevInfoSet, iMemberIndex);
}
else{
EnableNetAdapter(pNewDevInfoSet, iMemberIndex);
}
break;}
iMemberIndex++;
}
while (true); Win32DeviceMgmt.SetupDiDestroyDeviceInfoList(pNewDevInfoSet);}
static String GetClassNameFromGuid(Guid guid){
StringBuilder strClassName = new StringBuilder(0); Int32 iRequiredSize = 0; Int32 iSize = 0; Int32 iRet = Win32DeviceMgmt.SetupDiClassNameFromGuid(ref guid, strClassName, iSize, ref iRequiredSize);strClassName =
new StringBuilder(iRequiredSize);iSize = iRequiredSize;
iRet =
Win32DeviceMgmt.SetupDiClassNameFromGuid(ref guid, strClassName, iSize, ref iRequiredSize); if (iRet == 1){
return strClassName.ToString();}
return String.Empty;}
static String GetClassDescriptionFromGuid(Guid guid){
StringBuilder strClassDesc = new StringBuilder(0); Int32 iRequiredSize = 0; Int32 iSize = 0; Int32 iRet = Win32DeviceMgmt.SetupDiGetClassDescription(ref guid, strClassDesc, iSize, ref iRequiredSize);strClassDesc =
new StringBuilder(iRequiredSize);iSize = iRequiredSize;
iRet =
Win32DeviceMgmt.SetupDiGetClassDescription(ref guid, strClassDesc, iSize, ref iRequiredSize); if (iRet == 1){
return strClassDesc.ToString();}
return String.Empty;}
static String GetDeviceInstanceId(IntPtr DeviceInfoSet, Win32DeviceMgmt.SP_DEVINFO_DATA DeviceInfoData){
StringBuilder strId = new StringBuilder(0); Int32 iRequiredSize = 0; Int32 iSize = 0; Int32 iRet = Win32DeviceMgmt.SetupDiGetDeviceInstanceId(DeviceInfoSet, ref DeviceInfoData,strId,
iSize,
ref iRequiredSize);strId =
new StringBuilder(iRequiredSize);iSize = iRequiredSize;
iRet =
Win32DeviceMgmt.SetupDiGetDeviceInstanceId(DeviceInfoSet, ref DeviceInfoData, strId, iSize, ref iRequiredSize); if (iRet == 1){
return strId.ToString();}
return String.Empty;}
static bool StateChange(int NewState, int SelectedItem, IntPtr hDevInfo){
SP_PROPCHANGE_PARAMS PropChangeParams; SP_DEVINFO_DATA DeviceInfoData;PropChangeParams =
new SP_PROPCHANGE_PARAMS();PropChangeParams.Init();
DeviceInfoData =
new SP_DEVINFO_DATA();PropChangeParams.ClassInstallHeader.cbSize
=
Marshal.SizeOf(PropChangeParams.ClassInstallHeader);DeviceInfoData.cbSize =
Marshal.SizeOf(DeviceInfoData); if (!SetupDiEnumDeviceInfo(hDevInfo, SelectedItem, ref DeviceInfoData)) return false;PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
PropChangeParams.Scope = DICS_FLAG_GLOBAL;
PropChangeParams.StateChange = NewState;
if (!SetupDiSetClassInstallParams(hDevInfo,
ref DeviceInfoData, ref PropChangeParams.ClassInstallHeader, Marshal.SizeOf(PropChangeParams))) return false; if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,
hDevInfo,
ref DeviceInfoData)) return false; return true;}
static bool DisableNetAdapter(IntPtr hdi, int adapterNumber){
if (hdi == (IntPtr)INVALID_HANDLE_VALUE) return false; bool res = StateChange(DICS_DISABLE, adapterNumber, hdi);SetupDiDestroyDeviceInfoList(hdi);
return res;}
static bool EnableNetAdapter(IntPtr hdi, int adapterNumber){
if (hdi == (IntPtr)INVALID_HANDLE_VALUE) return false; bool res = StateChange(DICS_ENABLE, adapterNumber, hdi);SetupDiDestroyDeviceInfoList(hdi);
return res;}
}
}
Win32Device.cs
using
System;using
System.Text;using
System.Runtime.InteropServices;namespace
DeviceInterfaceConsole{
public class Win32DeviceMgmt{
internal static Int32 ERROR_NO_MORE_FILES = 259; internal static Int32 LINE_LEN = 256; internal static Int32 DIGCF_DEFAULT = 0x00000001; // only valid with DIGCF_DEVICEINTERFACE internal static Int32 DIGCF_PRESENT = 0x00000002; internal static Int32 DIGCF_ALLCLASSES = 0x00000004; internal static Int32 DIGCF_PROFILE = 0x00000008; internal static Int32 DIGCF_DEVICEINTERFACE = 0x00000010; internal static Int32 SPINT_ACTIVE = 0x00000001; internal static Int32 SPINT_DEFAULT = 0x00000002; internal static Int32 SPINT_REMOVED = 0x00000004;[
StructLayout(LayoutKind.Sequential)] internal struct SP_DEVINFO_DATA{
public Int32 cbSize; public Guid ClassGuid; public Int32 DevInst; public UIntPtr Reserved;};
[
StructLayout(LayoutKind.Sequential)] internal struct SP_DEVICE_INTERFACE_DATA{
public Int32 cbSize; public Guid InterfaceClassGuid; public Int32 Flags; public IntPtr Reserved;};
[
StructLayout(LayoutKind.Sequential)] internal struct SP_DRVINFO_DATA{
public Int32 cbSize; public Int32 DriverType; public UIntPtr Reserved;[
MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)] public String Description;[
MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)] public String MfgName;[
MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)] public String ProviderName; public FILETIME DriverDate; public Int64 DriverVersion;};
[
DllImport("setupapi.dll")] internal static extern IntPtr SetupDiGetClassDevsEx(IntPtr ClassGuid,[
MarshalAs(UnmanagedType.LPStr)]String enumerator, IntPtr hwndParent, Int32 Flags, IntPtr DeviceInfoSet,[
MarshalAs(UnmanagedType.LPStr)]String MachineName, IntPtr Reserved);[
DllImport("setupapi.dll")] internal static extern Int32 SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);[
DllImport("setupapi.dll")] internal static extern Int32 SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoSet, IntPtr DeviceInfoData, IntPtr InterfaceClassGuid, Int32 MemberIndex, ref SP_DEVINFO_DATA DeviceInterfaceData);[
DllImport("setupapi.dll")] internal static extern Int32 SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, Int32 MemberIndex, ref SP_DEVINFO_DATA DeviceInterfaceData);[
DllImport("setupapi.dll")] internal static extern Int32 SetupDiClassNameFromGuid(ref Guid ClassGuid, StringBuilder className, Int32 ClassNameSize, ref Int32 RequiredSize);[
DllImport("setupapi.dll")] internal static extern Int32 SetupDiGetClassDescription(ref Guid ClassGuid, StringBuilder classDescription, Int32 ClassDescriptionSize, ref Int32 RequiredSize);[
DllImport("setupapi.dll")] internal static extern Int32 SetupDiGetDeviceInstanceId(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, StringBuilder DeviceInstanceId, Int32 DeviceInstanceIdSize, ref Int32 RequiredSize);[
DllImport("kernel32.dll")] internal static extern Int32 GetLastError();}
}