A little C# -> VB.Net conversion help?

J

Jim Hubbard

I have some C# code that is supposed to wrap the defrag APIs and I am trying
to convert it to VB.Net (2003). But, I keep having problems.

The C# code is relatively short, so I'll post it here.....

--------
//
// a set of simple C# wrappers over the NT Defragmenter APIs
//

//
// Refrences

//
// http://www.sysinternals.com/ntw2k/info/defrag.shtml
//
// msdn how-to
// ms-help://MS.MSDNQTR.2004JUL.1033/fileio/base/defragmenting_files.htm
//
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/defragmenting_files.asp
//
// FSCTL_GET_VOLUME_BITMAP
//
http://msdn.microsoft.com/library/d...en-us/fileio/base/fsctl_get_volume_bitmap.asp
//
// interesting structures...
// FSCTL_MOVE_FILE
// FSCTL_GET_RETRIEVAL_POINTERS
// RETRIEVAL_POINTERS_BUFFER
// FSCTL_GET_RETRIEVAL_POINTERS
//
// DeviceIoControl
//
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/deviceiocontrol.asp
//

using System;
using System.Diagnostics;
using System.Collections;
using System.Runtime.InteropServices;

namespace defraglib
{
public class IOWrapper
{

//
// CreateFile constants
//
const uint FILE_SHARE_READ = 0x00000001;
const uint FILE_SHARE_WRITE = 0x00000002;
const uint FILE_SHARE_DELETE = 0x00000004;
const uint OPEN_EXISTING = 3;

const uint GENERIC_READ = (0x80000000);
const uint GENERIC_WRITE = (0x40000000);

const uint FILE_FLAG_NO_BUFFERING = 0x20000000;
const uint FILE_READ_ATTRIBUTES = (0x0080);
const uint FILE_WRITE_ATTRIBUTES = 0x0100;
const uint ERROR_INSUFFICIENT_BUFFER = 122;

[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr CreateFile(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);

[DllImport("kernel32.dll", SetLastError = true)]
static extern int CloseHandle(IntPtr hObject);

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool DeviceIoControl(
IntPtr hDevice,
uint dwIoControlCode,
IntPtr lpInBuffer,
uint nInBufferSize,
[Out] IntPtr lpOutBuffer,
uint nOutBufferSize,
ref uint lpBytesReturned,
IntPtr lpOverlapped);

static private IntPtr OpenVolume(string DeviceName)
{
IntPtr hDevice;
hDevice = CreateFile(
@"\\.\" + DeviceName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
0,
IntPtr.Zero);
if ((int)hDevice == -1)
{
throw new Exception(Marshal.GetLastWin32Error().ToString());
}
return hDevice;
}

static private IntPtr OpenFile(string path)
{
IntPtr hFile;
hFile = CreateFile(
path,
FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
0,
IntPtr.Zero);
if ((int)hFile == -1)
{
throw new Exception(Marshal.GetLastWin32Error().ToString());
}
return hFile;
}


/// <summary>
/// Get cluster usage for a device
/// </summary>
/// <param name="DeviceName">use "c:"</param>
/// <returns>a bitarray for each cluster</returns>
static public BitArray GetVolumeMap(string DeviceName)
{
IntPtr pAlloc = IntPtr.Zero;
IntPtr hDevice = IntPtr.Zero;

try
{
hDevice = OpenVolume(DeviceName);

Int64 i64 = 0;

GCHandle handle = GCHandle.Alloc(i64, GCHandleType.Pinned);
IntPtr p = handle.AddrOfPinnedObject();

// alloc off more than enough for my machine
// 64 megs == 67108864 bytes == 536870912 bits == cluster
count
// NTFS 4k clusters == 2147483648 k of storage == 2097152
megs == 2048 gig disk storage
uint q = 1024 * 1024 * 64; // 1024 bytes == 1k * 1024 == 1
meg * 64 == 64 megs

uint size = 0;
pAlloc = Marshal.AllocHGlobal((int)q);
IntPtr pDest = pAlloc;

bool fResult = DeviceIoControl(
hDevice,
FSConstants.FSCTL_GET_VOLUME_BITMAP,
p,
(uint)Marshal.SizeOf(i64),
pDest,
q,
ref size,
IntPtr.Zero);

if (!fResult)
{
throw new
Exception(Marshal.GetLastWin32Error().ToString());
}
handle.Free();

/*
object returned was...
typedef struct
{
LARGE_INTEGER StartingLcn;
LARGE_INTEGER BitmapSize;
BYTE Buffer[1];
} VOLUME_BITMAP_BUFFER, *PVOLUME_BITMAP_BUFFER;
*/
Int64 StartingLcn = (Int64)Marshal.PtrToStructure(pDest,
typeof(Int64));

Debug.Assert(StartingLcn == 0);

pDest = (IntPtr)((Int64)pDest + 8);
Int64 BitmapSize = (Int64)Marshal.PtrToStructure(pDest,
typeof(Int64));

Int32 byteSize = (int)(BitmapSize / 8);
byteSize++; // round up - even with no remainder

IntPtr BitmapBegin = (IntPtr)((Int64)pDest + 8);

byte[] byteArr = new byte[byteSize];

Marshal.Copy(BitmapBegin, byteArr, 0, (Int32)byteSize);

BitArray retVal = new BitArray(byteArr);
retVal.Length = (int)BitmapSize; // truncate to exact
cluster count
return retVal;
}
finally
{
CloseHandle(hDevice);
hDevice = IntPtr.Zero;

Marshal.FreeHGlobal(pAlloc);
pAlloc = IntPtr.Zero;
}
}

/// <summary>
/// returns a 2*number of extents array -
/// the vcn and the lcn as pairs
/// </summary>
/// <param name="path">file to get the map for ex:
"c:\windows\explorer.exe" </param>
/// <returns>An array of [virtual cluster, physical
cluster]</returns>
static public Array GetFileMap(string path)
{
IntPtr hFile = IntPtr.Zero;
IntPtr pAlloc = IntPtr.Zero;

try
{
hFile = OpenFile(path);

Int64 i64 = 0;

GCHandle handle = GCHandle.Alloc(i64, GCHandleType.Pinned);
IntPtr p = handle.AddrOfPinnedObject();

uint q = 1024 * 1024 * 64; // 1024 bytes == 1k * 1024 == 1
meg * 64 == 64 megs

uint size = 0;
pAlloc = Marshal.AllocHGlobal((int)q);
IntPtr pDest = pAlloc;
bool fResult = DeviceIoControl(
hFile,
FSConstants.FSCTL_GET_RETRIEVAL_POINTERS,
p,
(uint)Marshal.SizeOf(i64),
pDest,
q,
ref size,
IntPtr.Zero);

if (!fResult)
{
throw new
Exception(Marshal.GetLastWin32Error().ToString());
}

handle.Free();

/*
returned back one of...
typedef struct RETRIEVAL_POINTERS_BUFFER {
DWORD ExtentCount;
LARGE_INTEGER StartingVcn;
struct {
LARGE_INTEGER NextVcn;
LARGE_INTEGER Lcn;
} Extents[1];
} RETRIEVAL_POINTERS_BUFFER, *PRETRIEVAL_POINTERS_BUFFER;
*/

Int32 ExtentCount = (Int32)Marshal.PtrToStructure(pDest,
typeof(Int32));

pDest = (IntPtr)((Int64)pDest + 4);

Int64 StartingVcn = (Int64)Marshal.PtrToStructure(pDest,
typeof(Int64));

Debug.Assert(StartingVcn == 0);

pDest = (IntPtr)((Int64)pDest + 8);

// now pDest points at an array of pairs of Int64s.

Array retVal = Array.CreateInstance(typeof(Int64), new
int[2] { ExtentCount, 2 });

for (int i = 0; i < ExtentCount; i++)
{
for (int j = 0; j < 2; j++)
{
Int64 v = (Int64)Marshal.PtrToStructure(pDest,
typeof(Int64));
retVal.SetValue(v, new int[2] { i, j });
pDest = (IntPtr)((Int64)pDest + 8);
}
}

return retVal;
}
finally
{
CloseHandle(hFile);
hFile = IntPtr.Zero;

Marshal.FreeHGlobal(pAlloc);
pAlloc = IntPtr.Zero;
}
}

/// <summary>
/// input structure for use in MoveFile
/// </summary>
private struct MoveFileData
{
public IntPtr hFile;
public Int64 StartingVCN;
public Int64 StartingLCN;
public Int32 ClusterCount;
}

/// <summary>
/// move a virtual cluster for a file to a logical cluster on disk,
repeat for count clusters
/// </summary>
/// <param name="deviceName">device to move on"c:"</param>
/// <param name="path">file to muck with
"c:\windows\explorer.exe"</param>
/// <param name="VCN">cluster number in file to move</param>
/// <param name="LCN">cluster on disk to move to</param>
/// <param name="count">for how many clusters</param>
static public void MoveFile(string deviceName, string path, Int64
VCN, Int64 LCN, Int32 count)
{
IntPtr hVol = IntPtr.Zero;
IntPtr hFile = IntPtr.Zero;
try
{
hVol = OpenVolume(deviceName);

hFile = OpenFile(path);


MoveFileData mfd = new MoveFileData();
mfd.hFile = hFile;
mfd.StartingVCN = VCN;
mfd.StartingLCN = LCN;
mfd.ClusterCount = count;

GCHandle handle = GCHandle.Alloc(mfd, GCHandleType.Pinned);
IntPtr p = handle.AddrOfPinnedObject();
uint bufSize = (uint)Marshal.SizeOf(mfd);
uint size = 0;

bool fResult = DeviceIoControl(
hVol,
FSConstants.FSCTL_MOVE_FILE,
p,
bufSize,
IntPtr.Zero, // no output data from this FSCTL
0,
ref size,
IntPtr.Zero);

handle.Free();

if (!fResult)
{
throw new
Exception(Marshal.GetLastWin32Error().ToString());
}
}
finally
{
CloseHandle(hVol);
CloseHandle(hFile);
}
}
}


/// <summary>
/// constants lifted from winioctl.h from platform sdk
/// </summary>
internal class FSConstants
{
const uint FILE_DEVICE_FILE_SYSTEM = 0x00000009;

const uint METHOD_NEITHER = 3;
const uint METHOD_BUFFERED = 0;

const uint FILE_ANY_ACCESS = 0;
const uint FILE_SPECIAL_ACCESS = FILE_ANY_ACCESS;

public static uint FSCTL_GET_VOLUME_BITMAP =
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 27, METHOD_NEITHER, FILE_ANY_ACCESS);
public static uint FSCTL_GET_RETRIEVAL_POINTERS =
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 28, METHOD_NEITHER, FILE_ANY_ACCESS);
public static uint FSCTL_MOVE_FILE =
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 29, METHOD_BUFFERED, FILE_SPECIAL_ACCESS);

static uint CTL_CODE(uint DeviceType, uint Function, uint Method,
uint Access)
{
return ((DeviceType) << 16) | ((Access) << 14) | ((Function) <<
2) | (Method);
}
}

}
 
F

Frans Bouma [C# MVP]

Jim said:
I have some C# code that is supposed to wrap the defrag APIs and I am trying
to convert it to VB.Net (2003). But, I keep having problems.

The C# code is relatively short, so I'll post it here.....

I wouldn't call that 'short' ;)
I'd be very grateful for any help with this conversion that you could offer.

It would be great if you could point out the problem area's. Converting
C# to VB.NET is easy, and you should use one of the on-line conversion
sites for that. It can be that these sites/tools choke on small
constructs, (as you use win32 code, it might be the case) so you then
should convert these small constructs by hand. If that gives problems,
post THOSE constructs so people here can help you with these.

Another tip: don't bother converting, as it will not gain you anything
(it will only waste your time). Why don't you put this code into a
library and use that one in VB.NET?

Frans

--
 
G

Guest

Our Instant VB converter yields the following:
(feel free to download our Demo Edition at www.instantvb.com - it's the only
C# to VB converter that is fully supported)

Imports System
Imports System.Diagnostics
Imports System.Collections
Imports System.Runtime.InteropServices

Namespace defraglib
Public Class IOWrapper

'
' CreateFile constants
'
Private Const FILE_SHARE_READ As System.UInt32 = &H00000001
Private Const FILE_SHARE_WRITE As System.UInt32 = &H00000002
Private Const FILE_SHARE_DELETE As System.UInt32 = &H00000004
Private Const OPEN_EXISTING As System.UInt32 = 3

Private Const GENERIC_READ As System.UInt32 = (&H80000000)
Private Const GENERIC_WRITE As System.UInt32 = (&H40000000)

Private Const FILE_FLAG_NO_BUFFERING As System.UInt32 = &H20000000
Private Const FILE_READ_ATTRIBUTES As System.UInt32 = (&H0080)
Private Const FILE_WRITE_ATTRIBUTES As System.UInt32 = &H0100
Private Const ERROR_INSUFFICIENT_BUFFER As System.UInt32 = 122

<DllImport("kernel32.dll", SetLastError := True)> _
Shared Function CreateFile(ByVal lpFileName As String, ByVal
dwDesiredAccess As System.UInt32, ByVal dwShareMode As System.UInt32, ByVal
lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As System.UInt32,
ByVal dwFlagsAndAttributes As System.UInt32, ByVal hTemplateFile As IntPtr)
As IntPtr
End Function

<DllImport("kernel32.dll", SetLastError := True)> _
Shared Function CloseHandle(ByVal hObject As IntPtr) As Integer
End Function

<DllImport("kernel32.dll", SetLastError := True)> _
Shared Function DeviceIoControl(ByVal hDevice As IntPtr, ByVal
dwIoControlCode As System.UInt32, ByVal lpInBuffer As IntPtr, ByVal
nInBufferSize As System.UInt32, <Out> ByVal lpOutBuffer As IntPtr, ByVal
nOutBufferSize As System.UInt32, ByRef lpBytesReturned As System.UInt32,
ByVal lpOverlapped As IntPtr) As Boolean
End Function

Shared Private Function OpenVolume(ByVal DeviceName As String) As IntPtr
Dim hDevice As IntPtr
hDevice = CreateFile("\\.\" + DeviceName, GENERIC_READ Or GENERIC_WRITE,
FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
If CInt(hDevice) = -1 Then
Throw New Exception(Marshal.GetLastWin32Error().ToString())
End If
Return hDevice
End Function

Shared Private Function OpenFile(ByVal path As String) As IntPtr
Dim hFile As IntPtr
hFile = CreateFile(path, FILE_READ_ATTRIBUTES Or FILE_WRITE_ATTRIBUTES,
FILE_SHARE_READ Or FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero)
If CInt(hFile) = -1 Then
Throw New Exception(Marshal.GetLastWin32Error().ToString())
End If
Return hFile
End Function


''' <summary>
''' Get cluster usage for a device
''' </summary>
''' <param name="DeviceName">use "c:"</param>
''' <returns>a bitarray for each cluster</returns>
Shared Public Function GetVolumeMap(ByVal DeviceName As String) As BitArray
Dim pAlloc As IntPtr = IntPtr.Zero
Dim hDevice As IntPtr = IntPtr.Zero

Try
hDevice = OpenVolume(DeviceName)

Dim i64 As Int64 = 0

Dim handle As GCHandle = GCHandle.Alloc(i64, GCHandleType.Pinned)
Dim p As IntPtr = handle.AddrOfPinnedObject()

' alloc off more than enough for my machine
' 64 megs == 67108864 bytes == 536870912 bits == cluster count
' NTFS 4k clusters == 2147483648 k of storage == 2097152 megs == 2048
gig disk storage
Dim q As System.UInt32 = 1024 * 1024 * 64 ' 1024 bytes == 1k * 1024 == 1
meg * 64 == 64 megs

Dim size As System.UInt32
pAlloc = Marshal.AllocHGlobal(CInt(q))
Dim pDest As IntPtr = pAlloc

Dim fResult As Boolean = DeviceIoControl(hDevice,
FSConstants.FSCTL_GET_VOLUME_BITMAP, p, CUInt(Marshal.SizeOf(i64)), pDest, q,
size, IntPtr.Zero)

If (Not fResult) Then
Throw New Exception(Marshal.GetLastWin32Error().ToString())
End If
handle.Free()

'
'object returned was...
'typedef struct
'{
'LARGE_INTEGER StartingLcn;
'LARGE_INTEGER BitmapSize;
'BYTE Buffer[1];
'} VOLUME_BITMAP_BUFFER, *PVOLUME_BITMAP_BUFFER;
'
Dim StartingLcn As Int64 = CLng(Marshal.PtrToStructure(pDest,
GetType(Int64)))

Debug.Assert(StartingLcn = 0)

pDest = CType(CLng(pDest) + 8, IntPtr)
Dim BitmapSize As Int64 = CLng(Marshal.PtrToStructure(pDest,
GetType(Int64)))

Dim byteSize As Int32 = CInt(BitmapSize / 8)
byteSize += 1 ' round up - even with no remainder

Dim BitmapBegin As IntPtr = CType(CLng(pDest) + 8, IntPtr)

Dim byteArr As Byte() = New Byte(byteSize - 1) {}

Marshal.Copy(BitmapBegin, byteArr, 0, CInt(byteSize))

Dim retVal As BitArray = New BitArray(byteArr)
retVal.Length = CInt(BitmapSize) ' truncate to exact cluster count
Return retVal
Finally
CloseHandle(hDevice)
hDevice = IntPtr.Zero

Marshal.FreeHGlobal(pAlloc)
pAlloc = IntPtr.Zero
End Try
End Function

''' <summary>
''' returns a 2*number of extents array -
''' the vcn and the lcn as pairs
''' </summary>
''' <param name="path">file to get the map for ex:
"c:\windows\explorer.exe" </param>
''' <returns>An array of [virtual cluster, physical cluster]</returns>
Shared Public Function GetFileMap(ByVal path As String) As Array
Dim hFile As IntPtr = IntPtr.Zero
Dim pAlloc As IntPtr = IntPtr.Zero

Try
hFile = OpenFile(path)

Dim i64 As Int64 = 0

Dim handle As GCHandle = GCHandle.Alloc(i64, GCHandleType.Pinned)
Dim p As IntPtr = handle.AddrOfPinnedObject()

Dim q As System.UInt32 = 1024 * 1024 * 64 ' 1024 bytes == 1k * 1024 == 1
meg * 64 == 64 megs

Dim size As System.UInt32
pAlloc = Marshal.AllocHGlobal(CInt(q))
Dim pDest As IntPtr = pAlloc
Dim fResult As Boolean = DeviceIoControl(hFile,
FSConstants.FSCTL_GET_RETRIEVAL_POINTERS, p, CUInt(Marshal.SizeOf(i64)),
pDest, q, size, IntPtr.Zero)

If (Not fResult) Then
Throw New Exception(Marshal.GetLastWin32Error().ToString())
End If

handle.Free()

'
'returned back one of...
'typedef struct RETRIEVAL_POINTERS_BUFFER {
'DWORD ExtentCount;
'LARGE_INTEGER StartingVcn;
'struct {
'LARGE_INTEGER NextVcn;
'LARGE_INTEGER Lcn;
'} Extents[1];
'} RETRIEVAL_POINTERS_BUFFER, *PRETRIEVAL_POINTERS_BUFFER;
'

Dim ExtentCount As Int32 = CInt(Marshal.PtrToStructure(pDest,
GetType(Int32)))

pDest = CType(CLng(pDest) + 4, IntPtr)

Dim StartingVcn As Int64 = CLng(Marshal.PtrToStructure(pDest,
GetType(Int64)))

Debug.Assert(StartingVcn = 0)

pDest = CType(CLng(pDest) + 8, IntPtr)

' now pDest points at an array of pairs of Int64s.

Dim retVal As Array = Array.CreateInstance(GetType(Int64), New
Integer(1) { ExtentCount, 2 })

For i As Integer = 0 To ExtentCount - 1
For j As Integer = 0 To 1
Dim v As Int64 = CLng(Marshal.PtrToStructure(pDest, GetType(Int64)))
retVal.SetValue(v, New Integer(1) { i, j })
pDest = CType(CLng(pDest) + 8, IntPtr)
Next j
Next i

Return retVal
Finally
CloseHandle(hFile)
hFile = IntPtr.Zero

Marshal.FreeHGlobal(pAlloc)
pAlloc = IntPtr.Zero
End Try
End Function

''' <summary>
''' input structure for use in MoveFile
''' </summary>
Private Structure MoveFileData
Public hFile As IntPtr
Public StartingVCN As Int64
Public StartingLCN As Int64
Public ClusterCount As Int32
End Structure

''' <summary>
''' move a virtual cluster for a file to a logical cluster on disk, repeat
for count clusters
''' </summary>
''' <param name="deviceName">device to move on"c:"</param>
''' <param name="path">file to muck with "c:\windows\explorer.exe"</param>
''' <param name="VCN">cluster number in file to move</param>
''' <param name="LCN">cluster on disk to move to</param>
''' <param name="count">for how many clusters</param>
Shared Public Sub MoveFile(ByVal deviceName As String, ByVal path As
String, ByVal VCN As Int64, ByVal LCN As Int64, ByVal count As Int32)
Dim hVol As IntPtr = IntPtr.Zero
Dim hFile As IntPtr = IntPtr.Zero
Try
hVol = OpenVolume(deviceName)

hFile = OpenFile(path)


Dim mfd As MoveFileData = New MoveFileData()
mfd.hFile = hFile
mfd.StartingVCN = VCN
mfd.StartingLCN = LCN
mfd.ClusterCount = count

Dim handle As GCHandle = GCHandle.Alloc(mfd, GCHandleType.Pinned)
Dim p As IntPtr = handle.AddrOfPinnedObject()
Dim bufSize As System.UInt32 = CUInt(Marshal.SizeOf(mfd))
Dim size As System.UInt32

Dim fResult As Boolean = DeviceIoControl(hVol,
FSConstants.FSCTL_MOVE_FILE, p, bufSize, IntPtr.Zero, 0, size, IntPtr.Zero)

handle.Free()

If (Not fResult) Then
Throw New Exception(Marshal.GetLastWin32Error().ToString())
End If
Finally
CloseHandle(hVol)
CloseHandle(hFile)
End Try
End Sub
End Class


''' <summary>
''' constants lifted from winioctl.h from platform sdk
''' </summary>
Friend Class FSConstants
Private Const FILE_DEVICE_FILE_SYSTEM As System.UInt32 = &H00000009

Private Const METHOD_NEITHER As System.UInt32 = 3
Private Const METHOD_BUFFERED As System.UInt32

Private Const FILE_ANY_ACCESS As System.UInt32
Private Const FILE_SPECIAL_ACCESS As System.UInt32 = FILE_ANY_ACCESS

Public Shared FSCTL_GET_VOLUME_BITMAP As System.UInt32 =
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 27, METHOD_NEITHER, FILE_ANY_ACCESS)
Public Shared FSCTL_GET_RETRIEVAL_POINTERS As System.UInt32 =
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 28, METHOD_NEITHER, FILE_ANY_ACCESS)
Public Shared FSCTL_MOVE_FILE As System.UInt32 =
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 29, METHOD_BUFFERED, FILE_SPECIAL_ACCESS)

'TODO: INSTANT VB TODO TASK: Function is a keyword in VB.NET. Change the
name or use square brackets to override it:
Private Shared Function CTL_CODE(ByVal DeviceType As System.UInt32, ByVal
Function As System.UInt32, ByVal Method As System.UInt32, ByVal Access As
System.UInt32) As System.UInt32
Return ((DeviceType) << 16) Or ((Access) << 14) Or ((Function) << 2) Or
(Method)
End Function
End Class

End Namespace


Jim Hubbard said:
I have some C# code that is supposed to wrap the defrag APIs and I am trying
to convert it to VB.Net (2003). But, I keep having problems.

The C# code is relatively short, so I'll post it here.....

--------
//
// a set of simple C# wrappers over the NT Defragmenter APIs
//

//
// Refrences

//
// http://www.sysinternals.com/ntw2k/info/defrag.shtml
//
// msdn how-to
// ms-help://MS.MSDNQTR.2004JUL.1033/fileio/base/defragmenting_files.htm
//
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/defragmenting_files.asp
//
// FSCTL_GET_VOLUME_BITMAP
//
http://msdn.microsoft.com/library/d...en-us/fileio/base/fsctl_get_volume_bitmap.asp
//
// interesting structures...
// FSCTL_MOVE_FILE
// FSCTL_GET_RETRIEVAL_POINTERS
// RETRIEVAL_POINTERS_BUFFER
// FSCTL_GET_RETRIEVAL_POINTERS
//
// DeviceIoControl
//
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/deviceiocontrol.asp
//

using System;
using System.Diagnostics;
using System.Collections;
using System.Runtime.InteropServices;

namespace defraglib
{
public class IOWrapper
{

//
// CreateFile constants
//
const uint FILE_SHARE_READ = 0x00000001;
const uint FILE_SHARE_WRITE = 0x00000002;
const uint FILE_SHARE_DELETE = 0x00000004;
const uint OPEN_EXISTING = 3;

const uint GENERIC_READ = (0x80000000);
const uint GENERIC_WRITE = (0x40000000);

const uint FILE_FLAG_NO_BUFFERING = 0x20000000;
const uint FILE_READ_ATTRIBUTES = (0x0080);
const uint FILE_WRITE_ATTRIBUTES = 0x0100;
const uint ERROR_INSUFFICIENT_BUFFER = 122;

[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr CreateFile(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);

[DllImport("kernel32.dll", SetLastError = true)]
static extern int CloseHandle(IntPtr hObject);

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool DeviceIoControl(
IntPtr hDevice,
uint dwIoControlCode,
IntPtr lpInBuffer,
uint nInBufferSize,
[Out] IntPtr lpOutBuffer,
uint nOutBufferSize,
ref uint lpBytesReturned,
IntPtr lpOverlapped);

static private IntPtr OpenVolume(string DeviceName)
{
IntPtr hDevice;
hDevice = CreateFile(
@"\\.\" + DeviceName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
0,
IntPtr.Zero);
if ((int)hDevice == -1)
{
throw new Exception(Marshal.GetLastWin32Error().ToString());
}
return hDevice;
}

static private IntPtr OpenFile(string path)
{
IntPtr hFile;
hFile = CreateFile(
path,
FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
0,
IntPtr.Zero);
if ((int)hFile == -1)
{
throw new Exception(Marshal.GetLastWin32Error().ToString());
}
return hFile;
}


/// <summary>
/// Get cluster usage for a device
/// </summary>
/// <param name="DeviceName">use "c:"</param>
/// <returns>a bitarray for each cluster</returns>
static public BitArray GetVolumeMap(string DeviceName)
{
IntPtr pAlloc = IntPtr.Zero;
IntPtr hDevice = IntPtr.Zero;

try
{
hDevice = OpenVolume(DeviceName);

Int64 i64 = 0;

GCHandle handle = GCHandle.Alloc(i64, GCHandleType.Pinned);
IntPtr p = handle.AddrOfPinnedObject();

// alloc off more than enough for my machine
// 64 megs == 67108864 bytes == 536870912 bits == cluster
count
// NTFS 4k clusters == 2147483648 k of storage == 2097152
megs == 2048 gig disk storage
uint q = 1024 * 1024 * 64; // 1024 bytes == 1k * 1024 == 1
meg * 64 == 64 megs

uint size = 0;
pAlloc = Marshal.AllocHGlobal((int)q);
IntPtr pDest = pAlloc;

bool fResult = DeviceIoControl(
hDevice,
FSConstants.FSCTL_GET_VOLUME_BITMAP,
p,
(uint)Marshal.SizeOf(i64),
pDest,
q,
ref size,
IntPtr.Zero);

if (!fResult)
{
throw new
Exception(Marshal.GetLastWin32Error().ToString());
}
handle.Free();

/*
object returned was...
typedef struct
{
LARGE_INTEGER StartingLcn;
LARGE_INTEGER BitmapSize;
BYTE Buffer[1];
} VOLUME_BITMAP_BUFFER, *PVOLUME_BITMAP_BUFFER;
*/
Int64 StartingLcn = (Int64)Marshal.PtrToStructure(pDest,
typeof(Int64));

Debug.Assert(StartingLcn == 0);

pDest = (IntPtr)((Int64)pDest + 8);
Int64 BitmapSize = (Int64)Marshal.PtrToStructure(pDest,
typeof(Int64));

Int32 byteSize = (int)(BitmapSize / 8);
byteSize++; // round up - even with no remainder

IntPtr BitmapBegin = (IntPtr)((Int64)pDest + 8);

byte[] byteArr = new byte[byteSize];

Marshal.Copy(BitmapBegin, byteArr, 0, (Int32)byteSize);

BitArray retVal = new BitArray(byteArr);
retVal.Length = (int)BitmapSize; // truncate to exact
cluster count
return retVal;
}
finally
{
CloseHandle(hDevice);
hDevice = IntPtr.Zero;

Marshal.FreeHGlobal(pAlloc);
pAlloc = IntPtr.Zero;
}
}

/// <summary>
/// returns a 2*number of extents array -
/// the vcn and the lcn as pairs
/// </summary>
/// <param name="path">file to get the map for ex:
"c:\windows\explorer.exe" </param>
/// <returns>An array of [virtual cluster, physical
cluster]</returns>
static public Array GetFileMap(string path)
{
IntPtr hFile = IntPtr.Zero;
IntPtr pAlloc = IntPtr.Zero;

try
{
hFile = OpenFile(path);

Int64 i64 = 0;

GCHandle handle = GCHandle.Alloc(i64, GCHandleType.Pinned);
IntPtr p = handle.AddrOfPinnedObject();

uint q = 1024 * 1024 * 64; // 1024 bytes == 1k * 1024 == 1
meg * 64 == 64 megs

uint size = 0;
pAlloc = Marshal.AllocHGlobal((int)q);
IntPtr pDest = pAlloc;
bool fResult = DeviceIoControl(
hFile,
FSConstants.FSCTL_GET_RETRIEVAL_POINTERS,
p,
(uint)Marshal.SizeOf(i64),
pDest,
q,
ref size,
IntPtr.Zero);

if (!fResult)
{
throw new
Exception(Marshal.GetLastWin32Error().ToString());
}

handle.Free();

/*
returned back one of...
typedef struct RETRIEVAL_POINTERS_BUFFER {
DWORD ExtentCount;
LARGE_INTEGER StartingVcn;
struct {
LARGE_INTEGER NextVcn;
LARGE_INTEGER Lcn;
} Extents[1];
} RETRIEVAL_POINTERS_BUFFER, *PRETRIEVAL_POINTERS_BUFFER;
*/

Int32 ExtentCount = (Int32)Marshal.PtrToStructure(pDest,
typeof(Int32));

pDest = (IntPtr)((Int64)pDest + 4);

Int64 StartingVcn = (Int64)Marshal.PtrToStructure(pDest,
typeof(Int64));

Debug.Assert(StartingVcn == 0);

pDest = (IntPtr)((Int64)pDest + 8);

// now pDest points at an array of pairs of Int64s.

Array retVal = Array.CreateInstance(typeof(Int64), new
int[2] { ExtentCount, 2 });

for (int i = 0; i < ExtentCount; i++)
{
for (int j = 0; j < 2; j++)
{
Int64 v = (Int64)Marshal.PtrToStructure(pDest,
typeof(Int64));
retVal.SetValue(v, new int[2] { i, j });
pDest = (IntPtr)((Int64)pDest + 8);
}
}

return retVal;
}
finally
{
CloseHandle(hFile);
hFile = IntPtr.Zero;

Marshal.FreeHGlobal(pAlloc);
pAlloc = IntPtr.Zero;
}
}

/// <summary>
/// input structure for use in MoveFile
/// </summary>
private struct MoveFileData
{
public IntPtr hFile;
public Int64 StartingVCN;
public Int64 StartingLCN;
public Int32 ClusterCount;
}

/// <summary>
/// move a virtual cluster for a file to a logical cluster on disk,
repeat for count clusters
/// </summary>
/// <param name="deviceName">device to move on"c:"</param>
/// <param name="path">file to muck with
"c:\windows\explorer.exe"</param>
/// <param name="VCN">cluster number in file to move</param>
/// <param name="LCN">cluster on disk to move to</param>
/// <param name="count">for how many clusters</param>
static public void MoveFile(string deviceName, string path, Int64
VCN, Int64 LCN, Int32 count)
{
IntPtr hVol = IntPtr.Zero;
IntPtr hFile = IntPtr.Zero;
try
{
hVol = OpenVolume(deviceName);

hFile = OpenFile(path);


MoveFileData mfd = new MoveFileData();
mfd.hFile = hFile;
mfd.StartingVCN = VCN;
mfd.StartingLCN = LCN;
mfd.ClusterCount = count;

GCHandle handle = GCHandle.Alloc(mfd, GCHandleType.Pinned);
IntPtr p = handle.AddrOfPinnedObject();
uint bufSize = (uint)Marshal.SizeOf(mfd);
uint size = 0;

bool fResult = DeviceIoControl(
hVol,
FSConstants.FSCTL_MOVE_FILE,
p,
bufSize,
IntPtr.Zero, // no output data from this FSCTL
0,
ref size,
IntPtr.Zero);

handle.Free();

if (!fResult)
{
throw new
Exception(Marshal.GetLastWin32Error().ToString());
}
}
finally
{
CloseHandle(hVol);
CloseHandle(hFile);
}
}
}


/// <summary>
/// constants lifted from winioctl.h from platform sdk
/// </summary>
internal class FSConstants
{
const uint FILE_DEVICE_FILE_SYSTEM = 0x00000009;

const uint METHOD_NEITHER = 3;
const uint METHOD_BUFFERED = 0;

const uint FILE_ANY_ACCESS = 0;
const uint FILE_SPECIAL_ACCESS = FILE_ANY_ACCESS;

public static uint FSCTL_GET_VOLUME_BITMAP =
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 27, METHOD_NEITHER, FILE_ANY_ACCESS);
public static uint FSCTL_GET_RETRIEVAL_POINTERS =
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 28, METHOD_NEITHER, FILE_ANY_ACCESS);
public static uint FSCTL_MOVE_FILE =
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 29, METHOD_BUFFERED, FILE_SPECIAL_ACCESS);

static uint CTL_CODE(uint DeviceType, uint Function, uint Method,
uint Access)
{
return ((DeviceType) << 16) | ((Access) << 14) | ((Function) <<
2) | (Method);
}
}

}

-----

I'd be very grateful for any help with this conversion that you could offer.

Thanks!
 
J

Jim Hubbard

David! That's awesome!

Better than anything I've seen!

Thanks!

David Anton said:
Our Instant VB converter yields the following:
(feel free to download our Demo Edition at www.instantvb.com - it's the
only
C# to VB converter that is fully supported)

Imports System
Imports System.Diagnostics
Imports System.Collections
Imports System.Runtime.InteropServices

Namespace defraglib
Public Class IOWrapper

'
' CreateFile constants
'
Private Const FILE_SHARE_READ As System.UInt32 = &H00000001
Private Const FILE_SHARE_WRITE As System.UInt32 = &H00000002
Private Const FILE_SHARE_DELETE As System.UInt32 = &H00000004
Private Const OPEN_EXISTING As System.UInt32 = 3

Private Const GENERIC_READ As System.UInt32 = (&H80000000)
Private Const GENERIC_WRITE As System.UInt32 = (&H40000000)

Private Const FILE_FLAG_NO_BUFFERING As System.UInt32 = &H20000000
Private Const FILE_READ_ATTRIBUTES As System.UInt32 = (&H0080)
Private Const FILE_WRITE_ATTRIBUTES As System.UInt32 = &H0100
Private Const ERROR_INSUFFICIENT_BUFFER As System.UInt32 = 122

<DllImport("kernel32.dll", SetLastError := True)> _
Shared Function CreateFile(ByVal lpFileName As String, ByVal
dwDesiredAccess As System.UInt32, ByVal dwShareMode As System.UInt32,
ByVal
lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As
System.UInt32,
ByVal dwFlagsAndAttributes As System.UInt32, ByVal hTemplateFile As
IntPtr)
As IntPtr
End Function

<DllImport("kernel32.dll", SetLastError := True)> _
Shared Function CloseHandle(ByVal hObject As IntPtr) As Integer
End Function

<DllImport("kernel32.dll", SetLastError := True)> _
Shared Function DeviceIoControl(ByVal hDevice As IntPtr, ByVal
dwIoControlCode As System.UInt32, ByVal lpInBuffer As IntPtr, ByVal
nInBufferSize As System.UInt32, <Out> ByVal lpOutBuffer As IntPtr, ByVal
nOutBufferSize As System.UInt32, ByRef lpBytesReturned As System.UInt32,
ByVal lpOverlapped As IntPtr) As Boolean
End Function

Shared Private Function OpenVolume(ByVal DeviceName As String) As IntPtr
Dim hDevice As IntPtr
hDevice = CreateFile("\\.\" + DeviceName, GENERIC_READ Or GENERIC_WRITE,
FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
If CInt(hDevice) = -1 Then
Throw New Exception(Marshal.GetLastWin32Error().ToString())
End If
Return hDevice
End Function

Shared Private Function OpenFile(ByVal path As String) As IntPtr
Dim hFile As IntPtr
hFile = CreateFile(path, FILE_READ_ATTRIBUTES Or FILE_WRITE_ATTRIBUTES,
FILE_SHARE_READ Or FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero)
If CInt(hFile) = -1 Then
Throw New Exception(Marshal.GetLastWin32Error().ToString())
End If
Return hFile
End Function


''' <summary>
''' Get cluster usage for a device
''' </summary>
''' <param name="DeviceName">use "c:"</param>
''' <returns>a bitarray for each cluster</returns>
Shared Public Function GetVolumeMap(ByVal DeviceName As String) As
BitArray
Dim pAlloc As IntPtr = IntPtr.Zero
Dim hDevice As IntPtr = IntPtr.Zero

Try
hDevice = OpenVolume(DeviceName)

Dim i64 As Int64 = 0

Dim handle As GCHandle = GCHandle.Alloc(i64, GCHandleType.Pinned)
Dim p As IntPtr = handle.AddrOfPinnedObject()

' alloc off more than enough for my machine
' 64 megs == 67108864 bytes == 536870912 bits == cluster count
' NTFS 4k clusters == 2147483648 k of storage == 2097152 megs == 2048
gig disk storage
Dim q As System.UInt32 = 1024 * 1024 * 64 ' 1024 bytes == 1k * 1024 == 1
meg * 64 == 64 megs

Dim size As System.UInt32
pAlloc = Marshal.AllocHGlobal(CInt(q))
Dim pDest As IntPtr = pAlloc

Dim fResult As Boolean = DeviceIoControl(hDevice,
FSConstants.FSCTL_GET_VOLUME_BITMAP, p, CUInt(Marshal.SizeOf(i64)), pDest,
q,
size, IntPtr.Zero)

If (Not fResult) Then
Throw New Exception(Marshal.GetLastWin32Error().ToString())
End If
handle.Free()

'
'object returned was...
'typedef struct
'{
'LARGE_INTEGER StartingLcn;
'LARGE_INTEGER BitmapSize;
'BYTE Buffer[1];
'} VOLUME_BITMAP_BUFFER, *PVOLUME_BITMAP_BUFFER;
'
Dim StartingLcn As Int64 = CLng(Marshal.PtrToStructure(pDest,
GetType(Int64)))

Debug.Assert(StartingLcn = 0)

pDest = CType(CLng(pDest) + 8, IntPtr)
Dim BitmapSize As Int64 = CLng(Marshal.PtrToStructure(pDest,
GetType(Int64)))

Dim byteSize As Int32 = CInt(BitmapSize / 8)
byteSize += 1 ' round up - even with no remainder

Dim BitmapBegin As IntPtr = CType(CLng(pDest) + 8, IntPtr)

Dim byteArr As Byte() = New Byte(byteSize - 1) {}

Marshal.Copy(BitmapBegin, byteArr, 0, CInt(byteSize))

Dim retVal As BitArray = New BitArray(byteArr)
retVal.Length = CInt(BitmapSize) ' truncate to exact cluster count
Return retVal
Finally
CloseHandle(hDevice)
hDevice = IntPtr.Zero

Marshal.FreeHGlobal(pAlloc)
pAlloc = IntPtr.Zero
End Try
End Function

''' <summary>
''' returns a 2*number of extents array -
''' the vcn and the lcn as pairs
''' </summary>
''' <param name="path">file to get the map for ex:
"c:\windows\explorer.exe" </param>
''' <returns>An array of [virtual cluster, physical cluster]</returns>
Shared Public Function GetFileMap(ByVal path As String) As Array
Dim hFile As IntPtr = IntPtr.Zero
Dim pAlloc As IntPtr = IntPtr.Zero

Try
hFile = OpenFile(path)

Dim i64 As Int64 = 0

Dim handle As GCHandle = GCHandle.Alloc(i64, GCHandleType.Pinned)
Dim p As IntPtr = handle.AddrOfPinnedObject()

Dim q As System.UInt32 = 1024 * 1024 * 64 ' 1024 bytes == 1k * 1024 == 1
meg * 64 == 64 megs

Dim size As System.UInt32
pAlloc = Marshal.AllocHGlobal(CInt(q))
Dim pDest As IntPtr = pAlloc
Dim fResult As Boolean = DeviceIoControl(hFile,
FSConstants.FSCTL_GET_RETRIEVAL_POINTERS, p, CUInt(Marshal.SizeOf(i64)),
pDest, q, size, IntPtr.Zero)

If (Not fResult) Then
Throw New Exception(Marshal.GetLastWin32Error().ToString())
End If

handle.Free()

'
'returned back one of...
'typedef struct RETRIEVAL_POINTERS_BUFFER {
'DWORD ExtentCount;
'LARGE_INTEGER StartingVcn;
'struct {
'LARGE_INTEGER NextVcn;
'LARGE_INTEGER Lcn;
'} Extents[1];
'} RETRIEVAL_POINTERS_BUFFER, *PRETRIEVAL_POINTERS_BUFFER;
'

Dim ExtentCount As Int32 = CInt(Marshal.PtrToStructure(pDest,
GetType(Int32)))

pDest = CType(CLng(pDest) + 4, IntPtr)

Dim StartingVcn As Int64 = CLng(Marshal.PtrToStructure(pDest,
GetType(Int64)))

Debug.Assert(StartingVcn = 0)

pDest = CType(CLng(pDest) + 8, IntPtr)

' now pDest points at an array of pairs of Int64s.

Dim retVal As Array = Array.CreateInstance(GetType(Int64), New
Integer(1) { ExtentCount, 2 })

For i As Integer = 0 To ExtentCount - 1
For j As Integer = 0 To 1
Dim v As Int64 = CLng(Marshal.PtrToStructure(pDest, GetType(Int64)))
retVal.SetValue(v, New Integer(1) { i, j })
pDest = CType(CLng(pDest) + 8, IntPtr)
Next j
Next i

Return retVal
Finally
CloseHandle(hFile)
hFile = IntPtr.Zero

Marshal.FreeHGlobal(pAlloc)
pAlloc = IntPtr.Zero
End Try
End Function

''' <summary>
''' input structure for use in MoveFile
''' </summary>
Private Structure MoveFileData
Public hFile As IntPtr
Public StartingVCN As Int64
Public StartingLCN As Int64
Public ClusterCount As Int32
End Structure

''' <summary>
''' move a virtual cluster for a file to a logical cluster on disk, repeat
for count clusters
''' </summary>
''' <param name="deviceName">device to move on"c:"</param>
''' <param name="path">file to muck with "c:\windows\explorer.exe"</param>
''' <param name="VCN">cluster number in file to move</param>
''' <param name="LCN">cluster on disk to move to</param>
''' <param name="count">for how many clusters</param>
Shared Public Sub MoveFile(ByVal deviceName As String, ByVal path As
String, ByVal VCN As Int64, ByVal LCN As Int64, ByVal count As Int32)
Dim hVol As IntPtr = IntPtr.Zero
Dim hFile As IntPtr = IntPtr.Zero
Try
hVol = OpenVolume(deviceName)

hFile = OpenFile(path)


Dim mfd As MoveFileData = New MoveFileData()
mfd.hFile = hFile
mfd.StartingVCN = VCN
mfd.StartingLCN = LCN
mfd.ClusterCount = count

Dim handle As GCHandle = GCHandle.Alloc(mfd, GCHandleType.Pinned)
Dim p As IntPtr = handle.AddrOfPinnedObject()
Dim bufSize As System.UInt32 = CUInt(Marshal.SizeOf(mfd))
Dim size As System.UInt32

Dim fResult As Boolean = DeviceIoControl(hVol,
FSConstants.FSCTL_MOVE_FILE, p, bufSize, IntPtr.Zero, 0, size,
IntPtr.Zero)

handle.Free()

If (Not fResult) Then
Throw New Exception(Marshal.GetLastWin32Error().ToString())
End If
Finally
CloseHandle(hVol)
CloseHandle(hFile)
End Try
End Sub
End Class


''' <summary>
''' constants lifted from winioctl.h from platform sdk
''' </summary>
Friend Class FSConstants
Private Const FILE_DEVICE_FILE_SYSTEM As System.UInt32 = &H00000009

Private Const METHOD_NEITHER As System.UInt32 = 3
Private Const METHOD_BUFFERED As System.UInt32

Private Const FILE_ANY_ACCESS As System.UInt32
Private Const FILE_SPECIAL_ACCESS As System.UInt32 = FILE_ANY_ACCESS

Public Shared FSCTL_GET_VOLUME_BITMAP As System.UInt32 =
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 27, METHOD_NEITHER, FILE_ANY_ACCESS)
Public Shared FSCTL_GET_RETRIEVAL_POINTERS As System.UInt32 =
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 28, METHOD_NEITHER, FILE_ANY_ACCESS)
Public Shared FSCTL_MOVE_FILE As System.UInt32 =
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 29, METHOD_BUFFERED,
FILE_SPECIAL_ACCESS)

'TODO: INSTANT VB TODO TASK: Function is a keyword in VB.NET. Change the
name or use square brackets to override it:
Private Shared Function CTL_CODE(ByVal DeviceType As System.UInt32, ByVal
Function As System.UInt32, ByVal Method As System.UInt32, ByVal Access As
System.UInt32) As System.UInt32
Return ((DeviceType) << 16) Or ((Access) << 14) Or ((Function) << 2) Or
(Method)
End Function
End Class

End Namespace


Jim Hubbard said:
I have some C# code that is supposed to wrap the defrag APIs and I am
trying
to convert it to VB.Net (2003). But, I keep having problems.

The C# code is relatively short, so I'll post it here.....

--------
//
// a set of simple C# wrappers over the NT Defragmenter APIs
//

//
// Refrences

//
// http://www.sysinternals.com/ntw2k/info/defrag.shtml
//
// msdn how-to
// ms-help://MS.MSDNQTR.2004JUL.1033/fileio/base/defragmenting_files.htm
//
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/defragmenting_files.asp
//
// FSCTL_GET_VOLUME_BITMAP
//
http://msdn.microsoft.com/library/d...en-us/fileio/base/fsctl_get_volume_bitmap.asp
//
// interesting structures...
// FSCTL_MOVE_FILE
// FSCTL_GET_RETRIEVAL_POINTERS
// RETRIEVAL_POINTERS_BUFFER
// FSCTL_GET_RETRIEVAL_POINTERS
//
// DeviceIoControl
//
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/deviceiocontrol.asp
//

using System;
using System.Diagnostics;
using System.Collections;
using System.Runtime.InteropServices;

namespace defraglib
{
public class IOWrapper
{

//
// CreateFile constants
//
const uint FILE_SHARE_READ = 0x00000001;
const uint FILE_SHARE_WRITE = 0x00000002;
const uint FILE_SHARE_DELETE = 0x00000004;
const uint OPEN_EXISTING = 3;

const uint GENERIC_READ = (0x80000000);
const uint GENERIC_WRITE = (0x40000000);

const uint FILE_FLAG_NO_BUFFERING = 0x20000000;
const uint FILE_READ_ATTRIBUTES = (0x0080);
const uint FILE_WRITE_ATTRIBUTES = 0x0100;
const uint ERROR_INSUFFICIENT_BUFFER = 122;

[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr CreateFile(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);

[DllImport("kernel32.dll", SetLastError = true)]
static extern int CloseHandle(IntPtr hObject);

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool DeviceIoControl(
IntPtr hDevice,
uint dwIoControlCode,
IntPtr lpInBuffer,
uint nInBufferSize,
[Out] IntPtr lpOutBuffer,
uint nOutBufferSize,
ref uint lpBytesReturned,
IntPtr lpOverlapped);

static private IntPtr OpenVolume(string DeviceName)
{
IntPtr hDevice;
hDevice = CreateFile(
@"\\.\" + DeviceName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
0,
IntPtr.Zero);
if ((int)hDevice == -1)
{
throw new
Exception(Marshal.GetLastWin32Error().ToString());
}
return hDevice;
}

static private IntPtr OpenFile(string path)
{
IntPtr hFile;
hFile = CreateFile(
path,
FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
0,
IntPtr.Zero);
if ((int)hFile == -1)
{
throw new
Exception(Marshal.GetLastWin32Error().ToString());
}
return hFile;
}


/// <summary>
/// Get cluster usage for a device
/// </summary>
/// <param name="DeviceName">use "c:"</param>
/// <returns>a bitarray for each cluster</returns>
static public BitArray GetVolumeMap(string DeviceName)
{
IntPtr pAlloc = IntPtr.Zero;
IntPtr hDevice = IntPtr.Zero;

try
{
hDevice = OpenVolume(DeviceName);

Int64 i64 = 0;

GCHandle handle = GCHandle.Alloc(i64,
GCHandleType.Pinned);
IntPtr p = handle.AddrOfPinnedObject();

// alloc off more than enough for my machine
// 64 megs == 67108864 bytes == 536870912 bits == cluster
count
// NTFS 4k clusters == 2147483648 k of storage == 2097152
megs == 2048 gig disk storage
uint q = 1024 * 1024 * 64; // 1024 bytes == 1k * 1024 ==
1
meg * 64 == 64 megs

uint size = 0;
pAlloc = Marshal.AllocHGlobal((int)q);
IntPtr pDest = pAlloc;

bool fResult = DeviceIoControl(
hDevice,
FSConstants.FSCTL_GET_VOLUME_BITMAP,
p,
(uint)Marshal.SizeOf(i64),
pDest,
q,
ref size,
IntPtr.Zero);

if (!fResult)
{
throw new
Exception(Marshal.GetLastWin32Error().ToString());
}
handle.Free();

/*
object returned was...
typedef struct
{
LARGE_INTEGER StartingLcn;
LARGE_INTEGER BitmapSize;
BYTE Buffer[1];
} VOLUME_BITMAP_BUFFER, *PVOLUME_BITMAP_BUFFER;
*/
Int64 StartingLcn = (Int64)Marshal.PtrToStructure(pDest,
typeof(Int64));

Debug.Assert(StartingLcn == 0);

pDest = (IntPtr)((Int64)pDest + 8);
Int64 BitmapSize = (Int64)Marshal.PtrToStructure(pDest,
typeof(Int64));

Int32 byteSize = (int)(BitmapSize / 8);
byteSize++; // round up - even with no remainder

IntPtr BitmapBegin = (IntPtr)((Int64)pDest + 8);

byte[] byteArr = new byte[byteSize];

Marshal.Copy(BitmapBegin, byteArr, 0, (Int32)byteSize);

BitArray retVal = new BitArray(byteArr);
retVal.Length = (int)BitmapSize; // truncate to exact
cluster count
return retVal;
}
finally
{
CloseHandle(hDevice);
hDevice = IntPtr.Zero;

Marshal.FreeHGlobal(pAlloc);
pAlloc = IntPtr.Zero;
}
}

/// <summary>
/// returns a 2*number of extents array -
/// the vcn and the lcn as pairs
/// </summary>
/// <param name="path">file to get the map for ex:
"c:\windows\explorer.exe" </param>
/// <returns>An array of [virtual cluster, physical
cluster]</returns>
static public Array GetFileMap(string path)
{
IntPtr hFile = IntPtr.Zero;
IntPtr pAlloc = IntPtr.Zero;

try
{
hFile = OpenFile(path);

Int64 i64 = 0;

GCHandle handle = GCHandle.Alloc(i64,
GCHandleType.Pinned);
IntPtr p = handle.AddrOfPinnedObject();

uint q = 1024 * 1024 * 64; // 1024 bytes == 1k * 1024 ==
1
meg * 64 == 64 megs

uint size = 0;
pAlloc = Marshal.AllocHGlobal((int)q);
IntPtr pDest = pAlloc;
bool fResult = DeviceIoControl(
hFile,
FSConstants.FSCTL_GET_RETRIEVAL_POINTERS,
p,
(uint)Marshal.SizeOf(i64),
pDest,
q,
ref size,
IntPtr.Zero);

if (!fResult)
{
throw new
Exception(Marshal.GetLastWin32Error().ToString());
}

handle.Free();

/*
returned back one of...
typedef struct RETRIEVAL_POINTERS_BUFFER {
DWORD ExtentCount;
LARGE_INTEGER StartingVcn;
struct {
LARGE_INTEGER NextVcn;
LARGE_INTEGER Lcn;
} Extents[1];
} RETRIEVAL_POINTERS_BUFFER, *PRETRIEVAL_POINTERS_BUFFER;
*/

Int32 ExtentCount = (Int32)Marshal.PtrToStructure(pDest,
typeof(Int32));

pDest = (IntPtr)((Int64)pDest + 4);

Int64 StartingVcn = (Int64)Marshal.PtrToStructure(pDest,
typeof(Int64));

Debug.Assert(StartingVcn == 0);

pDest = (IntPtr)((Int64)pDest + 8);

// now pDest points at an array of pairs of Int64s.

Array retVal = Array.CreateInstance(typeof(Int64), new
int[2] { ExtentCount, 2 });

for (int i = 0; i < ExtentCount; i++)
{
for (int j = 0; j < 2; j++)
{
Int64 v = (Int64)Marshal.PtrToStructure(pDest,
typeof(Int64));
retVal.SetValue(v, new int[2] { i, j });
pDest = (IntPtr)((Int64)pDest + 8);
}
}

return retVal;
}
finally
{
CloseHandle(hFile);
hFile = IntPtr.Zero;

Marshal.FreeHGlobal(pAlloc);
pAlloc = IntPtr.Zero;
}
}

/// <summary>
/// input structure for use in MoveFile
/// </summary>
private struct MoveFileData
{
public IntPtr hFile;
public Int64 StartingVCN;
public Int64 StartingLCN;
public Int32 ClusterCount;
}

/// <summary>
/// move a virtual cluster for a file to a logical cluster on
disk,
repeat for count clusters
/// </summary>
/// <param name="deviceName">device to move on"c:"</param>
/// <param name="path">file to muck with
"c:\windows\explorer.exe"</param>
/// <param name="VCN">cluster number in file to move</param>
/// <param name="LCN">cluster on disk to move to</param>
/// <param name="count">for how many clusters</param>
static public void MoveFile(string deviceName, string path, Int64
VCN, Int64 LCN, Int32 count)
{
IntPtr hVol = IntPtr.Zero;
IntPtr hFile = IntPtr.Zero;
try
{
hVol = OpenVolume(deviceName);

hFile = OpenFile(path);


MoveFileData mfd = new MoveFileData();
mfd.hFile = hFile;
mfd.StartingVCN = VCN;
mfd.StartingLCN = LCN;
mfd.ClusterCount = count;

GCHandle handle = GCHandle.Alloc(mfd,
GCHandleType.Pinned);
IntPtr p = handle.AddrOfPinnedObject();
uint bufSize = (uint)Marshal.SizeOf(mfd);
uint size = 0;

bool fResult = DeviceIoControl(
hVol,
FSConstants.FSCTL_MOVE_FILE,
p,
bufSize,
IntPtr.Zero, // no output data from this FSCTL
0,
ref size,
IntPtr.Zero);

handle.Free();

if (!fResult)
{
throw new
Exception(Marshal.GetLastWin32Error().ToString());
}
}
finally
{
CloseHandle(hVol);
CloseHandle(hFile);
}
}
}


/// <summary>
/// constants lifted from winioctl.h from platform sdk
/// </summary>
internal class FSConstants
{
const uint FILE_DEVICE_FILE_SYSTEM = 0x00000009;

const uint METHOD_NEITHER = 3;
const uint METHOD_BUFFERED = 0;

const uint FILE_ANY_ACCESS = 0;
const uint FILE_SPECIAL_ACCESS = FILE_ANY_ACCESS;

public static uint FSCTL_GET_VOLUME_BITMAP =
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 27, METHOD_NEITHER, FILE_ANY_ACCESS);
public static uint FSCTL_GET_RETRIEVAL_POINTERS =
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 28, METHOD_NEITHER, FILE_ANY_ACCESS);
public static uint FSCTL_MOVE_FILE =
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 29, METHOD_BUFFERED,
FILE_SPECIAL_ACCESS);

static uint CTL_CODE(uint DeviceType, uint Function, uint Method,
uint Access)
{
return ((DeviceType) << 16) | ((Access) << 14) | ((Function)
<<
2) | (Method);
}
}

}

-----

I'd be very grateful for any help with this conversion that you could
offer.

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top