/// <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); }
/// <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 static NETRESOURCE[] EnumComputer(string computer, string username = null, string password = null) { NETRESOURCE[] EnumComputerRet = default; var lpnet = new NETRESOURCE(); MemPtr mm = new MemPtr(); if (computer.Substring(0, 2) == @"\\") { computer = computer.Substring(2); } mm.ReAlloc(10240L); if (username is object && password is object) { lpnet.lpRemoteName = @"\\" + computer; Marshal.StructureToPtr(lpnet, mm.Handle, false); int res = LocalNet.WNetAddConnection3(IntPtr.Zero, mm.Handle, password, username, CONNECT_INTERACTIVE); mm.Free(); if (res != 0) { return(null); } mm.ReAlloc(10240L); } lpnet.dwDisplayType = RESOURCEDISPLAYTYPE_SERVER; lpnet.lpRemoteName = @"\\" + computer; lpnet.dwScope = RESOURCE_CONTEXT; lpnet.dwUsage = RESOURCEUSAGE_CONTAINER; Marshal.StructureToPtr(lpnet, mm.Handle, false); EnumComputerRet = DoEnum(mm.Handle); mm.Free(); return(EnumComputerRet); }
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); }
/// <summary> /// Return the username for the current user /// </summary> /// <returns></returns> public static string CurrentUserName() { string CurrentUserNameRet = default; MemPtr lps = new MemPtr(); int cb = 10240; lps.ReAlloc(10240L); lps.ZeroMemory(); if (GetUserName(lps.Handle, ref cb)) { CurrentUserNameRet = lps.ToString(); } else { CurrentUserNameRet = null; } lps.Free(); return(CurrentUserNameRet); }
/// <summary> /// Retrieve the partition table of a GPT-layout disk, manually. /// Must be Administrator. /// </summary> /// <param name="inf">DiskDeviceInfo object to the physical disk to read.</param> /// <param name="gptInfo">Receives the drive layout information.</param> /// <returns>True if successful.</returns> /// <remarks></remarks> public static bool ReadRawGptDisk(string devicePath, ref RAW_GPT_DISK gptInfo) { // Demand Administrator for accessing a raw disk. AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); // Dim principalPerm As New PrincipalPermission(Nothing, "Administrators") // principalPerm.Demand() var hfile = IO.CreateFile(devicePath, IO.GENERIC_READ | IO.GENERIC_WRITE, IO.FILE_SHARE_READ | IO.FILE_SHARE_WRITE, IntPtr.Zero, IO.OPEN_EXISTING, IO.FILE_FLAG_NO_BUFFERING | IO.FILE_FLAG_RANDOM_ACCESS, IntPtr.Zero); if (hfile == DevProp.INVALID_HANDLE_VALUE) { return(false); } DISK_GEOMETRY_EX geo = default; // get the disk geometry to retrieve the sector (LBA) size. if (!DiskGeometry(hfile, ref geo)) { User32.CloseHandle(hfile); return(false); } // sector size (usually 512 bytes) uint bps = geo.Geometry.BytesPerSector; uint br = 0U; long lp = 0L; long lp2 = 0L; var mm = new MemPtr(bps * 2L); IO.SetFilePointerEx(hfile, 0L, ref lp, IO.FilePointerMoveMethod.Begin); IO.ReadFile(hfile, mm.Handle, bps * 2, ref br, IntPtr.Zero); var mbr = new RAW_MBR(); var gpt = new RAW_GPT_HEADER(); RAW_GPT_PARTITION[] gpp = null; // read the master boot record. mbr = mm.ToStructAt <RAW_MBR>(446L); // read the GPT structure header. gpt = mm.ToStructAt <RAW_GPT_HEADER>(bps); // check the partition header CRC. if (gpt.IsValid) { long lr = br; // seek to the LBA of the partition information. IO.SetFilePointerEx(hfile, (uint)(bps * gpt.PartitionEntryLBA), ref lr, IO.FilePointerMoveMethod.Begin); br = (uint)lr; // calculate the size of the partition table buffer. lp = gpt.NumberOfPartitions * gpt.PartitionEntryLength; // byte align to the sector size. if (lp % bps != 0L) { lp += bps - lp % bps; } // bump up the memory pointer. mm.ReAlloc(lp); mm.ZeroMemory(); // read the partition information into the pointer. IO.ReadFile(hfile, mm.Handle, (uint)lp, ref br, IntPtr.Zero); // check the partition table CRC. if (mm.CalculateCrc32() == gpt.PartitionArrayCRC32) { // disk is valid. lp = (uint)Marshal.SizeOf <RAW_GPT_PARTITION>(); br = 0U; int i; int c = (int)gpt.NumberOfPartitions; gpp = new RAW_GPT_PARTITION[c + 1]; // populate the drive information. for (i = 0; i < c; i++) { gpp[i] = mm.ToStructAt <RAW_GPT_PARTITION>(lp2); // break on empty GUID, we are past the last partition. if (gpp[i].PartitionTypeGuid == Guid.Empty) { break; } lp2 += lp; } // trim off excess records from the array. if (i < c) { if (i == 0) { gpp = Array.Empty <RAW_GPT_PARTITION>(); } else { Array.Resize(ref gpp, i); } } } } // free the resources. mm.Free(); User32.CloseHandle(hfile); // if gpp is nothing then some error occurred somewhere and we did not succeed. if (gpp is null) { return(false); } // create a new RAW_GPT_DISK structure. gptInfo = new RAW_GPT_DISK(); gptInfo.Header = gpt; gptInfo.Partitions = gpp; // we have succeeded. return(true); }