[python-win32] Handle to a Driver

Mark Hammond mhammond at skippinet.com.au
Sun Apr 3 05:06:05 CEST 2005


> Python:
> ReadEvent = CreateEvent ( None, 0, 0, None )
> ret = DeviceIoControl (hDevice, IOCTL_SET_READ_EVENT, str(ReadEvent),
> 0, None)

str(ReadEvent) would give you a string with something like '<PyHANDLE:768>'.
It seems unlikely to me that this is your intention.  Maybe
str(int(ReadEvent)) - this would just send the handle value (as a string)

> Driver code in Dispatch Routine for IOCTL:
> ntStatus = ObReferenceObjectByHandle ( handleFromPython,
> EVENT_MODIFY_STATE, *ExEventObjectType, Irp->RequestorMode, (PVOID*)
> &EventHandleDestination, NULL);

It's not clear to me how 'handleFromPython' gets its value.  I expect that
is your problem.

> Is it possible that the win32 extensions wrapped the functions whithin
> it's own handle-table, so that the operating system can't find the
> handle-id in the system handle table ?

Nope, but there is "auto-close" behaviour that may be biting you (or about
to bite you :)

As soon as the ReadEvent object goes out of scope, CloseHandle() will be
automatically called for the underlying handle.  The Detach() method on a
handle lets you get the integer handle and "detaches" it from the object, so
CloseHandle will not be called.  In your example above that should not be a
problem (ReadEvent remains in scope during the call to DeviceIoControl) -
but if the driver is storing the handle for use internally, things will
start going wrong when the object dies.

Mark



More information about the Python-win32 mailing list