-
Usb Enable Disable
Hi There,
Myself Abhijit and need some input from your side about DEVCON. I hope you will help me in this regard.
I am working on module in which USB drive needs to be ENABLED or DIABLED through command line argument ON or OFF.
Initially I achieved it by changing registry values for USBSTOR and imagepath location. But as REBOOTING of system is required, I used devcon.exe to enable or disable USB drive. I used command
Devcon disable USBSTOR\DISK* to Disable USB and devcon enable USBSTOR\DISK* to enable USB. Devcon.exe was placed in my code folder.
Now problem is I want to remove devcon dependency and use devcon's code in my application for enabling or disabling USB drive.
I am not getting which functions to used for the same and how?
Do you have any idea or sample code in this regard with you so that i can proceed further.
I am getting lot of compilations errors when I include devcon.h and use functions from devcon.cpp like enumeratedevices and so on.
Can you please help me in this regard as this is very urgent for me? I do not have any prior knowledge about devcon.
Hoping a positive reply from your side.
-
I had the same problem with PCMCIA. Devcon call the setupapi.dll so you need to call this dll in your code. I found a code in c# that work well, you can use it for usb, pcmcia or other peripheral. there is 2 files (programa.cs and win32_app.cs):
//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 = @"PCI\VEN_1033&DEV_00E0&SUBSYS_00E01033&REV_04\5&258BCC1F&2&0211F0";
//public const string USBVIDPID = @"USB\VID_1AC2&PID_0010\VERSION_1.00"; //desko
[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;
[DllImport("kernel32.dll")]
internal static extern bool IsSystemResumeAutomatic();
static void Main(string[] args)
{
if(IsSystemResumeAutomatic())
{
GetAllDeviceClasses(0); // Desativar o Device
GetAllDeviceClasses(1); // Ativar o Device
}
}
public 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,strMachin eName,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;
}
}
}
//win32_app.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 System.Runtime.InteropServices.ComTypes.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();
}
}
Page generated in 1,710,848,849.97544 seconds with 10 queries