Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Get if Device is enabled or Disabled in C#

Get if Device is enabled or Disabled in C#

Scheduled Pinned Locked Moved C#
csharpsharepointcomalgorithmsdebugging
4 Posts 3 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    XxKeldecknightxX
    wrote on last edited by
    #1

    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 :\ Thank you!!

    public static bool GetDeviceState(Func filter) //https://www.codeproject.com/script/Content/ViewAssociatedFile.aspx?rzp=%2FKB%2Fsystem%2FDevMgr%2Fdevmgr-src.zip&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\_
    
    L L 2 Replies Last reply
    0
    • X XxKeldecknightxX

      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 :\ Thank you!!

      public static bool GetDeviceState(Func filter) //https://www.codeproject.com/script/Content/ViewAssociatedFile.aspx?rzp=%2FKB%2Fsystem%2FDevMgr%2Fdevmgr-src.zip&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\_
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I'm not familiar with most of the functions you're using, but maybe this helps: I once was interested in the enable/disable status of my Ethernet connection, as set in the Control Panel Network Connections (which might be different from the choice offered in Device Manager). I used WMI (class Win32_NetworkAdapter), and checked property NetConnectionStatus which was zero for disabled and non-zero (7 IIRC) for enabled. :)

      Luc Pattyn [My Articles] If you can't find it on YouTube try TikTok...

      L 1 Reply Last reply
      0
      • X XxKeldecknightxX

        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 :\ Thank you!!

        public static bool GetDeviceState(Func filter) //https://www.codeproject.com/script/Content/ViewAssociatedFile.aspx?rzp=%2FKB%2Fsystem%2FDevMgr%2Fdevmgr-src.zip&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\_
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Luc is being modest. If he says he got a better idea, you better follow it. Not every device is complete in terms of device-drivers. I have a mountain of webcams that don't work under Linux to prove so.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

        1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, I'm not familiar with most of the functions you're using, but maybe this helps: I once was interested in the enable/disable status of my Ethernet connection, as set in the Control Panel Network Connections (which might be different from the choice offered in Device Manager). I used WMI (class Win32_NetworkAdapter), and checked property NetConnectionStatus which was zero for disabled and non-zero (7 IIRC) for enabled. :)

          Luc Pattyn [My Articles] If you can't find it on YouTube try TikTok...

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Here is fresh code that works for me:

          using System;
          using System.Management;

          namespace LP_Core {
          public class LP_Ethernet {
          public static string GetState() {
          string query = "SELECT NetConnectionStatus FROM Win32_NetworkAdapter "+
          "WHERE NetConnectionID='Ethernet'";
          ManagementObjectSearcher ethernets = new ManagementObjectSearcher(query);
          foreach (ManagementObject ethernet in ethernets.Get()) {
          switch (Convert.ToInt32(ethernet["NetConnectionStatus"])) {
          // constants: https://docs.microsoft.com/en-us/dotnet/
          // api/microsoft.powershell.commands.netconnectionstatus
          case 0:
          return "disabled";
          case 7:
          return "enabled but disconnected";
          case 2:
          return "enabled and connected";
          default:
          return "unknown";
          }
          }
          return "absent";
          }
          }
          }

          :)

          Luc Pattyn [My Articles] If you can't find it on YouTube try TikTok...

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups