/// <summary> /// Return device identified by instance ID/path (symbolic link). /// </summary> /// <param name="symbolicLink">The device interface path/ID/symbolic link name.</param> /// <param name="flags"> /// <see cref="DeviceLocationFlags" /> /// </param> /// <returns>A <see cref="Device" />.</returns> public static Device GetDeviceByInterfaceId(string symbolicLink, DeviceLocationFlags flags) { var instanceId = GetInstanceIdFromInterfaceId(symbolicLink); return(GetDeviceByInstanceId(instanceId, flags)); }
protected Device(string instanceId, DeviceLocationFlags flags) { InstanceId = instanceId; var iFlags = SetupApiWrapper.CM_LOCATE_DEVNODE_FLAG.CM_LOCATE_DEVNODE_NORMAL; switch (flags) { case DeviceLocationFlags.Normal: iFlags = SetupApiWrapper.CM_LOCATE_DEVNODE_FLAG.CM_LOCATE_DEVNODE_NORMAL; break; case DeviceLocationFlags.Phantom: iFlags = SetupApiWrapper.CM_LOCATE_DEVNODE_FLAG.CM_LOCATE_DEVNODE_PHANTOM; break; case DeviceLocationFlags.CancelRemove: iFlags = SetupApiWrapper.CM_LOCATE_DEVNODE_FLAG.CM_LOCATE_DEVNODE_CANCELREMOVE; break; } var ret = SetupApiWrapper.CM_Locate_DevNode( ref _instanceHandle, instanceId, iFlags ); if (ret == SetupApiWrapper.ConfigManagerResult.NoSuchDevinst) { throw new ArgumentException("The supplied instance wasn't found.", nameof(flags)); } if (ret != SetupApiWrapper.ConfigManagerResult.Success) { throw new Win32Exception(Marshal.GetLastWin32Error()); } uint nBytes = 256; var ptrInstanceBuf = Marshal.AllocHGlobal((int)nBytes); try { ret = SetupApiWrapper.CM_Get_Device_ID( _instanceHandle, ptrInstanceBuf, nBytes, 0 ); if (ret != SetupApiWrapper.ConfigManagerResult.Success) { throw new Win32Exception(Marshal.GetLastWin32Error()); } DeviceId = (Marshal.PtrToStringUni(ptrInstanceBuf) ?? string.Empty).ToUpper(); } finally { if (ptrInstanceBuf != IntPtr.Zero) { Marshal.FreeHGlobal(ptrInstanceBuf); } } }
/// <summary> /// Return device identified by instance ID. /// </summary> /// <param name="instanceId">The instance ID of the device.</param> /// <param name="flags"> /// <see cref="DeviceLocationFlags" /> /// </param> /// <returns>A <see cref="Device" />.</returns> public static Device GetDeviceByInstanceId(string instanceId, DeviceLocationFlags flags) { return(new Device(instanceId, flags)); }