I have a WDF driver which i can open via CreateFile and get a handle back.
Now i can send DeviceIoControl to my driver. So far so good, working.
My driver uses multiple instances to access multiple hardware devices via USB.
The CreateFile is done inside a DLL.
This DLL is used via multiple threads from one application or
from different application.
When i call CreateFile, i get a HANDLE back, which i print (for debugging) via “%d”
and it displays me, e.g. 4996.
I call DeviceIoControls with this handle also there i can see the same handle
and also again in CloseHandle.
Now i have certain rare cases (perhaps collision cases between the two devices),
where i send the DeviceIoControl, but get back a result of 0, meaning error.
Calling GetLastError i get error 6 which means ERROR_INVALID_HANDLE.
I printed the used HANDLE and it is still my 4996.
I have also a debug output at every position of CloseHandle
and when i have not overseen it, i think i have not called CloseHandle for this HANDLE.
I don’t understand yet why i get ERROR_INVALID_HANDLE.
Is there some way how i can debug this, e.g. display all opened handles,
not related to a EXE file, but in general?
Can checking if a HANDLE is valid only be done by comparing against
INVALID_HANDLE_VALUE, which is simply a certain value, but then
this is not the case, because i still see the 4996 as handle value.
I also though about tracing open / close of the driver,
is this the way to go?
In general:
When i call CreateFile in one thread, i am almost sure,
i can use it for all threads. Are there special cases i must pay attention to?
When i open a HANDLE for one application, can i use the same handle
also for a different application or must i get a new one?