Exemplo n.º 1
0
        /// <summary>
        /// Internal load icon.
        /// </summary>
        /// <param name="ptr">The pointer to load.</param>
        /// <returns>True if successful.</returns>
        /// <remarks></remarks>
        private bool _internalLoadFromPtr(IntPtr ptr)
        {
            bool _internalLoadFromPtrRet = default;

            // get the icon file header directory.
            MemPtr mm = ptr;

            _dir = mm.ToStruct <ICONDIR>();
            int i;
            int c    = _dir.nImages - 1;
            int f    = Marshal.SizeOf <ICONDIRENTRY>();
            int e    = Marshal.SizeOf <ICONDIR>();
            var optr = ptr;

            if (_dir.nImages <= 0 || _dir.wReserved != 0 || 0 == (int)(_dir.wIconType & IconImageType.IsValid))
            {
                return(false);
            }

            _entries = new List <IconImageEntry>();
            mm       = mm + e;

            for (i = 0; i < c; i++)
            {
                // load all images in sequence.
                _entries.Add(new IconImageEntry(mm.ToStruct <ICONDIRENTRY>(), optr));
                ptr = ptr + f;
            }

            _internalLoadFromPtrRet = true;
            return(_internalLoadFromPtrRet);
        }
Exemplo n.º 2
0
        /// <summary>
        /// For Windows 8, retrieves the user's Microsoft login account information.
        /// </summary>
        /// <param name="machine">Computer on which to perform the enumeration.  If this parameter is null, the local machine is assumed.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static UserInfo24[] EnumUsers24(string machine = null)
        {
            try
            {
                MemPtr rh = IntPtr.Zero;

                int i     = 0;
                var uorig = EnumUsers11();

                UserInfo24[] usas;

                int c = uorig.Length;

                usas = new UserInfo24[c];

                for (i = 0; i < c; i++)
                {
                    NetInfo.NetUserGetInfo(machine, uorig[i].Name, 24, ref rh);
                    usas[i] = rh.ToStruct <UserInfo24>();

                    rh.NetFree();
                }

                return(usas);
            }
            catch
            {
                throw new NativeException();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets network information for the specified computer.
        /// </summary>
        /// <param name="computer">Computer for which to retrieve the information.</param>
        /// <param name="info">A ServerInfo101 structure that receives the information.</param>
        /// <remarks></remarks>
        public static void GetServerInfo(string computer, ref ServerInfo101 info)
        {
            var mm = new MemPtr();

            NetInfo.NetServerGetInfo(computer, 101, ref mm);
            info = mm.ToStruct <ServerInfo101>();
            mm.NetFree();
        }
Exemplo n.º 4
0
        static SysInfo()
        {
            // let's get some version information!
            var mm = new MemPtr();

            GetNativeSystemInfo(ref nativeEnv);

            _memInfo.dwLength = Marshal.SizeOf(_memInfo);
            GlobalMemoryStatusEx(ref _memInfo);

            // now let's figure out how many processors we have on this system
            MemPtr org;

            var lp = new SystemLogicalProcessorInformation();

            SystemLogicalProcessorInformation[] rets;

            int i;
            int c;

            // The maximum number of processors for any version of Windows is 128, we'll allocate more for extra information.
            mm.Alloc(Marshal.SizeOf(lp) * 1024);

            // record the original memory pointer

            org = mm;
            var lRet = (int)mm.Length;

            GetLogicalProcessorInformation(mm, ref lRet);

            c = (int)(lRet / (double)Marshal.SizeOf(lp));

            rets          = new SystemLogicalProcessorInformation[c];
            nativeEnv.nop = 0;

            for (i = 0; i < c; i++)
            {
                rets[i] = mm.ToStruct <SystemLogicalProcessorInformation>();
                mm     += Marshal.SizeOf(lp);

                // what we're really after are the number of cores.
                if (rets[i].Relationship == LogicalProcessorRelationship.RelationProcessorCore)
                {
                    nativeEnv.nop++;
                }
            }

            // store that data in case we need it for later.
            _procRaw = rets;

            // free our unmanaged resources.
            org.Free();
        }
Exemplo n.º 5
0
        public static SECURITY_DESCRIPTOR_REAL SecurityDescriptorToReal(SECURITY_DESCRIPTOR sd)
        {
            SECURITY_DESCRIPTOR_REAL SecurityDescriptorToRealRet = default;
            var    sr    = new SECURITY_DESCRIPTOR_REAL();
            MemPtr msacl = sd.Sacl;
            MemPtr mdacl = sd.Dacl;

            sr.Sacl = msacl.ToStruct <ACL>();
            sr.Dacl = mdacl.ToStruct <ACL>();
            SecurityDescriptorToRealRet = sr;
            return(SecurityDescriptorToRealRet);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets the members of the specified local group on the specified machine.
        /// </summary>
        /// <param name="computer">The computer for which to retrieve the information.</param>
        /// <param name="group">The name of the group to enumerate.</param>
        /// <param name="SidType">The type of group members to return.</param>
        /// <returns>A list of group member names.</returns>
        /// <remarks></remarks>
        public static string[] LocalGroupUsers(string computer, string group, SidUsage SidType = SidUsage.SidTypeUser)
        {
            var mm = new MemPtr();

            MemPtr op = new MemPtr();

            int x   = 0;
            int cbt = 0;
            int cb  = 0;

            string[] s = null;

            try
            {
                var inul = new IntPtr();
                if (NetInfo.NetLocalGroupGetMembers(computer, group, 1, ref mm, -1, ref cb, ref cbt, ref inul) == NET_API_STATUS.NERR_Success)
                {
                    if (cb == 0)
                    {
                        mm.NetFree();
                        return(null);
                    }

                    op = mm;

                    UserLocalGroup1 z;
                    int             i;

                    s = new string[cb];

                    for (i = 0; i < cb; i++)
                    {
                        z = mm.ToStruct <UserLocalGroup1>();
                        if (z.SidUsage == SidType)
                        {
                            s[x] = z.Name;
                            mm   = mm + Marshal.SizeOf(z);
                            x   += 1;
                        }
                    }

                    Array.Resize(ref s, x);
                }
            }
            catch
            {
                throw new NativeException();
            }

            op.NetFree();
            return(s);
        }
Exemplo n.º 7
0
        public static SECURITY_DESCRIPTOR StringToSecurityDescriptor(string strSD)
        {
            SECURITY_DESCRIPTOR StringToSecurityDescriptorRet = default;
            MemPtr ptr = IntPtr.Zero;
            uint   ls  = 0U;
            SECURITY_DESCRIPTOR sd;
            IntPtr argSecurityDescriptor = ptr;

            SecurityDescriptor.ConvertStringSecurityDescriptorToSecurityDescriptor(strSD, 1U, ref argSecurityDescriptor, ref ls);
            sd = ptr.ToStruct <SECURITY_DESCRIPTOR>();
            ptr.LocalFree();
            StringToSecurityDescriptorRet = sd;
            return(StringToSecurityDescriptorRet);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Create a new image from the pointer.
        /// </summary>
        /// <param name="ptr">Pointer to the start of the ICONDIRENTRY structure.</param>
        /// <remarks></remarks>
        internal IconImageEntry(IntPtr ptr)
        {
            MemPtr mm = ptr;

            _entry = mm.ToStruct <ICONDIRENTRY>();
            ptr    = ptr + _entry.dwOffset;
            if (_entry.wBitsPixel < 24)
            {
                // Throw New InvalidDataException("Reading low-bit icons is not supported")
            }

            _image = new byte[_entry.dwImageSize];
            Marshal.Copy(ptr, _image, 0, _entry.dwImageSize);

            // MemCpy(_image, ptr, _entry.dwImageSize)
        }
Exemplo n.º 9
0
        public static NETRESOURCE[] DoEnum(IntPtr lpNet)
        {
            NETRESOURCE[] DoEnumRet = default;
            MemPtr        mm        = new MemPtr();
            int           cb        = 10240;

            NETRESOURCE[] bb    = null;
            NETRESOURCE[] nin   = null;
            IntPtr        hEnum = IntPtr.Zero;
            int           e     = 0;

            e = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_ALL, lpNet, ref hEnum);
            if (e != NO_ERROR)
            {
                return(null);
            }

            e = 0;
            mm.ReAlloc(10240L);
            int arglpcCount = 1;

            while (WNetEnumResource(hEnum, ref arglpcCount, mm, ref cb) == NO_ERROR)
            {
                Array.Resize(ref bb, e + 1);
                nin   = DoEnum(mm.Handle);
                bb[e] = mm.ToStruct <NETRESOURCE>();
                if (nin is object)
                {
                    bb  = WNACat(bb, nin);
                    nin = null;
                }

                if (bb is object)
                {
                    e = bb.Length;
                }
                else
                {
                    e = 0;
                }
            }

            mm.Free();
            WNetCloseEnum(hEnum);
            DoEnumRet = bb;
            return(DoEnumRet);
        }
Exemplo n.º 10
0
        static SysInfo()
        {
            // let's get some version information!

            _memInfo.dwLength = Marshal.SizeOf(_memInfo);
            GetNativeSystemInfo(ref _sysInfo);
            GlobalMemoryStatusEx(ref _memInfo);

            // now let's figure out how many processors we have on this system
            var    mm = new MemPtr();
            MemPtr org;
            var    lp   = new SYSTEM_LOGICAL_PROCESSOR_INFORMATION();
            int    lRet = 0;

            SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] rets;
            int i;
            int c;

            // The maximum number of processors for any version of Windows is 128, we'll allocate more for extra information.
            mm.Alloc(Marshal.SizeOf(lp) * 1024);

            // record the original memory pointer
            org  = mm;
            lRet = (int)mm.Length;
            GetLogicalProcessorInformation(mm, ref lRet);
            c    = (int)(lRet / (double)Marshal.SizeOf(lp));
            rets = new SYSTEM_LOGICAL_PROCESSOR_INFORMATION[c];
            var loopTo = c - 1;

            for (i = 0; i <= loopTo; i++)
            {
                rets[i] = mm.ToStruct <SYSTEM_LOGICAL_PROCESSOR_INFORMATION>();
                mm     += Marshal.SizeOf(lp);

                // what we're really after are the number of cores.
                if (rets[i].Relationship == LOGICAL_PROCESSOR_RELATIONSHIP.RelationProcessorCore)
                {
                    _sysInfo.dwNumberOfProcessors += 1;
                }
            }

            // store that data in case we need it for later.
            _procRaw = rets;

            // free our unmanaged resources.
            org.Free();
        }
Exemplo n.º 11
0
        /// <summary>
        /// Retrieves the disk geometry of the specified disk.
        /// </summary>
        /// <param name="hfile">Handle to a valid, open disk.</param>
        /// <param name="geo">Receives the disk geometry information.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static bool DiskGeometry(IntPtr hfile, ref DISK_GEOMETRY_EX geo)
        {
            if (hfile == DevProp.INVALID_HANDLE_VALUE)
            {
                return(false);
            }
            MemPtr mm = new MemPtr();
            uint   l  = 0U;
            uint   cb = 0U;

            l = (uint)Marshal.SizeOf <DISK_GEOMETRY_EX>();
            mm.Alloc(l);
            NativeDisk.DeviceIoControl(hfile, NativeDisk.IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, IntPtr.Zero, 0, mm.Handle, l, ref cb, IntPtr.Zero);
            geo = mm.ToStruct <DISK_GEOMETRY_EX>();
            mm.Free();
            return(true);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Enumerate users into a USER_INFO_11 structure.
        /// </summary>
        /// <param name="machine">Computer on which to perform the enumeration.  If this parameter is null, the local machine is assumed.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static UserInfo11[] EnumUsers11(string machine = null)
        {
            try
            {
                int cb = 0;
                int er = 0;

                MemPtr rh = IntPtr.Zero;

                int te = 0;

                MemPtr       buff = new MemPtr();
                UserInfo11[] usas;

                try
                {
                    cb = Marshal.SizeOf <UserInfo11>();
                }
                catch
                {
                    //Interaction.MsgBox(ex.Message + "\r\n" + "\r\n" + "Stack Trace: " + ex.StackTrace, MsgBoxStyle.Exclamation);
                }

                var inul = new IntPtr();
                var err  = NetInfo.NetUserEnum(machine, 11, 0, ref buff, -1, ref er, ref te, ref inul);

                rh   = buff;
                usas = new UserInfo11[te];

                for (int i = 0; i < te; i++)
                {
                    usas[i]            = buff.ToStruct <UserInfo11>();
                    usas[i].LogonHours = IntPtr.Zero;

                    buff = buff + cb;
                }

                rh.NetFree();
                return(usas);
            }
            catch
            {
                throw new NativeException();
            }
        }
Exemplo n.º 13
0
 /// <summary>
 /// Read a structure of type T from a stream
 /// </summary>
 /// <param name="stream"><see cref="Stream"/> object</param>
 /// <param name="struct">The output struct of type T</param>
 /// <returns>True if successful</returns>
 public static bool ReadStruct <T>(Stream stream, ref T @struct) where T : struct
 {
     try
     {
         int    a = Marshal.SizeOf <T>();
         byte[] b;
         b = new byte[a];
         stream.Read(b, 0, a);
         var    gch = GCHandle.Alloc(b, GCHandleType.Pinned);
         MemPtr mm  = gch.AddrOfPinnedObject();
         @struct = mm.ToStruct <T>();
         gch.Free();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Exemplo n.º 14
0
        /// <summary>
        /// Enumerates users of a given group.
        /// </summary>
        /// <param name="computer">Computer for which to retrieve the information.</param>
        /// <param name="group">Group to enumerate.</param>
        /// <returns>An array of user names.</returns>
        /// <remarks></remarks>
        public static string[] GroupUsers(string computer, string group)
        {
            var mm = new MemPtr();

            MemPtr op = new MemPtr();

            int cbt = 0;
            int cb  = 0;

            string[] s = null;

            try
            {
                var inul = new IntPtr();

                if (NetInfo.NetGroupGetUsers(computer, group, 0, ref mm, -1, ref cb, ref cbt, ref inul) == NET_API_STATUS.NERR_Success)
                {
                    op = mm;
                    UserGroup0 z;
                    int        i;

                    s = new string[cb];

                    for (i = 0; i < cb; i++)
                    {
                        z    = mm.ToStruct <UserGroup0>();
                        s[i] = z.Name;

                        mm = mm + Marshal.SizeOf <UserGroup0>();
                    }
                }
            }
            catch
            {
                throw new NativeException();
            }

            op.NetFree();

            return(s);
        }
Exemplo n.º 15
0
            public static VOLUME_DISK_EXTENTS FromPtr(IntPtr ptr)
            {
                VOLUME_DISK_EXTENTS FromPtrRet = default;
                var    ve = new VOLUME_DISK_EXTENTS();
                int    cb = Marshal.SizeOf <DISK_EXTENT>();
                MemPtr m  = ptr;
                int    i;

                ve.NumberOfExtents = m.IntAt(0L);
                ve.Space           = m.IntAt(1L);
                ve.Extents         = new DISK_EXTENT[ve.NumberOfExtents];
                m = m + 8;
                var c = ve.NumberOfExtents;

                for (i = 0; i < c; i++)
                {
                    ve.Extents[i] = m.ToStruct <DISK_EXTENT>();
                    m             = m + cb;
                }

                FromPtrRet = ve;
                return(FromPtrRet);
            }
Exemplo n.º 16
0
        /// <summary>
        /// Construct an icon from the raw image data.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        private Icon _constructIcon()
        {
            Icon _constructIconRet = default;

            if (_hIcon != IntPtr.Zero)
            {
                User32.DestroyIcon(_hIcon);
                _hIcon = IntPtr.Zero;
            }

            MemPtr mm  = (MemPtr)_image;
            var    bmp = mm.ToStruct <BITMAPINFOHEADER>();

            IntPtr hBmp;
            IntPtr ptr;
            IntPtr ppBits = new IntPtr();

            var lpicon = default(ICONINFO);

            IntPtr hicon;
            IntPtr hBmpMask = new IntPtr();

            bool hasMask;

            if (bmp.biHeight == bmp.biWidth * 2)
            {
                hasMask      = true;
                bmp.biHeight = (int)(bmp.biHeight / 2d);
            }
            else
            {
                hasMask = false;
            }

            bmp.biSizeImage = (int)(bmp.biWidth * bmp.biHeight * (bmp.biBitCount / 8d));

            bmp.biXPelsPerMeter = (int)(24.5d * 1000d);
            bmp.biYPelsPerMeter = (int)(24.5d * 1000d);

            bmp.biClrUsed      = 0;
            bmp.biClrImportant = 0;
            bmp.biPlanes       = 1;

            Marshal.StructureToPtr(bmp, mm.Handle, false);

            ptr = mm.Handle + bmp.biSize;

            if (bmp.biSize != 40)
            {
                return(null);
            }

            hBmp = User32.CreateDIBSection(IntPtr.Zero, mm.Handle, 0U, ref ppBits, IntPtr.Zero, 0);

            Native.MemCpy(ptr, ppBits, bmp.biSizeImage);

            if (hasMask)
            {
                ptr             = ptr + bmp.biSizeImage;
                bmp.biBitCount  = 1;
                bmp.biSizeImage = 0;
                Marshal.StructureToPtr(bmp, mm.Handle, false);
                hBmpMask = User32.CreateDIBSection(IntPtr.Zero, mm.Handle, 0U, ref ppBits, IntPtr.Zero, 0);
                Native.MemCpy(ptr, ppBits, (long)(Math.Max(bmp.biWidth, 32) * bmp.biHeight / 8d));
            }

            lpicon.fIcon    = 1;
            lpicon.hbmColor = hBmp;
            lpicon.hbmMask  = hBmpMask;
            hicon           = User32.CreateIconIndirect(ref lpicon);
            NativeShell.DeleteObject(hBmp);
            if (hasMask)
            {
                NativeShell.DeleteObject(hBmpMask);
            }
            _constructIconRet = Icon.FromHandle(hicon);
            _hIcon            = hicon;
            mm.Free();
            return(_constructIconRet);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Enumerate all display monitors.
        /// </summary>
        /// <returns>An array of PrinterDeviceInfo objects.</returns>
        /// <remarks></remarks>
        public static MonitorDeviceInfo[] EnumMonitors()
        {
            var minf = _internalEnumerateDevices <MonitorDeviceInfo>(DevProp.GUID_DEVINTERFACE_MONITOR, ClassDevFlags.DeviceInterface | ClassDevFlags.Present);
            var mon  = new Monitors();

            DISPLAY_DEVICE dd;

            dd.cb = (uint)Marshal.SizeOf <DISPLAY_DEVICE>();
            var mm = new MemPtr();

            mm.Alloc(dd.cb);
            mm.UIntAt(0) = dd.cb;

            if (minf is object && minf.Count() > 0)
            {
                foreach (var x in minf)
                {
                    foreach (var y in mon)
                    {
                        if (MultiMon.EnumDisplayDevices(y.DevicePath, 0, mm, MultiMon.EDD_GET_DEVICE_INTERFACE_NAME))
                        {
                            dd = mm.ToStruct <DISPLAY_DEVICE>();
                            DEVMODE dev = new DEVMODE();

                            dev.dmSize        = (ushort)Marshal.SizeOf <DEVMODE>();
                            dev.dmDriverExtra = 0;

                            var mm2 = new MemPtr(65535 + dev.dmSize);



                            var b = MultiMon.EnumDisplaySettingsEx(y.DevicePath, 0xffffffff, ref dev, 0);
                            if (!b)
                            {
                                var s = NativeErrorMethods.FormatLastError();
                            }


                            mm2.Free();

                            if (dd.DeviceID.ToUpper() == x.DevicePath.ToUpper())
                            {
                                x.Source = y;
                                break;
                            }
                        }
                    }
                }
            }

            mm.Free();

            if (minf is null)
            {
                return(null);
            }

            Array.Sort(minf, new Comparison <MonitorDeviceInfo>((x, y) => { if (x.FriendlyName is object && y.FriendlyName is object)
                                                                            {
                                                                                return(string.Compare(x.FriendlyName, y.FriendlyName));
                                                                            }
                                                                            else
                                                                            {
                                                                                return(string.Compare(x.Description, y.Description));
                                                                            } }));
            return(minf);
        }