Click here to Skip to main content
15,894,460 members
Home / Discussions / C#
   

C#

 
AnswerRe: Expand/Collapse TreeView Nodes on Ctrl+LeftMouse in WPF Pin
OriginalGriff12-Mar-21 4:17
mveOriginalGriff12-Mar-21 4:17 
Question.NET native vs. custom localization Pin
Niemand2510-Mar-21 23:18
professionalNiemand2510-Mar-21 23:18 
AnswerRe: .NET native vs. custom localization Pin
Gerry Schmitz11-Mar-21 7:20
mveGerry Schmitz11-Mar-21 7:20 
AnswerRe: .NET native vs. custom localization Pin
lmoelleb15-Mar-21 22:40
lmoelleb15-Mar-21 22:40 
QuestionContinue program after encountering error Pin
Member 1447460710-Mar-21 10:05
Member 1447460710-Mar-21 10:05 
AnswerRe: Continue program after encountering error Pin
Dave Kreskowiak10-Mar-21 10:14
mveDave Kreskowiak10-Mar-21 10:14 
GeneralRe: Continue program after encountering error Pin
Member 1447460710-Mar-21 10:35
Member 1447460710-Mar-21 10:35 
GeneralRe: Continue program after encountering error Pin
Dave Kreskowiak10-Mar-21 11:05
mveDave Kreskowiak10-Mar-21 11:05 
GeneralRe: Continue program after encountering error Pin
Gerry Schmitz11-Mar-21 7:23
mveGerry Schmitz11-Mar-21 7:23 
AnswerRe: Continue program after encountering error Pin
NotTodayYo10-Mar-21 10:52
NotTodayYo10-Mar-21 10:52 
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 
Radnor,

You were on the right track. The program was running as administrator so the "Access denied" was confusing me as well. The SafeFileHandle was created like this ...

using (dd = new FileStream(path + ".dd", FileMode.Open, FileAccess.Write, FileShare.Write))
{
bool finished = NativeMethods.GetSparseRange(ldd.SafeFileHandle, query, ref response, 1024);

}

So when the DeviceIoControl was called the file was empty which for some reason caused the return code 5.

Changing the FileStream creation for an existing file stopped the error.

However now I get an 0x00000057 error, parameter error which brings me back to the original question as how to pass an array of structures to DeviceIoControl.

This is what I've tried, the output buffer is the issue I think....

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);
    uint bytesread=0;

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

    //IntPtr[] ptrArr = new IntPtr[count];
    //for (int i = 0; i < count; ++i)
    //{
    //    ptrArr[i] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(FILE_ALLOCATED_RANGE_BUFFER)));
    //    Marshal.StructureToPtr(response[i], ptrArr[i], true);
    //}
    bool rc = DeviceIoControl(
        hdrive,                                                     // device to be queried
        (uint)EIOControlCode.FsctlQueryAllocatedRanges,             // operation to perform
        lpInBuffer, (uint)nInBufferSize,                         // input buffer
        lpOutBuffer, (uint)nOutBufferSize,
        //ptrArr, (uint)(nInBufferSize * count),                   // output buffer
        ref bytesread,                                          // # bytes returned
        IntPtr.Zero);                                               // synchronous I/O
    // Get the last error and display it.
    int error = Marshal.GetLastWin32Error();
    Marshal.PtrToStructure(lpOutBuffer, response);
    //for (int j = 0; j < count / nInBufferSize; ++j)
    //{
    //    if (j < bytesread / nInBufferSize)
    //        Marshal.PtrToStructure(ptrArr[j], response[j]);
    //    Marshal.FreeHGlobal(ptrArr[j]);
    //}
    Marshal.FreeHGlobal(lpInBuffer);
    Marshal.FreeHGlobal(lpOutBuffer);
    return rc;
}


Thanks
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 
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 

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.