/// <summary> /// Enumerates all computers visible to the specified computer on the specified domain. /// </summary> /// <param name="computer">Optional computer name. The local computer is assumed if this parameter is null.</param> /// <param name="domain">Optional domain name. The primary domain of the specified computer is assumed if this parameter is null.</param> /// <returns>An array of ServerInfo1 objects.</returns> /// <remarks></remarks> public static ServerInfo101[] EnumServers() { MemPtr adv; var mm = new MemPtr(); int en = 0; int ten = 0; ServerInfo101[] servers; int i; int c; var inul = new IntPtr(); NetServerEnum(null, 101, ref mm, -1, ref en, ref ten, ServerTypes.WindowsNT, null, ref inul); adv = mm; c = ten; servers = new ServerInfo101[c + 1]; for (i = 0; i < c; i++) { servers[i] = adv.ToStruct <ServerInfo101>(); adv = adv + Marshal.SizeOf <ServerInfo101>(); } mm.NetFree(); return(servers); }
/// <summary> /// Returns the raw byte data for a Hid feature code. /// </summary> /// <param name="featureCode">The Hid feature code to retrieve.</param> /// <param name="result">Receives the result of the operation.</param> /// <param name="expectedSize">The expected size, in bytes, of the result.</param> /// <returns>True if successful.</returns> /// <remarks></remarks> public bool HidGetFeature(byte featureCode, ref byte[] result, int expectedSize) { bool HidGetFeatureRet = default; var hfile = HidFeatures.OpenHid(this); if (hfile == IntPtr.Zero) { return(false); } var mm = new MemPtr(); mm.Alloc(expectedSize + 1); mm.ByteAt(0L) = featureCode; if (!UsbLibHelpers.HidD_GetFeature(hfile, mm, expectedSize)) { HidGetFeatureRet = false; } else { HidGetFeatureRet = true; result = mm.ToByteArray(1L, expectedSize); } HidFeatures.CloseHid(hfile); mm.Free(); return(HidGetFeatureRet); }
/// <summary> /// Sets the long value of a Hid feature code. /// </summary> /// <param name="featureCode">The Hid feature code to set.</param> /// <param name="value">The value to set.</param> /// <returns></returns> /// <remarks></remarks> public bool HidSetFeature(byte featureCode, long value) { bool HidSetFeatureRet = default; var hfile = HidFeatures.OpenHid(this); if (hfile == IntPtr.Zero) { return(false); } var mm = new MemPtr(); mm.Alloc(9L); mm.ByteAt(0L) = featureCode; mm.LongAtAbsolute(1L) = value; if (!UsbLibHelpers.HidD_SetFeature(hfile, mm, 9)) { HidSetFeatureRet = false; } else { HidSetFeatureRet = true; } HidFeatures.CloseHid(hfile); mm.Free(); return(HidSetFeatureRet); }
/// <summary> /// Creates a System.Drawing.Bitmap image from a WPF source. /// </summary> /// <param name="source"></param> /// <returns></returns> /// <remarks></remarks> public static Bitmap MakeBitmapFromWPF(System.Windows.Media.Imaging.BitmapSource source) { var mm = new MemPtr(); Bitmap bmp = null; if (System.Windows.Application.Current is object) { System.Windows.Application.Current.Dispatcher.Invoke(() => { bmp = new Bitmap(source.PixelWidth, source.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); mm.Alloc(bmp.Width * bmp.Height * 4); var bm = new System.Drawing.Imaging.BitmapData(); bm.Scan0 = mm.Handle; bm.Stride = bmp.Width * 4; bm = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite | System.Drawing.Imaging.ImageLockMode.UserInputBuffer, System.Drawing.Imaging.PixelFormat.Format32bppArgb, bm); source.CopyPixels(System.Windows.Int32Rect.Empty, mm.Handle, (int)mm.Length, bmp.Width * 4); bmp.UnlockBits(bm); mm.Free(); }); } else { bmp = new Bitmap(source.PixelWidth, source.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); mm.Alloc(bmp.Width * bmp.Height * 4); var bm = new System.Drawing.Imaging.BitmapData(); bm.Scan0 = mm.Handle; bm.Stride = bmp.Width * 4; bm = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite | System.Drawing.Imaging.ImageLockMode.UserInputBuffer, System.Drawing.Imaging.PixelFormat.Format32bppArgb, bm); source.CopyPixels(System.Windows.Int32Rect.Empty, mm.Handle, (int)mm.Length, bmp.Width * 4); bmp.UnlockBits(bm); mm.Free(); } return(bmp); }
/// <summary> /// Returns the short value of a Hid feature code. /// </summary> /// <param name="featureCode">The Hid feature code to retrieve.</param> /// <param name="result">Receives the result of the operation.</param> /// <returns>True if successful.</returns> /// <remarks></remarks> public bool HidGetFeature(byte featureCode, ref short result) { bool HidGetFeatureRet = default; var hfile = HidFeatures.OpenHid(this); if (hfile == IntPtr.Zero) { return(false); } var mm = new MemPtr(); mm.Alloc(3L); mm.ByteAt(0L) = featureCode; if (!UsbLibHelpers.HidD_GetFeature(hfile, mm, 3)) { HidGetFeatureRet = false; } else { HidGetFeatureRet = true; result = mm.ShortAtAbsolute(1L); } mm.Free(); HidFeatures.CloseHid(hfile); return(HidGetFeatureRet); }
public static extern bool GetPhysicalMonitorsFromHMONITOR ( IntPtr hMonitor, int dwPhysicalMonitorArraySize, MemPtr pPhysicalMonitorArray );
/// <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(); } }
/// <summary> /// Get all mount points for a volume. /// </summary> /// <param name="path">Volume Guid Path.</param> /// <returns>An array of strings that represent mount points.</returns> /// <remarks></remarks> public static string[] GetVolumePaths(string path) { string[] GetVolumePathsRet = default; int cc = 1024; int retc = 0; var mm = new MemPtr(); bool r; mm.Alloc(cc); r = NativeDisk.GetVolumePathNamesForVolumeName(path, mm.Handle, cc, ref retc); if (!r) { return(null); } if (retc > 1024) { mm.ReAlloc(retc); r = NativeDisk.GetVolumePathNamesForVolumeName(path, mm, retc, ref retc); } GetVolumePathsRet = mm.GetStringArray(0L); mm.Free(); return(GetVolumePathsRet); }
public static extern bool EnumDisplayDevices( [MarshalAs(UnmanagedType.LPWStr)] string lpDevice, uint iDevNum, MemPtr lpDisplayDevice, int dwFlags );
/// <summary> /// Retrieves a feature from the device. /// </summary> /// <param name="device"></param> /// <param name="code"></param> /// <param name="datalen"></param> /// <returns></returns> /// <remarks></remarks> public static HIDFeatureResult GetHIDFeature(IntPtr device, int code, int datalen = 16) { HIDFeatureResult GetHIDFeatureRet = default; MemPtr mm = new MemPtr(); int i = code; try { mm.AllocZero(datalen); mm.ByteAt(0L) = (byte)i; if (UsbLibHelpers.HidD_GetFeature(device, mm.Handle, (int)mm.Length)) { GetHIDFeatureRet = new HIDFeatureResult(i, mm); } else { GetHIDFeatureRet = null; } mm.Free(); } catch { mm.Free(); return(null); } return(GetHIDFeatureRet); }
/// <summary> /// Enumerates local groups for the specified computer. /// </summary> /// <param name="computer">The computer to enumerate.</param> /// <returns>An array of LocalGroupInfo1 structures.</returns> /// <remarks></remarks> public static LocalGroupInfo1[] EnumLocalGroups(string computer) { var mm = new MemPtr(); int en = 0; int ten = 0; MemPtr adv; LocalGroupInfo1[] grp; var inul = new IntPtr(); NetInfo.NetLocalGroupEnum(computer, 1, ref mm, -1, ref en, ref ten, ref inul); adv = mm; int i; int c = ten; grp = new LocalGroupInfo1[c + 1]; for (i = 0; i < c; i++) { grp[i] = adv.ToStruct <LocalGroupInfo1>(); adv = adv + Marshal.SizeOf <LocalGroupInfo1>(); } mm.NetFree(); return(grp); }
/// <summary> /// Sets the raw byte value of a Hid feature code. /// </summary> /// <param name="featureCode">The Hid feature code to set.</param> /// <param name="value">The value to set.</param> /// <returns></returns> /// <remarks></remarks> public bool HidSetFeature(byte featureCode, byte[] value) { bool HidSetFeatureRet = default; var hfile = HidFeatures.OpenHid(this); if (hfile == IntPtr.Zero) { return(false); } var mm = new MemPtr(); mm.Alloc(value.Length + 1); mm.FromByteArray(value, 1L); mm.ByteAt(0L) = featureCode; if (!UsbLibHelpers.HidD_SetFeature(hfile, mm, (int)mm.Length)) { HidSetFeatureRet = false; } else { HidSetFeatureRet = true; } mm.Free(); HidFeatures.CloseHid(hfile); return(HidSetFeatureRet); }
/// <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); }
/// <summary> /// Retrieves a linked, unmanaged structure array of IP_ADAPTER_ADDRESSES, enumerating all network interfaces on the system. /// This function is internal to the managed API in this assembly and is not intended to be used independently from it. /// The results of this function are abstracted into the managed <see cref="AdaptersCollection" /> class. Use that, instead. /// </summary> /// <param name="origPtr">Receives the memory pointer for the memory allocated to retrieve the information from the system.</param> /// <param name="noRelease">Specifies that the memory will not be released after usage (this is a typical scenario).</param> /// <returns>A linked, unmanaged structure array of IP_ADAPTER_ADDRESSES.</returns> /// <remarks></remarks> internal static IP_ADAPTER_ADDRESSES[] GetAdapters(ref MemPtr origPtr, bool noRelease = true) { var lpadapt = new LPIP_ADAPTER_ADDRESSES(); IP_ADAPTER_ADDRESSES adapt; uint cblen = 0; var res = GetAdaptersAddresses(AfENUM.AfUnspecified, GAA_FLAGS.GAA_FLAG_INCLUDE_GATEWAYS | GAA_FLAGS.GAA_FLAG_INCLUDE_WINS_INFO | GAA_FLAGS.GAA_FLAG_INCLUDE_ALL_COMPARTMENTS | GAA_FLAGS.GAA_FLAG_INCLUDE_ALL_INTERFACES, IntPtr.Zero, lpadapt, ref cblen); // we have a buffer overflow? We need to get more memory. if (res == ADAPTER_ENUM_RESULT.ERROR_BUFFER_OVERFLOW) { lpadapt.Handle.Alloc(cblen, noRelease); res = GetAdaptersAddresses(AfENUM.AfUnspecified, GAA_FLAGS.GAA_FLAG_INCLUDE_GATEWAYS | GAA_FLAGS.GAA_FLAG_INCLUDE_WINS_INFO, IntPtr.Zero, lpadapt, ref cblen); } if (res != ADAPTER_ENUM_RESULT.NO_ERROR) { lpadapt.Dispose(); throw new NativeException(); } origPtr = lpadapt.Handle; IP_ADAPTER_ADDRESSES[] adapters = null; int c = 0; int cc = 0; adapt = lpadapt; do { if (string.IsNullOrEmpty(adapt.Description) | adapt.FirstDnsServerAddress.Handle == IntPtr.Zero) { c += 1; adapt = adapt.Next; if (adapt.Next.Handle == MemPtr.Empty) { break; } continue; } Array.Resize(ref adapters, cc + 1); adapters[cc] = adapt; adapt = adapt.Next; cc += 1; c += 1; }while (adapt.Next.Handle != MemPtr.Empty); // there is currently no reason for this function to free this pointer, // but we reserve the right to do so, in the future. if (!noRelease) { origPtr.Free(); } return(adapters); }
/// <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(); }
private bool _enum(IntPtr hMonitor, IntPtr hdcMonitor, ref W32RECT lpRect, IntPtr lParamIn) { MemPtr lParam = lParamIn; Add(new MonitorInfo(hMonitor, lParam.IntAt(0L))); //string[] ss = GetPhysicalMonitorNames(hMonitor); lParam.IntAt(0L) += 1; return(true); }
/// <summary> /// Create a writable device-independent bitmap from the specified image. /// </summary> /// <param name="img">Image to copy.</param> /// <param name="bitPtr">Optional variable to receive a pointer to the bitmap bits.</param> /// <returns>A new DIB handle that must be destroyed with DeleteObject().</returns> /// <remarks></remarks> public static IntPtr MakeDIBSection(Bitmap img, ref IntPtr bitPtr) { // Build header. // adapted from C++ code examples. short wBitsPerPixel = 32; int BytesPerRow = (int)((double)(img.Width * wBitsPerPixel + 31 & ~31L) / 8d); int size = img.Height * BytesPerRow; var bmpInfo = default(BITMAPINFO); var mm = new MemPtr(); int bmpSizeOf = Marshal.SizeOf(bmpInfo); mm.ReAlloc(bmpSizeOf + size); var pbmih = default(BITMAPINFOHEADER); pbmih.biSize = Marshal.SizeOf(pbmih); pbmih.biWidth = img.Width; pbmih.biHeight = img.Height; // positive indicates bottom-up DIB pbmih.biPlanes = 1; pbmih.biBitCount = wBitsPerPixel; pbmih.biCompression = (int)BI_RGB; pbmih.biSizeImage = size; pbmih.biXPelsPerMeter = (int)(24.5d * 1000d); // pixels per meter! And these values MUST be correct if you want to pass a DIB to a native menu. pbmih.biYPelsPerMeter = (int)(24.5d * 1000d); // pixels per meter! pbmih.biClrUsed = 0; pbmih.biClrImportant = 0; var pPixels = IntPtr.Zero; int DIB_RGB_COLORS = 0; Marshal.StructureToPtr(pbmih, mm.Handle, false); var hPreviewBitmap = BitmapTools.CreateDIBSection(IntPtr.Zero, mm.Handle, (uint)DIB_RGB_COLORS, ref pPixels, IntPtr.Zero, 0); bitPtr = pPixels; var bm = new System.Drawing.Imaging.BitmapData(); bm = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppPArgb, bm); var pCurrSource = bm.Scan0; // Our DIBsection is bottom-up...start at the bottom row... var pCurrDest = pPixels + (img.Width - 1) * BytesPerRow; // ... and work our way up int DestinationStride = -BytesPerRow; for (int curY = 0, ih = img.Height - 1; curY <= ih; curY++) { Native.MemCpy(pCurrSource, pCurrDest, BytesPerRow); pCurrSource = pCurrSource + bm.Stride; pCurrDest = pCurrDest + DestinationStride; } // Free up locked buffer. img.UnlockBits(bm); return(hPreviewBitmap); }
public string[] AllStrings() { string[] AllStringsRet = default; char ch = '\0'; MemPtr p2 = ptr; string[] s; int c = _Sb.Count - 1; s = new string[c + 1]; var a = default(ulong); if (_Sb.LpWStr) { for (int j = 0, loopTo = c; j <= loopTo; j++) { s[j] = p2.GrabString((IntPtr)0); p2 += s[j].Length * 2 + 2; } } else { for (int j = 0, loopTo1 = c; j <= loopTo1; j++) { switch (sd) { case 2: { a = p2.get_UShortAt(0L); break; } case 4: { a = p2.get_UIntegerAt(0L); break; } case 8: { a = p2.get_ULongAt(0L); break; } } s[j] = p2.GrabString((IntPtr)sd, (int)a); p2 += (long)(a * 2m + sd); } } AllStringsRet = s; return(AllStringsRet); }
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(); }
/// <summary> /// Get volume disk extents for volumes that may or may not span more than one physical drive. /// </summary> /// <param name="devicePath">The device path of the volume.</param> /// <returns>An array of DiskExtent structures.</returns> /// <remarks></remarks> public static DiskExtent[] GetDiskExtentsFor(string devicePath) { DiskExtent[] deOut = null; MemPtr inBuff = new MemPtr(); int inSize; IntPtr file; int h = 0; var de = new DISK_EXTENT(); var ve = new VOLUME_DISK_EXTENTS(); bool r; file = IO.CreateFile(devicePath, IO.GENERIC_READ, IO.FILE_SHARE_READ | IO.FILE_SHARE_WRITE, IntPtr.Zero, IO.OPEN_EXISTING, IO.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero); if (file == DevProp.INVALID_HANDLE_VALUE) { return(null); } uint arb = 0; inSize = Marshal.SizeOf(de) + Marshal.SizeOf(ve); inBuff.Length = inSize; r = DeviceIoControl(file, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, IntPtr.Zero, 0, inBuff, (uint)inSize, ref arb, IntPtr.Zero); if (!r && User32.GetLastError() == ERROR_MORE_DATA) { inBuff.Length = inSize * inBuff.IntAt(0L); r = DeviceIoControl(file, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, IntPtr.Zero, 0, inBuff, (uint)inSize, ref arb, IntPtr.Zero); } if (!r) { inBuff.Free(); User32.CloseHandle(file); return(null); } User32.CloseHandle(file); ve = VOLUME_DISK_EXTENTS.FromPtr(inBuff); inBuff.Free(); h = 0; deOut = new DiskExtent[ve.Extents.Length]; foreach (var currentDe in ve.Extents) { de = currentDe; deOut[h].PhysicalDevice = de.DiskNumber; deOut[h].Space = de.Space; deOut[h].Size = de.ExtentLength; deOut[h].Offset = de.StartingOffset; h += 1; } return(deOut); }
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); }
/// <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); }
/// <summary> /// Converts this raw icon source into a managed System.Drawing.Icon image. /// </summary> /// <returns>A new Icon object.</returns> /// <remarks></remarks> public Icon ToIcon() { Icon iconOut; if (IsPngFormat) { Bitmap bmp = (Bitmap)ToImage(); var bmi = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format1bppIndexed); var lpicon = default(ICONINFO); int i; var bm = bmi.LockBits(new Rectangle(0, 0, bmi.Width, bmi.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format1bppIndexed); MemPtr mm = bm.Scan0; int z = (int)(Math.Max(bmp.Width, 32) * bmp.Height / 8d); for (i = 0; i < z; i++) { mm.ByteAt(i) = 255; } bmi.UnlockBits(bm); lpicon.hbmColor = bmp.GetHbitmap(); lpicon.hbmMask = bmi.GetHbitmap(); lpicon.fIcon = 1; var hIcon = User32.CreateIconIndirect(ref lpicon); if (hIcon != IntPtr.Zero) { iconOut = (Icon)Icon.FromHandle(hIcon).Clone(); User32.DestroyIcon(hIcon); } else { iconOut = null; } NativeShell.DeleteObject(lpicon.hbmMask); NativeShell.DeleteObject(lpicon.hbmColor); } else { iconOut = _constructIcon(); } return(iconOut); }
/// <summary> /// Validate the header and CRC-32 of this structure. /// </summary> /// <returns>True if the structure is valid.</returns> /// <remarks></remarks> public bool Validate() { bool ValidateRet = default; var mm = new MemPtr(); mm.FromStruct(this); mm.UIntAt(4L) = 0U; // validate the crc and the signature moniker ValidateRet = HeaderCRC32 == mm.CalculateCrc32() && Signature == 0x5452415020494645; mm.Free(); return(ValidateRet); }
/// <summary> /// Create a transparency mask from the transparent bits in an image. /// </summary> /// <param name="hBits">A pointer to the memory address of the bitmap bits.</param> /// <param name="hMask">A pointer to the memory address of the mask bits.</param> /// <param name="Width">The width of the image.</param> /// <param name="Height">The height of the image.</param> /// <remarks></remarks> private void _setMask(MemPtr hBits, MemPtr hMask, int Width, int Height) { // this never changes int numBits = 32; int x; int y; byte bit; int d; int e; int f; double move = numBits / 8d; int stride = (int)(Width * (numBits / 8d)); int msize = (int)(Math.Max(32, Width) * Height / 8d); int isize = (int)(Width * Height * (numBits / 8d)); byte[] bb; byte[] imgb; imgb = new byte[isize]; bb = new byte[msize]; Marshal.Copy(hBits.Handle, imgb, 0, isize); for (y = 0; y < Height; y++) { d = y * stride; for (x = 0; x < Width; x++) { f = (int)(d + x * move); e = (int)Math.Floor(x / 8d); bit = (byte)(7 - x % 8); if (imgb[f + 3] == 0) { bb[e] = (byte)(bb[e] | 1 << bit); } } } // MemCpy(hMask.Handle, bb, msize) hMask.FromByteArray(bb, 0L); }
/// <summary> /// Calculate the CRC-32 of a memory pointer. /// </summary> /// <param name="data">Pointer containing the bytes to calculate.</param> /// <param name="length">Specify the length, in bytes, of the data to be checked.</param> /// <param name="crc">Input CRC value for ongoing calculations (default is FFFFFFFFh).</param> /// <param name="bufflen">Specify the size, in bytes, of the marshaling buffer to be used (default is 1k).</param> /// <returns>A 32-bit unsigned integer representing the calculated CRC.</returns> /// <remarks></remarks> public static uint Calculate(IntPtr data, IntPtr length, uint crc = 0xFFFFFFFFU, int bufflen = 1024) { uint CalculateRet = default; if (length.ToInt64() <= 0L) { throw new ArgumentOutOfRangeException("length", "length must be a positive number."); } if (data == IntPtr.Zero) { throw new ArgumentNullException("data", "data cannot be equal to null."); } // ' our working marshal buffer will be 1k, this is a good compromise between eating up memory and efficiency. int blen = bufflen; byte[] b; var mm = new MemPtr(data); long i; long l = length.ToInt64(); long c = l - 1L; int e; int j; b = new byte[blen]; var loopTo = c; for (i = 0L; (long)blen >= 0 ? i <= loopTo : i >= loopTo; i += blen) { if (l - i > blen) { e = blen; } else { e = (int)(l - i); } mm.GrabBytes((IntPtr)i, e, ref b); e -= 1; var loopTo1 = e; for (j = 0; j <= loopTo1; j++) { crc = Crc32Table[(int)((crc ^ b[j]) & 0xFFL)] ^ crc >> 8; } } CalculateRet = crc ^ 0xFFFFFFFFU; return(CalculateRet); }
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); }
public static bool GetVolumePathNamesForVolumeName(string lpszVolumeName, ref string[] lpszVolumePathNames) { var sp = new MemPtr(); uint ul = 0U; IO.GetVolumePathNamesForVolumeNameW(lpszVolumeName, IntPtr.Zero, 0U, ref ul); sp.Alloc((ul + 1L) * sizeof(char)); IO.GetVolumePathNamesForVolumeNameW(lpszVolumeName, sp, (uint)sp.Length, ref ul); lpszVolumePathNames = sp.GetStringArray(0L); sp.Free(); return(true); }
public static string[] GetPhysicalMonitorNames(IntPtr hMonitor) { var mm = new MemPtr(); string[] sOut = null; PHYSICAL_MONITOR pm; uint nmon = 0; if (!GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, out nmon)) { return(null); } int cb = Marshal.SizeOf <PHYSICAL_MONITOR>(); int size = cb * (int)nmon; mm.Alloc(size); try { if (GetPhysicalMonitorsFromHMONITOR(hMonitor, size, mm)) { sOut = new string[size]; int i; for (i = 0; i < nmon; i++) { pm = mm.ToStructAt <PHYSICAL_MONITOR>(i * cb); sOut[i] = pm.szPhysicalMonitorDescription; } DestroyPhysicalMonitors((uint)size, mm); } else { sOut = new string[] { NativeErrorMethods.FormatLastError() }; } mm.Free(); } catch { mm.Free(); } return(sOut); }
/// <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) }