Click here to Skip to main content
14,874,228 members
Home / Discussions / C#
   

C#

 
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
MemberMember 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
MemberNotTodayYo10-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
MemberBobZscharnagk7-Mar-21 22:13 
I'm trying to get the list of allocated ranges for a sparse file on Windows 10 from a C# program. DeviceIOControl requires an array of FILE_ALLOCATED_RANGE_BUFFER structures as the output buffer. I just can't seem to get the correct way of passing these. So far I have....

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;
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
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();
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);
return rc;
}

However this return rc=false, byteread=0 and error=5.

Can anyone suggest the correct way to do this?

Thanks
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
MemberBobZscharnagk10-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 
GeneralRe: How to pass an array or structs to DeviceIoControl when using pinvoke Pin
BobZscharnagk10-Mar-21 19:30
MemberBobZscharnagk10-Mar-21 19:30 
GeneralRe: How to pass an array or structs to DeviceIoControl when using pinvoke Pin
BobZscharnagk10-Mar-21 22:40
MemberBobZscharnagk10-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
Member 147923937-Mar-21 9:17
MemberMember 147923937-Mar-21 9:17 
AnswerRe: User Control Pin
OriginalGriff7-Mar-21 11:20
mveOriginalGriff7-Mar-21 11:20 
GeneralRe: User Control Pin
Member 147923938-Mar-21 1:50
MemberMember 147923938-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
mveBillWoodruff7-Mar-21 17:03 
GeneralRe: User Control Pin
Member 147923938-Mar-21 1:49
MemberMember 147923938-Mar-21 1:49 
AnswerRe: User Control Pin
Ralf Meier7-Mar-21 22:46
professionalRalf Meier7-Mar-21 22:46 
GeneralRe: User Control Pin
BillWoodruff8-Mar-21 1:07
mveBillWoodruff8-Mar-21 1:07 
GeneralRe: User Control Pin
Member 147923938-Mar-21 1:51
MemberMember 147923938-Mar-21 1:51 

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.