Example #1
0
 internal static extern bool DriverStoreEnumObjects(
     IntPtr hDriverStore,
     DriverStoreObjectType objectType,
     DRIVERSTORE_LOCK_LEVEL flags,
     EnumObjectsDelegate callbackRoutine,
     IntPtr lParam
     );
        public void EnumObjects(EnumObjectsDelegate callback)
        {
            NtStatus status;
            int      context   = 0;
            bool     firstTime = true;
            int      retLength;

            using (var data = new MemoryAlloc(0x200))
            {
                while (true)
                {
                    while ((status = Win32.NtQueryDirectoryObject(
                                this,
                                data,
                                data.Size,
                                false,
                                firstTime,
                                ref context,
                                out retLength
                                )) == NtStatus.MoreEntries)
                    {
                        // Check if we have at least one entry. If not,
                        // we need to double the buffer size and try again.
                        if (data.ReadStruct <ObjectDirectoryInformation>(0).Name.Buffer != IntPtr.Zero)
                        {
                            break;
                        }

                        if (data.Size > 16 * 1024 * 1024)
                        {
                            Win32.ThrowLastError(status);
                        }

                        data.Resize(data.Size * 2);
                    }

                    if (status >= NtStatus.Error)
                    {
                        Win32.ThrowLastError(status);
                    }

                    int i = 0;

                    while (true)
                    {
                        ObjectDirectoryInformation info = data.ReadStruct <ObjectDirectoryInformation>(i);

                        if (info.Name.Buffer == IntPtr.Zero)
                        {
                            break;
                        }

                        if (!callback(new ObjectEntry(info.Name.Read(), info.TypeName.Read())))
                        {
                            return;
                        }

                        i++;
                    }

                    if (status != NtStatus.MoreEntries)
                    {
                        break;
                    }

                    firstTime = false;
                }
            }
        }