Ejemplo n.º 1
1
 private static string GetStringPropertyForDevice(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, SPDRP propId)
 {
     uint proptype;
     uint outsize = 0;
     var buffer = new byte[MAX_DEVICE_LEN];
     var result = DeviceDetector.SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref deviceInfoData, (uint)propId, out proptype, buffer, (uint)buffer.Length, out outsize);
     if (!result)
     {
         var errcode = Marshal.GetLastWin32Error();
         if (errcode == ERROR_INVALID_DATA) return null;
         throw new Exception("Error calling SetupDiGetDeviceRegistryPropertyW: " + errcode);
     }
     var o = "";
     if (outsize > 0)
     {
         switch (proptype)
         {
             case (uint)REG.REG_SZ:
                 o = Encoding.Unicode.GetString(buffer, 0, (int)outsize - 2);
                 break;
             case (uint)REG.REG_MULTI_SZ:
                 o = Encoding.Unicode.GetString(buffer, 0, (int)outsize - 2);
                 o = o.Trim('\0').Replace("\0", "\r\n");
                 break;
             default:
                 break;
         }
     }
     return o;
 }
Ejemplo n.º 2
0
 static string GetVidPidRev(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, out uint vid, out uint pid, out uint rev)
 {
     VidPidRx = VidPidRx ?? new Regex("(VID|VEN)_(?<vid>[0-9A-F]{4})&PID_(?<pid>[0-9A-F]{4})(&REV_(?<rev>[0-9A-F]{4}))?");
     vid = 0;
     pid = 0;
     rev = 0;
     var value = GetStringPropertyForDevice(deviceInfoSet, deviceInfoData, SPDRP.SPDRP_HARDWAREID) ?? "";
     if (string.IsNullOrEmpty(value)) return value;
     var m = VidPidRx.Match(value.ToUpper());
     if (m.Success)
     {
         vid = System.Convert.ToUInt32(m.Groups["vid"].Value, 16);
         pid = System.Convert.ToUInt32(m.Groups["pid"].Value, 16);
         if (!string.IsNullOrEmpty(m.Groups["rev"].Value))
         {
             rev = System.Convert.ToUInt32(m.Groups["rev"].Value, 16);
         }
     }
     return value;
 }
Ejemplo n.º 3
0
 static DeviceInfo GetDeviceInfo(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, string deviceId)
 {
     var deviceName = GetDeviceDescription(deviceInfoSet, deviceInfoData);
     var deviceManufacturer = GetDeviceManufacturer(deviceInfoSet, deviceInfoData);
     var deviceClassGuid = deviceInfoData.ClassGuid;
     var classDescription = GetClassDescription(deviceClassGuid);
     Win32.DeviceNodeStatus status;
     GetDeviceNodeStatus(deviceInfoData.DevInst, IntPtr.Zero, out status);
     uint vid;
     uint pid;
     uint rev;
     var hwid = GetVidPidRev(deviceInfoSet, deviceInfoData, out vid, out pid, out rev);
     //if (deviceName.Contains("vJoy Device"))
     //{
     //	var sb = new StringBuilder();
     //	var props = (SPDRP[])Enum.GetValues(typeof(SPDRP));
     //	foreach (var item in props)
     //	{
     //		if (new[] { SPDRP.SPDRP_UNUSED0, SPDRP.SPDRP_UNUSED1, SPDRP.SPDRP_UNUSED2 }.Contains(item))
     //		{
     //			continue;
     //		}
     //		try
     //		{
     //			var value = GetStringPropertyForDevice(deviceInfoSet, deviceInfoData, item);
     //			sb.AppendFormat("{0}={1}\r\n", item, value);
     //		}
     //		catch (Exception ex)
     //		{
     //			sb.AppendFormat("{0}={1}\r\n", item, ex.ToString());
     //		}
     //	}
     //}
     var device = new DeviceInfo(deviceId, deviceManufacturer, deviceName, deviceClassGuid, classDescription, status, vid, pid, rev);
     return device;
 }
Ejemplo n.º 4
0
 public static extern bool SetupDiSetSelectedDevice(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData);
Ejemplo n.º 5
0
 public static extern bool SetupDiOpenDeviceInfo(IntPtr deviceInfoSet, string deviceInstanceId, IntPtr hwndParent, UInt32 openFlags, ref SP_DEVINFO_DATA deviceInfoData);
Ejemplo n.º 6
0
 public static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, IntPtr deviceInterfaceDetailData, int deviceInterfaceDetailDataSize, ref int requiredSize, ref SP_DEVINFO_DATA deviceInfoData);
Ejemplo n.º 7
0
 public static extern bool SetupDiEnumDeviceInfo(IntPtr deviceInfoSet, int memberIndex, ref SP_DEVINFO_DATA deviceInfoData);
Ejemplo n.º 8
0
 public static DeviceInfo GetParentDevice(Guid classGuid, DIGCF flags, string deviceId)
 {
     lock (GetDevicesLock)
     {
         IntPtr deviceInfoSet = SetupDiGetClassDevs(classGuid, null, IntPtr.Zero, flags); //  | DIGCF.DIGCF_PRESENT
         if (deviceInfoSet.ToInt32() == ERROR_INVALID_HANDLE_VALUE)
         {
             throw new Exception("Invalid Handle");
         }
         DeviceInfo device = null;
         var deviceInfoData = new SP_DEVINFO_DATA();
         deviceInfoData.Initialize();
         int i;
         for (i = 0; SetupDiEnumDeviceInfo(deviceInfoSet, i, ref deviceInfoData); i++)
         {
             if (deviceId == GetDeviceId(deviceInfoData.DevInst))
             {
                 uint parentDeviceInstance = 0;
                 var CRResult = CM_Get_Parent(out parentDeviceInstance, deviceInfoData.DevInst, 0);
                 if (CRResult == CR.CR_NO_SUCH_DEVNODE) break;
                 if (CRResult != CR.CR_SUCCESS) break;
                 var parentDeviceId = GetDeviceId(parentDeviceInstance);
                 device = GetDevice(classGuid, flags, parentDeviceId);
                 break;
             }
         }
         SetupDiDestroyDeviceInfoList(deviceInfoSet);
         return device;
     }
 }
 public static extern bool SetupDiRemoveDevice(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData);
 internal static extern bool SetupDiGetDeviceInstanceId(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, IntPtr DeviceInstanceId, int DeviceInstanceIdSize, ref int RequiredSize);
 public static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, IntPtr DeviceInterfaceDetailData, int DeviceInterfaceDetailDataSize, ref int RequiredSize, ref SP_DEVINFO_DATA DeviceInfoData);
 public static extern bool SetupDiSetDeviceInstallParams(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, ref SP_DEVINSTALL_PARAMS DeviceInstallParams);
 public static extern bool SetupDiBuildDriverInfoList(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, SPDIT DriverType);
 public static extern bool SetupDiEnumDriverInfo(
     IntPtr DeviceInfoSet,
     ref SP_DEVINFO_DATA DeviceInfoData,
     SPDIT DriverType,
     int MemberIndex,
     ref SP_DRVINFO_DATA DriverInfoData);
 public static extern bool SetupDiChangeState(IntPtr deviceInfoSet, [In] ref SP_DEVINFO_DATA deviceInfoData);
Ejemplo n.º 16
0
 static extern bool SetupDiCallClassInstaller(UInt32 InstallFunction, IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData);
 public static extern bool SetupDiOpenDeviceInfo(IntPtr deviceInfoSet, string deviceInstanceId, IntPtr hwndParent, UInt32 openFlags, ref SP_DEVINFO_DATA deviceInfoData);
 public static extern bool DiUninstallDevice(IntPtr hwndParent, IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, int ulFlags, out bool NeedReboot);
Ejemplo n.º 19
0
 /// <summary>
 /// Set device state.
 /// </summary>
 /// <param name="match"></param>
 /// <param name="enable"></param>
 /// <returns>Success state.</returns>
 /// <remarks>
 /// This is nearly identical to the method above except it
 /// tries to match the hardware description against the criteria
 /// passed in.  If a match is found, that device will the be
 /// enabled or disabled based on bEnable.
 /// Errors:   This method may throw the following exceptions.
 ///           Failed to enumerate device tree!
 /// </remarks>
 public static bool SetDeviceState(string deviceId, bool enable)
 {
     try
     {
         Guid myGUID = System.Guid.Empty;
         IntPtr deviceInfoSet = SetupDiGetClassDevs(myGUID, null, IntPtr.Zero, DIGCF.DIGCF_ALLCLASSES | DIGCF.DIGCF_PRESENT);
         if (deviceInfoSet.ToInt32() == INVALID_HANDLE_VALUE)
         {
             return false;
         }
         var deviceInfoData = new SP_DEVINFO_DATA();
         deviceInfoData.Initialize();
         deviceInfoData.DevInst = 0;
         deviceInfoData.ClassGuid = System.Guid.Empty;
         deviceInfoData.Reserved = IntPtr.Zero;
         for (var i = 0; SetupDiEnumDeviceInfo(deviceInfoSet, i, ref deviceInfoData); i++)
         {
             var currentDeviceId = GetDeviceId(deviceInfoData.DevInst);
             if (deviceId == currentDeviceId)
             {
                 SetDeviceState(deviceInfoSet, deviceInfoData, enable);
             }
         }
         SetupDiDestroyDeviceInfoList(deviceInfoSet);
     }
     catch (Exception ex)
     {
         throw new Exception("Failed to enumerate device tree!", ex);
         //return false;
     }
     return true;
 }
 public static extern bool SetupDiEnumDeviceInfo(IntPtr deviceInfoSet, int memberIndex, ref SP_DEVINFO_DATA deviceInfoData);
Ejemplo n.º 21
0
 public static extern bool SetupDiEnumDeviceInterfaces(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, [MarshalAs(UnmanagedType.LPStruct)]Guid interfaceClassGuid, int memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
 public static extern bool SetupDiSetSelectedDevice(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData);
Ejemplo n.º 23
0
        public static extern bool SetupDiGetDeviceRegistryProperty(
		   IntPtr DeviceInfoSet,
		   ref SP_DEVINFO_DATA DeviceInfoData,
		   uint Property,
		   out uint PropertyRegDataType,
		   byte[] PropertyBuffer,
		   uint PropertyBufferSize,
		   out uint RequiredSize
		);
 public static extern bool SetupDiSetClassInstallParams(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, SP_REMOVEDEVICE_PARAMS classInstallParams, UInt32 classInstallParamsSize);
Ejemplo n.º 25
0
 public static extern bool SetupDiRemoveDevice(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData);
Ejemplo n.º 26
0
 public static extern bool DiUninstallDevice(IntPtr hwndParent, IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, int ulFlags, out bool NeedReboot);
Ejemplo n.º 27
0
 internal static extern bool SetupDiGetDeviceInstanceId(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, IntPtr DeviceInstanceId, int DeviceInstanceIdSize, ref int RequiredSize);
Ejemplo n.º 28
0
 public static string GetDeviceDescription(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData)
 {
     var deviceDescription = GetStringPropertyForDevice(deviceInfoSet, deviceInfoData, SPDRP.SPDRP_DEVICEDESC);
     if (!string.IsNullOrEmpty(deviceDescription)) return deviceDescription.Trim();
     var deviceFriendlyName = GetStringPropertyForDevice(deviceInfoSet, deviceInfoData, SPDRP.SPDRP_FRIENDLYNAME);
     return (deviceFriendlyName ?? "").Trim();
 }
Ejemplo n.º 29
0
 public static SP_DEVINFO_DATA? GetDeviceInfo(IntPtr deviceInfoSet, string deviceInstanceId)
 {
     var da = new SP_DEVINFO_DATA();
     da.Initialize();
     var result = SetupDiOpenDeviceInfo(deviceInfoSet, deviceInstanceId, IntPtr.Zero, 0, ref da);
     if (!result) return null;
     return da;
 }
Ejemplo n.º 30
0
 public static string GetDeviceManufacturer(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData)
 {
     var deviceManufacturer = GetStringPropertyForDevice(deviceInfoSet, deviceInfoData, SPDRP.SPDRP_MFG);
     return (deviceManufacturer ?? "").Trim();
 }
Ejemplo n.º 31
0
 /// <summary>
 /// Attempts to enable or disable a device driver.
 /// </summary>
 /// <param name="deviceInfoSet">Pointer to device.</param>
 /// <param name="deviceInfoData"></param>
 /// <param name="bEnable"></param>
 /// <returns>State of success.</returns>
 /// <remarks>
 /// IMPORTANT NOTE!!!
 /// This code currently does not check the reboot flag.
 /// Some devices require you reboot the OS for the change
 /// to take affect.  If this describes your device, you 
 /// will need to look at the SDK call:
 /// SetupDiGetDeviceInstallParams.  You can call it 
 /// directly after ChangeIt to see whether or not you need 
 /// to reboot the OS for you change to go into effect.
 /// Errors:   This method may throw the following exceptions.
 ///           Unable to change device state!
 /// </remarks>
 private static bool SetDeviceState(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, bool bEnable)
 {
     try
     {
         SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER();
         header.cbSize = (UInt32)Marshal.SizeOf(header);
         header.InstallFunction = DIF_PROPERTYCHANGE;
         SP_PROPCHANGE_PARAMS classInstallParams = new SP_PROPCHANGE_PARAMS();
         classInstallParams.ClassInstallHeader = header;
         classInstallParams.StateChange = bEnable ? DICS_ENABLE : DICS_DISABLE;
         classInstallParams.Scope = DICS_FLAG_GLOBAL;
         classInstallParams.HwProfile = 0;
         var classInstallParamsSize = (UInt32)Marshal.SizeOf(classInstallParams);
         bool result = SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInfoData, classInstallParams, classInstallParamsSize);
         if (result) result = SetupDiChangeState(deviceInfoSet, ref deviceInfoData);
         if (!result)
         {
             var ex = new Win32Exception();
             SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, deviceInfoSet, ref deviceInfoData);
         }
         return result;
     }
     catch (Exception)
     {
         return false;
     }
 }
Ejemplo n.º 32
0
 public static DeviceInfo[] GetDevices(Guid classGuid, DIGCF flags, string deviceId, int vid, int pid, int rev)
 {
     lock (GetDevicesLock)
     {
         IntPtr deviceInfoSet = SetupDiGetClassDevs(classGuid, null, IntPtr.Zero, flags); //  | DIGCF.DIGCF_PRESENT
         if (deviceInfoSet.ToInt32() == ERROR_INVALID_HANDLE_VALUE)
         {
             throw new Exception("Invalid Handle");
         }
         var list = new List<DeviceInfo>();
         var deviceInfoData = new SP_DEVINFO_DATA();
         deviceInfoData.Initialize();
         int i;
         for (i = 0; SetupDiEnumDeviceInfo(deviceInfoSet, i, ref deviceInfoData); i++)
         {
             var currentDeviceId = GetDeviceId(deviceInfoData.DevInst);
             if (!string.IsNullOrEmpty(deviceId) && deviceId != currentDeviceId) continue;
             var device = GetDeviceInfo(deviceInfoSet, deviceInfoData, deviceId);
             if (vid > 0 && device.VendorId != vid) continue;
             if (pid > 0 && device.ProductId != pid) continue;
             if (rev > 0 && device.Revision != rev) continue;
             list.Add(device);
         }
         SetupDiDestroyDeviceInfoList(deviceInfoSet);
         return list.OrderBy(x => x.ClassDescription).ThenBy(x => x.Description).ToArray();
     }
 }
Ejemplo n.º 33
0
 static extern bool SetupDiSetClassInstallParams(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, SP_REMOVEDEVICE_PARAMS ClassInstallParams, UInt32 ClassInstallParamsSize);
 public static extern bool SetupDiCallClassInstaller(UInt32 installFunction, IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData);