Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a handle to a device in Python on Windows

I'm trying to use the giveio.sys driver which requires a "file" to be opened before you can access protected memory. I'm looking at a C example from WinAVR/AVRdude that uses the syntax:

 #define DRIVERNAME      "\\\\.\\giveio"
 HANDLE h = CreateFile(DRIVERNAME,
            GENERIC_READ,
            0,
            NULL,
            OPEN_EXISTING,
            FILE_ATTRIBUTE_NORMAL,
            NULL);

but this does not seem to work in Python - I just get a "The specified path is invalid" error, for both

f = os.open("\\\\.\\giveio", os.O_RDONLY)

and

f = os.open("//./giveio", os.O_RDONLY)

Why doesn't this do the same thing?

Edited to hopefully reduce confusion of ideas (thanks Will). I did verify that the device driver is running via the batch files that come with AVRdude.

Further edited to clarify SamB's bounty.

like image 483
apaulsen Avatar asked Oct 17 '08 15:10

apaulsen


2 Answers

Solution: in python you have to use win32file.CreateFile() instead of open(). Thanks everyone for telling me what I was trying to do, it helped me find the answer!

like image 114
apaulsen Avatar answered Oct 01 '22 04:10

apaulsen


I don't know anything about Python, but I do know a bit about drivers. You're not trying to 'open a file in kernel space' at all - you're just trying to open a handle to a device which happens to be made to look a bit like opening a file.

CreateFile is a user-mode function, and everything you're doing here is user-mode, not kernel mode.

As xenon says, your call may be failing because you haven't loaded the driver yet, or because whatever Python call you're using to do the CreateFile is not passing the write parameters in.

I've never used giveio.sys myself, but personally I would establish that it was loaded correctly by using 'C' or C++ (or some pre-written app) before I tried to get it working via Python.

like image 35
Will Dean Avatar answered Oct 01 '22 03:10

Will Dean