post your code
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool DeviceIoControl(IntPtr handle, uint dwIoControlCode, IntPtr lpInBuffer, int unInBufferSize, [Out] IntPtr lpOutBuffer, uint nOutBufferSize, uint lpBytesReturned, IntPtr lpOverlapped);
struct DISK_GEOMETRY
{
public long Cylinders;
//public MEDIA_TYPE MediaType; // don't really want to add MEDIA_TYPE constants when they aren't needed here
public int TracksPerCylinder;
public int SectorsPerTrack;
public int BytesPerSector;
}
private void getDiskGeometry()
{
// takes a physicaldevice string and returns the number of bytes -
// maybe i'll build this into filling out the struct in the function below and get rid of getfreediskspace
//long numSectors = 0;
uint bytesReturned = 0;
//DISK_GEOMETRY diskGeom = new DISK_GEOMETRY();
//DISK_GEOMETRY diskGeom;
var diskGeom = new DISK_GEOMETRY();
/*using (*/
var diskGeomPtr = new UnmanagedStruct<DISK_GEOMETRY>();//)
//{
diskGeomPtr.Data = diskGeom;
//do something with diskGeomPtr.Handle here
IntPtr p = IntPtr.Zero; // file handle for createfile and deviceIoControl
bool result;
//device, command, no input buffer(2args), output buffer, size of output buffer, bytes returned, synchronous I/O
p = CreateFile("\\.\\PhysicalDrive1", GENERIC_READ | GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero);
// this opens the device fine, don't need GENERIC_READ|GENERIC_WRITE if it's just for IOCTL operations
// check if our file handle is valid
if (p != null)
MessageBox.Show("device opened ok"); // valid
else MessageBox.Show("device not opened ok"); // invalid
// ok, it seems to be getting opened fine, so I need to find out why this DeviceIoControl call isn't
result = DeviceIoControl(p, IOCTL_DISK_GET_DRIVE_GEOMETRY, IntPtr.Zero, 0, diskGeomPtr.Handle, diskGeomPtr.Size, bytesReturned, IntPtr.Zero);
// closest I've got is error 87, wrong parameter or something.
MessageBox.Show(result.ToString(), "result is");
MessageBox.Show(bytesReturned.ToString(), "bytes returned");
if (result)
{
MessageBox.Show(bytesReturned.ToString(), "bytes from deviceio call");
MessageBox.Show(diskGeom.BytesPerSector.ToString(), "bytes per sector");
}
else
{
int error = Marshal.GetLastWin32Error();
MessageBox.Show("Error " + error.ToString(), "getlastwin32error");
}
CloseHandle(p);
//} // end using
//return numSectors;
}
internal const int IOCTL_DISK_GET_DRIVE_GEOMETRY = unchecked((int)0x00070000);
Warning 4 Field 'WindowsFormsApplication1.frmMainWindow.DISK_GEOMETRY.BytesPerSector' is never assigned to, and will always have its default value 0 C:\Users\Shaun\Desktop\IMGtool\IMGtool\Form1.cs 48 24 IMGtool
Warning 1 Field 'WindowsFormsApplication1.frmMainWindow.DISK_GEOMETRY.Cylinders' is never assigned to, and will always have its default value 0 C:\Users\Shaun\Desktop\IMGtool\IMGtool\Form1.cs 44 25 IMGtool
Warning 3 Field 'WindowsFormsApplication1.frmMainWindow.DISK_GEOMETRY.SectorsPerTrack' is never assigned to, and will always have its default value 0 C:\Users\Shaun\Desktop\IMGtool\IMGtool\Form1.cs 47 24 IMGtool
Warning 2 Field 'WindowsFormsApplication1.frmMainWindow.DISK_GEOMETRY.TracksPerCylinder' is never assigned to, and will always have its default value 0 C:\Users\Shaun\Desktop\IMGtool\IMGtool\Form1.cs 46 24 IMGtool
Not with Zeljko scurrying around.
using System;
using System.Diagnostics;
using System.Collections;
using System.Runtime.InteropServices;
namespace DiskGeometryTest
{
class Program
{
//
// 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;
internal const int IOCTL_DISK_GET_DRIVE_GEOMETRY = unchecked((int)0x00070000);
[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);
struct DISK_GEOMETRY
{
public long Cylinders;
//public MEDIA_TYPE MediaType; // don't really want to add MEDIA_TYPE constants when they aren't needed here
public int TracksPerCylinder;
public int SectorsPerTrack;
public int BytesPerSector;
}
static void getDiskGeometry()
{
// takes a physicaldevice string and returns the number of bytes -
// maybe i'll build this into filling out the struct in the function below and get rid of getfreediskspace
//long numSectors = 0;
uint bytesReturned = 0;
var diskGeom = new DISK_GEOMETRY();
var geomp = Marshal.AllocHGlobal(Marshal.SizeOf(diskGeom));
Marshal.StructureToPtr(diskGeom, geomp, false);
IntPtr p = IntPtr.Zero; // file handle for createfile and deviceIoControl
bool result;
//device, command, no input buffer(2args), output buffer, size of output buffer, bytes returned, synchronous I/O
p = CreateFile("\\\\.\\PhysicalDrive1", GENERIC_READ | GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero);
// this opens the device fine, don't need GENERIC_READ|GENERIC_WRITE if it's just for IOCTL operations
// check if our file handle is valid
if (p != null)
Console.WriteLine("device opened ok"); // valid
else Console.WriteLine("device not opened ok"); // invalid
// ok, it seems to be getting opened fine, so I need to find out why this DeviceIoControl call isn't
result = DeviceIoControl(p, IOCTL_DISK_GET_DRIVE_GEOMETRY, IntPtr.Zero, 0, geomp, (uint)Marshal.SizeOf(typeof(DISK_GEOMETRY)), ref bytesReturned, IntPtr.Zero);
// closest I've got is error 87, wrong parameter or something.
var ob = (DISK_GEOMETRY)Marshal.PtrToStructure(geomp, typeof(DISK_GEOMETRY));
Console.WriteLine(result.ToString(), "result is");
Console.WriteLine(bytesReturned.ToString(), "bytes returned");
if (result)
{
Console.WriteLine(bytesReturned.ToString(), "bytes from deviceio call");
Console.WriteLine(ob.BytesPerSector.ToString(), "bytes per sector");
}
else
{
int error = Marshal.GetLastWin32Error();
Console.WriteLine("Error " + error.ToString(), "getlastwin32error");
}
CloseHandle(p);
//} // end using
//return numSectors;
}
static void Main(string[] args)
{
getDiskGeometry();
}
}
}
private void getDiskGeometry()
{
// takes a physicaldevice string and returns the number of bytes -
// maybe i'll build this into filling out the struct in the function below and get rid of getfreediskspace
//long numSectors = 0;
uint bytesReturned = 0;
var diskGeom = new DISK_GEOMETRY();
var geomp = Marshal.AllocHGlobal(Marshal.SizeOf(diskGeom));
Marshal.StructureToPtr(diskGeom, geomp, false);
IntPtr p = IntPtr.Zero; // file handle for createfile and deviceIoControl
bool result;
p = CreateFile("\\\\.\\PhysicalDrive1", GENERIC_READ | GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero);
// check if our file handle is valid
if (p != null)
MessageBox.Show("device opened ok"); // valid
else MessageBox.Show("device not opened ok"); // invalid
// ok, it seems to be getting opened fine, so I need to find out why this DeviceIoControl call isn't
result = DeviceIoControl(p, IOCTL_DISK_GET_DRIVE_GEOMETRY, IntPtr.Zero, 0, geomp, (uint)Marshal.SizeOf(typeof(DISK_GEOMETRY)), ref bytesReturned, IntPtr.Zero);
var ob = (DISK_GEOMETRY)Marshal.PtrToStructure(geomp, typeof(DISK_GEOMETRY));
MessageBox.Show(result.ToString(), "result is");
MessageBox.Show(bytesReturned.ToString(), "bytes returned");
if (result)
{
MessageBox.Show(bytesReturned.ToString(), "bytes from deviceio call");
MessageBox.Show(ob.BytesPerSector.ToString(), "bytes per sector");
}
else
{
int error = Marshal.GetLastWin32Error();
MessageBox.Show("Error " + error.ToString(), "getlastwin32error");
}
CloseHandle(p);
//} // end using
//return numSectors;
}
Warning 4 Field 'WindowsFormsApplication1.frmMainWindow.DISK_GEOMETRY.BytesPerSector' is never assigned to, and will always have its default value 0 C:\Users\Shaun\Desktop\IMGtool\IMGtool\Form1.cs 48 24 IMGtool
Warning 1 Field 'WindowsFormsApplication1.frmMainWindow.DISK_GEOMETRY.Cylinders' is never assigned to, and will always have its default value 0 C:\Users\Shaun\Desktop\IMGtool\IMGtool\Form1.cs 44 25 IMGtool
Warning 3 Field 'WindowsFormsApplication1.frmMainWindow.DISK_GEOMETRY.SectorsPerTrack' is never assigned to, and will always have its default value 0 C:\Users\Shaun\Desktop\IMGtool\IMGtool\Form1.cs 47 24 IMGtool
Warning 2 Field 'WindowsFormsApplication1.frmMainWindow.DISK_GEOMETRY.TracksPerCylinder' is never assigned to, and will always have its default value 0 C:\Users\Shaun\Desktop\IMGtool\IMGtool\Form1.cs 46 24 IMGtool