A
AgentSmithers
Guest
Hi everyone!
Does anyone have a sniblet to be able to detect if the device is enabled or disabled in device manager. I've done a bit a searching and couldn't find anything of use when attempting to get a status of a device.
What I have so far, it appears the value I am obtaining by marshaling my Data stays the same regardless of its status so my API call is incorrect or the way I am marshaling out and I was hoping one of you may have a sniblet that actually works.
Below are two failed attempts at getting the device status :\
P.S. I'd prefer without using WMI Win32_PnPEntity method, but Native Windows API.
Thank you!!
Similar post: C# How to get capability HardwareDisabled property for hardware device
public static bool GetDeviceState(Func<string, bool> filter) //https://www.codeproject.com/script/...p&zep=GetTypeInfo.c&obid=14469&obtid=2&ovid=1
{
var dpk = new DEVPROPKEY();
dpk.fmtid = new Guid("60b193cb-5276-4d0f-96fc-f173abad3ec6");
dpk.pid = 2;
var displayDevClass = new Guid("{ca3e7ab9-b4c3-4ae6-8251-579ef933890f}".ToString());
var hDevInfo = SetupDiGetClassDevs(ref displayDevClass, null, IntPtr.Zero, DiGetClassFlags.DIGCF_PRESENT | DiGetClassFlags.DIGCF_DEVICEINTERFACE);
if (hDevInfo != INVALID_HANDLE_VALUE)
{
uint i = 0;
while (true)
{
var did = new SP_DEVINFO_DATA();
did.cbSize = (uint)Marshal.SizeOf(did);
if (!SetupDiEnumDeviceInfo(hDevInfo, i, out did)) break;
uint required = 0;
DEVPROPTYPE dpt = 0;
var temp = new byte[0];
SetupDiGetDeviceProperty(hDevInfo, ref did, ref dpk, ref dpt, temp, 0, ref required);
if (required > 0)
{
var data = new byte[required];
if (SetupDiGetDeviceProperty(hDevInfo, ref did, ref dpk, ref dpt, data, required, ref required))
{
Debug.WriteLine(BitConverter.ToString(data));
}
}
}
}
IntPtr info = IntPtr.Zero;
Guid NullGuid = Guid.Empty;
try
{
info = SetupDiGetClassDevsW(ref NullGuid, null, IntPtr.Zero, DIGCF_ALLCLASSES);
CheckError("SetupDiGetClassDevs");
SP_DEVINFO_DATA devdata = new SP_DEVINFO_DATA();
devdata.cbSize = (UInt32)Marshal.SizeOf(devdata);
// Get first device matching device criterion.
for (uint i = 0; ; i++)
{
SetupDiEnumDeviceInfo(info, i, out devdata);
// if no items match filter, throw
if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS)
CheckError("No device found matching filter.", 0xcffff);
CheckError("SetupDiEnumDeviceInfo");
string devicepath = GetStringPropertyForDevice(info, devdata, 1); // SPDRP_HARDWAREID
//uint CM_DEVCAP_HARDWAREDISABLED = 16384u;
uint CM_DEVCAP_HARDWAREDISABLED = 0x100;
uint proptype, outsize;
IntPtr buffer = IntPtr.Zero;
SetupDiGetDeviceRegistryPropertyW(info, ref devdata, (uint)SetupDiGetDeviceRegistryPropertyEnum.SPDRP_CAPABILITIES, out proptype, IntPtr.Zero, 0, out outsize); //ref outside
uint buflen = outsize;
buffer = Marshal.AllocHGlobal((int)buflen);
outsize = 0;
SetupDiGetDeviceRegistryPropertyW(info, ref devdata, (uint)SetupDiGetDeviceRegistryPropertyEnum.SPDRP_CAPABILITIES, out proptype, buffer, buflen, out outsize); //ref outside
//Encoding.Unicode.GetString(lbuffer);
/*
byte[] lbuffer = new byte[outsize];
Marshal.Copy(buffer, lbuffer, 0, (int)outsize);
int errcode = Marshal.GetLastWin32Error();
if (errcode == ERROR_INVALID_DATA) throw new Exception("ERROR_INVALID_DATA");
CheckError("SetupDiGetDeviceRegistryPropertyW", errcode);
*/
byte[] lbuffer = new byte[outsize];
Marshal.Copy(buffer, lbuffer, 0, (int)outsize);
int errcode = Marshal.GetLastWin32Error();
if (errcode == ERROR_INVALID_DATA) return false;
CheckError("SetupDiGetDeviceProperty", errcode);
//return Encoding.Unicode.GetBytes(lbuffer);
uint capabilities = BitConverter.ToUInt32(lbuffer, 0); // always 128
capabilities = getDWORDProp(info, devdata, SetupDiGetDeviceRegistryPropertyEnum.SPDRP_CAPABILITIES);
uint bitwise = capabilities & CM_DEVCAP_HARDWAREDISABLED; // always 0
bool isHardwareDisabled = bitwise > 0;
Debug.WriteLine(devicepath);
Debug.WriteLine(" - HARDWAREDISABLED: " + isHardwareDisabled.ToString().Trim() + " Compat: " + capabilities.ToString());
// Uncomment to print name/path
//Console.WriteLine(GetStringPropertyForDevice(info,
// devdata, DEVPKEY_Device_DeviceDesc));
//Console.WriteLine(" {0}", devicepath);
if (devicepath != null && filter(devicepath)) break;
}
}
finally
{
if (info != IntPtr.Zero)
SetupDiDestroyDeviceInfoList(info);
}
return true; //Need to be fixed
}
//Reliably know if a volume is removable or not with WinApi
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool SetupDiGetDeviceRegistryProperty(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, uint property, out UInt32 propertyRegDataType, byte[] propertyBuffer, uint propertyBufferSize, out UInt32 requiredSize);
public enum RegType : uint
{
REG_BINARY = 3,
REG_DWORD = 4,
REG_EXPAND_SZ = 2,
REG_MULTI_SZ = 7,
REG_SZ = 1
}
const int BUFFER_SIZE = 1024;
static string getStringProp(IntPtr h, SP_DEVINFO_DATA da, SetupDiGetDeviceRegistryPropertyEnum prop)
{
UInt32 requiredSize;
UInt32 regType;
byte[] ptrBuf = new byte[BUFFER_SIZE];
if (!SetupDiGetDeviceRegistryProperty(h, ref da, (uint)prop, out regType, ptrBuf, BUFFER_SIZE, out requiredSize))
throw new InvalidOperationException("Error getting string property");
if (regType != (uint)RegType.REG_SZ || (requiredSize & 1) != 0)
throw new InvalidOperationException("Property is not a REG_SZ");
if (requiredSize == 0)
return "";
return Encoding.Unicode.GetString(ptrBuf, 0, (int)requiredSize - 2);
}
static uint getDWORDProp(IntPtr h, SP_DEVINFO_DATA da, SetupDiGetDeviceRegistryPropertyEnum prop)
{
UInt32 requiredSize;
UInt32 regType;
byte[] ptrBuf = new byte[4];
if (!SetupDiGetDeviceRegistryProperty(h, ref da, (uint)prop, out regType, ptrBuf, 4, out requiredSize))
throw new InvalidOperationException("Error getting DWORD property");
if (regType != (uint)RegType.REG_DWORD || requiredSize != 4)
throw new InvalidOperationException("Property is not a REG_DWORD");
return BitConverter.ToUInt32(ptrBuf, 0);
}
Continue reading...
Does anyone have a sniblet to be able to detect if the device is enabled or disabled in device manager. I've done a bit a searching and couldn't find anything of use when attempting to get a status of a device.
What I have so far, it appears the value I am obtaining by marshaling my Data stays the same regardless of its status so my API call is incorrect or the way I am marshaling out and I was hoping one of you may have a sniblet that actually works.
Below are two failed attempts at getting the device status :\
P.S. I'd prefer without using WMI Win32_PnPEntity method, but Native Windows API.
Thank you!!
Similar post: C# How to get capability HardwareDisabled property for hardware device
public static bool GetDeviceState(Func<string, bool> filter) //https://www.codeproject.com/script/...p&zep=GetTypeInfo.c&obid=14469&obtid=2&ovid=1
{
var dpk = new DEVPROPKEY();
dpk.fmtid = new Guid("60b193cb-5276-4d0f-96fc-f173abad3ec6");
dpk.pid = 2;
var displayDevClass = new Guid("{ca3e7ab9-b4c3-4ae6-8251-579ef933890f}".ToString());
var hDevInfo = SetupDiGetClassDevs(ref displayDevClass, null, IntPtr.Zero, DiGetClassFlags.DIGCF_PRESENT | DiGetClassFlags.DIGCF_DEVICEINTERFACE);
if (hDevInfo != INVALID_HANDLE_VALUE)
{
uint i = 0;
while (true)
{
var did = new SP_DEVINFO_DATA();
did.cbSize = (uint)Marshal.SizeOf(did);
if (!SetupDiEnumDeviceInfo(hDevInfo, i, out did)) break;
uint required = 0;
DEVPROPTYPE dpt = 0;
var temp = new byte[0];
SetupDiGetDeviceProperty(hDevInfo, ref did, ref dpk, ref dpt, temp, 0, ref required);
if (required > 0)
{
var data = new byte[required];
if (SetupDiGetDeviceProperty(hDevInfo, ref did, ref dpk, ref dpt, data, required, ref required))
{
Debug.WriteLine(BitConverter.ToString(data));
}
}
}
}
IntPtr info = IntPtr.Zero;
Guid NullGuid = Guid.Empty;
try
{
info = SetupDiGetClassDevsW(ref NullGuid, null, IntPtr.Zero, DIGCF_ALLCLASSES);
CheckError("SetupDiGetClassDevs");
SP_DEVINFO_DATA devdata = new SP_DEVINFO_DATA();
devdata.cbSize = (UInt32)Marshal.SizeOf(devdata);
// Get first device matching device criterion.
for (uint i = 0; ; i++)
{
SetupDiEnumDeviceInfo(info, i, out devdata);
// if no items match filter, throw
if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS)
CheckError("No device found matching filter.", 0xcffff);
CheckError("SetupDiEnumDeviceInfo");
string devicepath = GetStringPropertyForDevice(info, devdata, 1); // SPDRP_HARDWAREID
//uint CM_DEVCAP_HARDWAREDISABLED = 16384u;
uint CM_DEVCAP_HARDWAREDISABLED = 0x100;
uint proptype, outsize;
IntPtr buffer = IntPtr.Zero;
SetupDiGetDeviceRegistryPropertyW(info, ref devdata, (uint)SetupDiGetDeviceRegistryPropertyEnum.SPDRP_CAPABILITIES, out proptype, IntPtr.Zero, 0, out outsize); //ref outside
uint buflen = outsize;
buffer = Marshal.AllocHGlobal((int)buflen);
outsize = 0;
SetupDiGetDeviceRegistryPropertyW(info, ref devdata, (uint)SetupDiGetDeviceRegistryPropertyEnum.SPDRP_CAPABILITIES, out proptype, buffer, buflen, out outsize); //ref outside
//Encoding.Unicode.GetString(lbuffer);
/*
byte[] lbuffer = new byte[outsize];
Marshal.Copy(buffer, lbuffer, 0, (int)outsize);
int errcode = Marshal.GetLastWin32Error();
if (errcode == ERROR_INVALID_DATA) throw new Exception("ERROR_INVALID_DATA");
CheckError("SetupDiGetDeviceRegistryPropertyW", errcode);
*/
byte[] lbuffer = new byte[outsize];
Marshal.Copy(buffer, lbuffer, 0, (int)outsize);
int errcode = Marshal.GetLastWin32Error();
if (errcode == ERROR_INVALID_DATA) return false;
CheckError("SetupDiGetDeviceProperty", errcode);
//return Encoding.Unicode.GetBytes(lbuffer);
uint capabilities = BitConverter.ToUInt32(lbuffer, 0); // always 128
capabilities = getDWORDProp(info, devdata, SetupDiGetDeviceRegistryPropertyEnum.SPDRP_CAPABILITIES);
uint bitwise = capabilities & CM_DEVCAP_HARDWAREDISABLED; // always 0
bool isHardwareDisabled = bitwise > 0;
Debug.WriteLine(devicepath);
Debug.WriteLine(" - HARDWAREDISABLED: " + isHardwareDisabled.ToString().Trim() + " Compat: " + capabilities.ToString());
// Uncomment to print name/path
//Console.WriteLine(GetStringPropertyForDevice(info,
// devdata, DEVPKEY_Device_DeviceDesc));
//Console.WriteLine(" {0}", devicepath);
if (devicepath != null && filter(devicepath)) break;
}
}
finally
{
if (info != IntPtr.Zero)
SetupDiDestroyDeviceInfoList(info);
}
return true; //Need to be fixed
}
//Reliably know if a volume is removable or not with WinApi
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool SetupDiGetDeviceRegistryProperty(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, uint property, out UInt32 propertyRegDataType, byte[] propertyBuffer, uint propertyBufferSize, out UInt32 requiredSize);
public enum RegType : uint
{
REG_BINARY = 3,
REG_DWORD = 4,
REG_EXPAND_SZ = 2,
REG_MULTI_SZ = 7,
REG_SZ = 1
}
const int BUFFER_SIZE = 1024;
static string getStringProp(IntPtr h, SP_DEVINFO_DATA da, SetupDiGetDeviceRegistryPropertyEnum prop)
{
UInt32 requiredSize;
UInt32 regType;
byte[] ptrBuf = new byte[BUFFER_SIZE];
if (!SetupDiGetDeviceRegistryProperty(h, ref da, (uint)prop, out regType, ptrBuf, BUFFER_SIZE, out requiredSize))
throw new InvalidOperationException("Error getting string property");
if (regType != (uint)RegType.REG_SZ || (requiredSize & 1) != 0)
throw new InvalidOperationException("Property is not a REG_SZ");
if (requiredSize == 0)
return "";
return Encoding.Unicode.GetString(ptrBuf, 0, (int)requiredSize - 2);
}
static uint getDWORDProp(IntPtr h, SP_DEVINFO_DATA da, SetupDiGetDeviceRegistryPropertyEnum prop)
{
UInt32 requiredSize;
UInt32 regType;
byte[] ptrBuf = new byte[4];
if (!SetupDiGetDeviceRegistryProperty(h, ref da, (uint)prop, out regType, ptrBuf, 4, out requiredSize))
throw new InvalidOperationException("Error getting DWORD property");
if (regType != (uint)RegType.REG_DWORD || requiredSize != 4)
throw new InvalidOperationException("Property is not a REG_DWORD");
return BitConverter.ToUInt32(ptrBuf, 0);
}
Continue reading...