Raw Disk Access?

  • Thread starter Thread starter mqudsi
  • Start date Start date
M

mqudsi

Is there any way to get raw disk access in C#?

e.g. get the first XX bytes from drive0, partition1 starting from byte
YYY?

(something like the dd command in Unix maybe?)

If I have to use P/Invoke can anyone tell me what libraries are
needed?

Thanks.
 
Hello!
You wrote on 29 Mar 2007 01:41:09 -0700:

m> e.g. get the first XX bytes from drive0, partition1 starting from byte
m> YYY?
m> (something like the dd command in Unix maybe?)
m> If I have to use P/Invoke can anyone tell me what libraries are
m> needed?

Before XP you could just call CreateFile() Windows API function.
In Windows XP you could do this only if the application is run with
administrator's permissons.
In Vista this way doesn't work.

We are offering RawDisk product ( http://www.eldos.com/rawdisk/ ) which
provides raw disk access on all systems without any restrictions. The
product constists of a kernel-mode driver and a user mode API (just 3
methods). The API is available for C++, .NET (C#, VB.NET, C++.NET) and VCL
(Delphi, C++Builder).

With best regards,
Eugene Mayevski
http://www.SecureBlackbox.com - the comprehensive component suite for
network security
 
Is there any way to get raw disk access in C#?

e.g. get the first XX bytes from drive0, partition1 starting from byte
YYY?

(something like the dd command in Unix maybe?)

If I have to use P/Invoke can anyone tell me what libraries are
needed?

Thanks.


You'll have to call Win32 CreateFile using PInvoke, just like you would do using C.
Note also that you need Administrative privileges to access devices.

Following is a small sample to get you started:

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

internal const int GENERIC_READ = unchecked((int)0x80000000);
internal const int OPEN_EXISTING = 3;
internal const int FILE_ATTRIBUTE_NORMAL = 0x80;

....
const int blockSize = 512;
SafeFileHandle h = null;
h = CreateFile("\\\\.\\PhysicalDrive0",
GENERIC_READ, 0, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
new SafeFileHandle(IntPtr.Zero, true));
if (! h.IsInvalid ) {
FileStream stream = new FileStream(h, FileAccess.Read);
// Read from stream
else
{
// get error code and throw
int error = Marshal.GetLastWin32Error();
..
}


Willy.
 
Is there any way to get raw disk access in C#?
e.g. get the first XX bytes from drive0, partition1 starting from byte
YYY?
(something like the dd command in Unix maybe?)
If I have to use P/Invoke can anyone tell me what libraries are
needed?

You'll have to call Win32 CreateFile using PInvoke, just like you would do using C.
Note also that you need Administrative privileges to access devices.

Following is a small sample to get you started:

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

internal const int GENERIC_READ = unchecked((int)0x80000000);
internal const int OPEN_EXISTING = 3;
internal const int FILE_ATTRIBUTE_NORMAL = 0x80;

...
const int blockSize = 512;
SafeFileHandle h = null;
h = CreateFile("\\\\.\\PhysicalDrive0",
GENERIC_READ, 0, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
new SafeFileHandle(IntPtr.Zero, true));
if (! h.IsInvalid ) {
FileStream stream = new FileStream(h, FileAccess.Read);
// Read from stream
else
{
// get error code and throw
int error = Marshal.GetLastWin32Error();
..
}

Willy.

Thanks Willy, that's perfect!
 

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

Back
Top