DeviceIoControl(..., FSCTL_LOCK_VOLUME,...); Issue

Hello together,

I want to lock a volume (USB Device) but receive an ERROR_INVALID_HANDLE all the time:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
HANDLE hDev = CreateFile(
	"\\\\.\\E:",
	0,
	(FILE_SHARE_READ | FILE_SHARE_WRITE),
	NULL,
	OPEN_EXISTING,
	FILE_FLAG_OVERLAPPED,
	NULL
	);

if (hDev == INVALID_HANDLE_VALUE)
{
	cout << "CreateFile() failed!" << endl;
	cout << "GetLastError(): " << GetLastError() << endl;
	return -1;
}
DWORD bytesReturned;
OVERLAPPED overlapped;

BOOL retDevIoCtrl = DeviceIoControl(
	hDev,              // handle to a volume
	FSCTL_LOCK_VOLUME, // dwIoControlCode
	NULL,              // lpInBuffer
	0,                 // nInBufferSize
	NULL,              // lpOutBuffer
	0,                 // nOutBufferSize
	&bytesReturned,    // number of bytes returned
	&overlapped        // OVERLAPPED structure
);
if (!retDevIoCtrl)
{
	cout << "FSCTL_LOCK_VOLUME failed!" << endl;
	cout << "GetLastError(): " << GetLastError() << endl;


The output is always "FSCTL_LOCK_VOLUME failed! GetLastError(): 6"

I've tried a few variations of the CreateFile parameters but didn't find a solution so far. Maybe somebody can help? Am I using the wrong parameters? As far as I understand the MSDN documentation I use them as advised.

Cheers
The 'hEvent' data member in your OVERLAPPED struct was never initialized or set to zero, that's the "Invalid Handle" that the error code is referring to: http://msdn.microsoft.com/en-us/library/windows/desktop/ms684342(v=vs.85).aspx


Remember that C objects don't have constructors so stuff like this has to be done by hand.
Is asynchronous I/O really needed in this situation? I think &overlapped can be replaced with NULL and the code will work fine.
Thanks a lot to both of you.
You were absolutely right: I forgot to initialize the OVERLAPPED struct.
Nevertheless there had been another error after that. I had to modify the access rights of the CreateFile funtion.

1
2
3
4
5
6
7
8
9
HANDLE hDev = CreateFile(
	"\\\\.\\E:",
	(GENERIC_READ | GENERIC_WRITE),
	(FILE_SHARE_READ | FILE_SHARE_WRITE),
	NULL,
	OPEN_EXISTING,
	FILE_FLAG_OVERLAPPED,
	NULL
	);
Topic archived. No new replies allowed.