2

I have a function that I'm trying to get working. My intention is to be able to get a file handle (IntPtr) object so that I can call "Win32.DeviceIoControl" against it. It works just fine in Windows 7, however when I test on Windows 8, I'm getting a -1 returned from object variable.

Perhaps there is a different way to do this in windows 8? or a better way to do it overall.

Here is the code:

    public bool OpenHandle()
    {
        string filename = "\\\\.\\D:";

        hFile = Win32.CreateFile(filename, Win32.GENERIC_READ | Win32.GENERIC_WRITE,
            Win32.FILE_SHARE_READ | Win32.FILE_SHARE_WRITE, IntPtr.Zero, Win32.OPEN_EXISTING, 0, IntPtr.Zero);

        if (hFile.ToInt32() == Win32.INVALID_HANDLE)
        {
            return false;
        }
        return true;
    }

This code was taken from https://stackoverflow.com/a/9579881/1607306 , I'm just trying to utilize it. Again, working in Win7, not Win8.

Thanks!

Community
  • 1
  • 1
Trihedron
  • 246
  • 2
  • 14
  • There could be any number of things wrong. After the call fails, call `Marshal.GetLastWin32Error` and post the error message. – Gabe Jan 14 '13 at 20:07
  • I'm getting back a "5". I've never used Marshal before, does it decode to this list here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx – Trihedron Jan 14 '13 at 20:14
  • Yes. Looks like Access Denied. Are you running the app as Admin? – Pete Jan 14 '13 at 20:35
  • Running as Admin doesn't seem to help very much. It works on Windows 7 with doing run as Admin (on a computer that previously gave a 5), so something in Windows 8 is saying No. – Trihedron Jan 14 '13 at 22:40

0 Answers0