Click here to Skip to main content
15,881,715 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to generate random decimal numbers in C# Pin
Patrice T14-Aug-20 22:33
mvePatrice T14-Aug-20 22:33 
AnswerRe: How to generate random decimal numbers in C# Pin
BillWoodruff19-Aug-20 1:31
professionalBillWoodruff19-Aug-20 1:31 
GeneralRe: How to generate random decimal numbers in C# Pin
jsc4219-Aug-20 6:41
professionaljsc4219-Aug-20 6:41 
QuestionReading Multiple files Pin
lalgourav11-Aug-20 5:45
lalgourav11-Aug-20 5:45 
AnswerRe: Reading Multiple files Pin
Gerry Schmitz11-Aug-20 6:25
mveGerry Schmitz11-Aug-20 6:25 
AnswerRe: Reading Multiple files Pin
OriginalGriff11-Aug-20 6:29
mveOriginalGriff11-Aug-20 6:29 
AnswerRe: Reading Multiple files Pin
Eddy Vluggen13-Aug-20 12:20
professionalEddy Vluggen13-Aug-20 12:20 
QuestionGet if Device is enabled or Disabled in C# Pin
XxKeldecknightxX10-Aug-20 5:52
XxKeldecknightxX10-Aug-20 5:52 
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!!

C#
public static bool GetDeviceState(Func<string, bool> 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_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
        }

  //https://stackoverflow.com/questions/45045670/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);
        }

AnswerRe: Get if Device is enabled or Disabled in C# Pin
Luc Pattyn10-Aug-20 10:44
sitebuilderLuc Pattyn10-Aug-20 10:44 
GeneralRe: Get if Device is enabled or Disabled in C# Pin
Luc Pattyn10-Aug-20 12:02
sitebuilderLuc Pattyn10-Aug-20 12:02 
AnswerRe: Get if Device is enabled or Disabled in C# Pin
Eddy Vluggen10-Aug-20 11:57
professionalEddy Vluggen10-Aug-20 11:57 
QuestionThe provider is not compatible with the version of Oracle client Pin
Member 148339589-Aug-20 22:57
Member 148339589-Aug-20 22:57 
AnswerRe: The provider is not compatible with the version of Oracle client Pin
OriginalGriff9-Aug-20 23:07
mveOriginalGriff9-Aug-20 23:07 
GeneralRe: The provider is not compatible with the version of Oracle client Pin
Member 1483395810-Aug-20 1:13
Member 1483395810-Aug-20 1:13 
SuggestionRe: The provider is not compatible with the version of Oracle client Pin
Richard Deeming10-Aug-20 1:46
mveRichard Deeming10-Aug-20 1:46 
GeneralRe: The provider is not compatible with the version of Oracle client Pin
Member 1483395810-Aug-20 2:03
Member 1483395810-Aug-20 2:03 
GeneralRe: The provider is not compatible with the version of Oracle client Pin
Richard MacCutchan10-Aug-20 3:26
mveRichard MacCutchan10-Aug-20 3:26 
AnswerRe: The provider is not compatible with the version of Oracle client Pin
Eddy Vluggen10-Aug-20 12:04
professionalEddy Vluggen10-Aug-20 12:04 
GeneralRe: The provider is not compatible with the version of Oracle client Pin
OriginalGriff10-Aug-20 19:36
mveOriginalGriff10-Aug-20 19:36 
GeneralRe: The provider is not compatible with the version of Oracle client Pin
Richard Deeming10-Aug-20 21:47
mveRichard Deeming10-Aug-20 21:47 
GeneralRe: The provider is not compatible with the version of Oracle client Pin
OriginalGriff10-Aug-20 21:54
mveOriginalGriff10-Aug-20 21:54 
GeneralRe: The provider is not compatible with the version of Oracle client Pin
Jörgen Andersson12-Aug-20 9:17
professionalJörgen Andersson12-Aug-20 9:17 
QuestionNeed to add a new visual studio solution to an existing visual studio solution Git repository Pin
syedripon6-Aug-20 6:43
syedripon6-Aug-20 6:43 
AnswerRe: Need to add a new visual studio solution to an existing visual studio solution Git repository Pin
Dave Kreskowiak6-Aug-20 7:05
mveDave Kreskowiak6-Aug-20 7:05 
AnswerRe: Need to add a new visual studio solution to an existing visual studio solution Git repository Pin
OriginalGriff6-Aug-20 7:58
mveOriginalGriff6-Aug-20 7:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.