Dear All,
I am writing an application for windows desktop system.This application unlocks a removable USB drives from all the file handles acquired by some applications.
Question:
- Is it right way to unlock the device. (See Below)
- What could be the better way to unlock the device.
Thanks in advance.
-- Raghav
What I have tried:
For this I have written a program in C/C++ using visual studio. I am using NtQuerySystemInformation(), NtDuplicateObject(), NtQueryObject() API to get the handles information and then querying the handle object. If the handle name contains the device name (which has to be unlocked) then I close this handle using the following method-
BOOL WINAPI DuplicateHandle(
_In_ HANDLE hSourceProcessHandle,
_In_ HANDLE hSourceHandle,
_In_ HANDLE hTargetProcessHandle,
_Out_ LPHANDLE lpTargetHandle,
_In_ DWORD dwDesiredAccess,
_In_ BOOL bInheritHandle,
_In_ DWORD dwOptions
);
And in dwOptions I use DUPLICATE_CLOSE_SOURCE so that the source handle (which has locked the device) gets closed and the removable USB device is unlocked.