473,383 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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/de...ting_files.asp
//
// FSCTL_GET_VOLUME_BITMAP
//
http://msdn.microsoft.com/library/de...ume_bitmap.asp
//
// interesting structures...
// FSCTL_MOVE_FILE
// FSCTL_GET_RETRIEVAL_POINTERS
// RETRIEVAL_POINTERS_BUFFER
// FSCTL_GET_RETRIEVAL_POINTERS
//
// DeviceIoControl
//
http://msdn.microsoft.com/library/de...eiocontrol.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!


Nov 21 '05 #1
3 2022
"Jim Hubbard" <re***@groups.please> schrieb:
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.


Converting code between .NET programming languages
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=languageconverters&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #2
Thanks for the links.

I tried all of the free converters, and all of the failed.

There were 2 here I had not seen before. The best of the free converters is
at http://authors.aspalliance.com/aldot...translate.aspx .

Although it also failed to convert the code, it is more verbose in the
reasons why.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:OT**************@TK2MSFTNGP10.phx.gbl...
"Jim Hubbard" <re***@groups.please> schrieb:
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.


Converting code between .NET programming languages
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=languageconverters&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
Jim Hubbard wrote:
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

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Philipp Lenssen | last post by:
I want to write a third installment of "Little Known HTML Facts"*. I would appreciate your input here. For one thing, I would like to remember what exactly those proprietary icons were you could...
3
by: gary | last post by:
Hi, 1. About all C/C++ compilers, Does stack increase from high address to low address and heap grow increase from low to high? What on earth decides their increase direction, CPU architecture, OS...
1
by: MLH | last post by:
The text below was cut 'n past from an HTML file. When I pasted it into a text field of an Access 97 table, it looked different than it does below. Below, you can see that the first 3 lines have...
38
by: Martin Marcher | last post by:
Hi, I've read several questions and often the answer was 'C knows nothing about .' So if C knows that little as some people say, what are the benefits, I mean do other languages know more...
0
by: Kenneth Lantrip | last post by:
After some cleaning of some of my personal directories and files, I stumbled upon this little program I wrote some time ago. A scammer was trying to introduce me into his little pyramid scam. So...
2
by: bhatia | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
5
by: manishster | last post by:
I keep getting the following in my output file , regardless of whether I convert endian-ness or not . How do i get "01 02 03 04" .... Mahamannu output : 04 03 02 01 b0 00 00
13
by: junky_fellow | last post by:
Hi guys, I need to convert a big endian integer to little endian integer. (the integer is 4 bytes in size on my implementation). I came up with the following code. I need your comments on...
102
by: BoogieWithStu22 | last post by:
I am running into a problem with a web page I have created when viewing it in IE6 on some machines. The page has a database lookup. The user enters an account name or number and clicks a lookup...
23
by: Niranjan | last post by:
I have this program : void main() { int i=1; if((*(char*)&i)==1) printf("The machine is little endian."); else printf("The machine is big endian."); }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.