JLW,
how do you declare CreateFile?
Here is working sample - I tried it calling TF (@"\\.\PHYSICALDRIVE0") -
note @ before string and see function defnition below. I don't have tape -
ughhh!
Sample uses some prototypes from
www.pinvoke.net - thank you guys!
public static int CREATE_NEW = 1;
public static int CREATE_ALWAYS = 2;
public static int OPEN_EXISTING = 3;
public static int OPEN_ALWAYS = 4;
public static int TRUNCATE_EXISTING = 5;
[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 bool CloseHandle(IntPtr hObject);
private static void TF(string filename) {
IntPtr ptr = CreateFile(filename,0, 0,IntPtr.Zero, (uint)OPEN_EXISTING,0,
IntPtr.Zero);
/* Is bad handle? INVALID_HANDLE_VALUE */
if (ptr.ToInt32() == -1)
{
/* ask the framework to marshall the win32 error code to an exception */
Console.WriteLine(String.Format("{0}",Marshal.GetLastWin32Error()));
}
else
{
Console.WriteLine("Opened");
CloseHandle(ptr);
}
}
HTH
Alex