Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
AnswerRe: Continue program after encountering error Pin
Eddy Vluggen10-Mar-21 11:24
professionalEddy Vluggen10-Mar-21 11:24 
AnswerRe: Continue program after encountering error Pin
OriginalGriff10-Mar-21 21:17
mveOriginalGriff10-Mar-21 21:17 
QuestionHow to pass an array or structs to DeviceIoControl when using pinvoke Pin
BobZscharnagk7-Mar-21 22:13
BobZscharnagk7-Mar-21 22:13 
AnswerRe: How to pass an array or structs to DeviceIoControl when using pinvoke Pin
Randor 8-Mar-21 11:50
professional Randor 8-Mar-21 11:50 
GeneralRe: How to pass an array or structs to DeviceIoControl when using pinvoke Pin
Gerry Schmitz9-Mar-21 8:16
mveGerry Schmitz9-Mar-21 8:16 
GeneralRe: How to pass an array or structs to DeviceIoControl when using pinvoke Pin
Randor 9-Mar-21 10:24
professional Randor 9-Mar-21 10:24 
GeneralRe: How to pass an array or structs to DeviceIoControl when using pinvoke Pin
BobZscharnagk10-Mar-21 14:09
BobZscharnagk10-Mar-21 14:09 
GeneralRe: How to pass an array or structs to DeviceIoControl when using pinvoke Pin
Randor 10-Mar-21 16:54
professional Randor 10-Mar-21 16:54 
Hello,

Your GetSparseRange function is working for me. I simply wrote a quick test application and used the function you provided.

C#
using System;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace SparseRanges
{
    #region Structs

    struct FILE_ZERO_DATA_INFORMATION
    {
        public long offset;
        public long zero;
    }

    struct FILE_ALLOCATED_RANGE_BUFFER
    {
        public long offset;
        public long length;
    }
    #endregion

    class Program
    {
        #region Const ints
        const uint FSCTL_SET_SPARSE = 590020;
        const uint FSCTL_QUERY_ALLOCATED_RANGES = 606415;
        #endregion

        [DllImport("kernel32.dll", SetLastError = true)]
        extern static bool GetVolumeInformation(string vol, StringBuilder name, int nameSize, out uint serialNum, out uint maxNameLen, out uint flags, StringBuilder fileSysName, int fileSysNameSize);
        
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool DeviceIoControl([In] SafeFileHandle hDevice, [In] uint dwIoControlCode, IntPtr lpInBuffer, [In] int nInBufferSize, IntPtr lpOutBuffer, [In] int nOutBufferSize, ref uint lpBytesReturned, [In] IntPtr lpOverlapped);
        public static bool GetSparseRange(SafeFileHandle hdrive, in FILE_ALLOCATED_RANGE_BUFFER query, ref FILE_ALLOCATED_RANGE_BUFFER[] response, int count)
        {
            int nInBufferSize = (int)Marshal.SizeOf(typeof(FILE_ALLOCATED_RANGE_BUFFER));
            IntPtr lpInBuffer = Marshal.AllocHGlobal(nInBufferSize);
            Marshal.StructureToPtr(query, lpInBuffer, true);

            int nOutBufferSize = nInBufferSize * count;
            IntPtr lpOutBuffer = Marshal.AllocHGlobal(nOutBufferSize);

            uint bytesread = 0;

            bool rc = DeviceIoControl(hdrive, FSCTL_QUERY_ALLOCATED_RANGES, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, ref bytesread, IntPtr.Zero);

            int error = Marshal.GetLastWin32Error();

            Marshal.FreeHGlobal(lpInBuffer);
            Marshal.FreeHGlobal(lpOutBuffer);
            return rc;
        }

        private static void PrintSparseRange(FILE_ALLOCATED_RANGE_BUFFER range)
        {
            Console.WriteLine("{0:d} offset, length = {1:d}", range.offset, range.length);
        }

        static void Main(string[] args)
        {
            uint temp1, temp2, flags, bytes=0;
            if (GetVolumeInformation("C:\\", null, 0, out temp1, out temp2, out flags, null, 0))
            {
                uint supports_sparse = flags & 0x40;
                if (supports_sparse == 0x40)
                {
                    Console.WriteLine("Volume supports sparse files...");
                    
                    string sparse_filename = Path.GetTempFileName();
                    System.IO.FileStream f = System.IO.File.Create(sparse_filename);

                    if (DeviceIoControl(f.SafeFileHandle, FSCTL_SET_SPARSE, IntPtr.Zero, 0, IntPtr.Zero, 0, ref bytes, IntPtr.Zero))
                    {
                        Console.WriteLine("Successfully generated a sparse file.");

                        f.SetLength(10240);

                        FILE_ALLOCATED_RANGE_BUFFER[] response = new FILE_ALLOCATED_RANGE_BUFFER[64];
                        FILE_ALLOCATED_RANGE_BUFFER query;
                        query.length = 10240;
                        query.offset = 0;
                        int count = 64;
                        
                        if(GetSparseRange(f.SafeFileHandle, query, ref response, count))
                        {
                            Action<FILE_ALLOCATED_RANGE_BUFFER> print_all = new Action<FILE_ALLOCATED_RANGE_BUFFER>(PrintSparseRange);
                            Array.ForEach(response, print_all);
                        }
                        f.Close();

                        Console.WriteLine("Removing temp sparse file.");
                        File.Delete(sparse_filename);
                    }
                }
            }
        }
    }
}



BobZscharnagk wrote:
The SafeFileHandle was created like this ...
using (dd = new FileStream(path + ".dd", FileMode.Open, FileAccess.Write, FileShare.Write))
Any particular reason why you are opening the file without Read access?

Best Wishes,
-David Delaune
GeneralRe: How to pass an array or structs to DeviceIoControl when using pinvoke Pin
BobZscharnagk10-Mar-21 19:30
BobZscharnagk10-Mar-21 19:30 
GeneralRe: How to pass an array or structs to DeviceIoControl when using pinvoke Pin
BobZscharnagk10-Mar-21 22:40
BobZscharnagk10-Mar-21 22:40 
GeneralRe: How to pass an array or structs to DeviceIoControl when using pinvoke Pin
Randor 10-Mar-21 23:08
professional Randor 10-Mar-21 23:08 
QuestionUser Control Pin
Ismael_19997-Mar-21 9:17
Ismael_19997-Mar-21 9:17 
AnswerRe: User Control Pin
OriginalGriff7-Mar-21 11:20
mveOriginalGriff7-Mar-21 11:20 
GeneralRe: User Control Pin
Ismael_19998-Mar-21 1:50
Ismael_19998-Mar-21 1:50 
GeneralRe: User Control Pin
OriginalGriff8-Mar-21 2:45
mveOriginalGriff8-Mar-21 2:45 
AnswerRe: User Control Pin
BillWoodruff7-Mar-21 17:03
professionalBillWoodruff7-Mar-21 17:03 
GeneralRe: User Control Pin
Ismael_19998-Mar-21 1:49
Ismael_19998-Mar-21 1:49 
AnswerRe: User Control Pin
Ralf Meier7-Mar-21 22:46
mveRalf Meier7-Mar-21 22:46 
GeneralRe: User Control Pin
BillWoodruff8-Mar-21 1:07
professionalBillWoodruff8-Mar-21 1:07 
GeneralRe: User Control Pin
Ismael_19998-Mar-21 1:51
Ismael_19998-Mar-21 1:51 
GeneralRe: User Control Pin
Ralf Meier8-Mar-21 5:10
mveRalf Meier8-Mar-21 5:10 
QuestionHow to take JSON POST data and covert it to something i can work with Pin
jacobarrey4-Mar-21 11:40
jacobarrey4-Mar-21 11:40 
AnswerRe: How to take JSON POST data and covert it to something i can work with Pin
OriginalGriff4-Mar-21 11:45
mveOriginalGriff4-Mar-21 11:45 
GeneralRe: How to take JSON POST data and covert it to something i can work with Pin
jacobarrey5-Mar-21 9:37
jacobarrey5-Mar-21 9:37 
GeneralRe: How to take JSON POST data and covert it to something i can work with Pin
OriginalGriff5-Mar-21 10:57
mveOriginalGriff5-Mar-21 10:57 

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.