DeviceIoControl to lock drive is not working
-
Okay I am trying to lock CD drive (F: here) with the code shown. But DeviceIoControl is returning "error 87: Parameter not correct". Please someone help me. I cannot find out which parameter is incorrcet.
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>void ShowError(void); //function to display message from GetLastError()
int main(){
char szVolume[] = "\\\\.\\F:";
HANDLE hVolume;
DWORD dwBytes = 0;
PREVENT_MEDIA_REMOVAL pmr;pmr.PreventMediaRemoval = TRUE; hVolume = CreateFile(szVolume,GENERIC\_READ|GENERIC\_WRITE,FILE\_SHARE\_READ|FILE\_SHARE\_WRITE,NULL,OPEN\_EXISTING,0,NULL); if(hVolume==INVALID\_HANDLE\_VALUE){ ShowError(); exit(1); } if(DeviceIoControl(hVolume,IOCTL\_STORAGE\_EJECTION\_CONTROL,(LPVOID)&pmr,(DWORD)sizeof(pmr),NULL,0,&dwBytes,NULL)==FALSE){ ShowError(); } CloseHandle(hVolume); return 0;
}
I can't find an error, but maybe you open the device in a wrong way? MSDN says: "hDevice [in] A handle to the device. To obtain a device handle, call the CreateFile function. Files must be opened with the FILE_READ_ATTRIBUTES access right. For more information, see File Security and Access Rights." You try to open your device with generic read and write access. Sorry buts the only thing I found, what could maybe explain this error. Has hVolume the right value? (Your check is right, but i wonder what GetLastError() says.) Edit: Maybe you should also change your sharing parameter in your CreateFile call (for example try 0 -> no share).
-
Okay I am trying to lock CD drive (F: here) with the code shown. But DeviceIoControl is returning "error 87: Parameter not correct". Please someone help me. I cannot find out which parameter is incorrcet.
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>void ShowError(void); //function to display message from GetLastError()
int main(){
char szVolume[] = "\\\\.\\F:";
HANDLE hVolume;
DWORD dwBytes = 0;
PREVENT_MEDIA_REMOVAL pmr;pmr.PreventMediaRemoval = TRUE; hVolume = CreateFile(szVolume,GENERIC\_READ|GENERIC\_WRITE,FILE\_SHARE\_READ|FILE\_SHARE\_WRITE,NULL,OPEN\_EXISTING,0,NULL); if(hVolume==INVALID\_HANDLE\_VALUE){ ShowError(); exit(1); } if(DeviceIoControl(hVolume,IOCTL\_STORAGE\_EJECTION\_CONTROL,(LPVOID)&pmr,(DWORD)sizeof(pmr),NULL,0,&dwBytes,NULL)==FALSE){ ShowError(); } CloseHandle(hVolume); return 0;
}
Are you completely sure about F:?
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <
-
Okay I am trying to lock CD drive (F: here) with the code shown. But DeviceIoControl is returning "error 87: Parameter not correct". Please someone help me. I cannot find out which parameter is incorrcet.
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>void ShowError(void); //function to display message from GetLastError()
int main(){
char szVolume[] = "\\\\.\\F:";
HANDLE hVolume;
DWORD dwBytes = 0;
PREVENT_MEDIA_REMOVAL pmr;pmr.PreventMediaRemoval = TRUE; hVolume = CreateFile(szVolume,GENERIC\_READ|GENERIC\_WRITE,FILE\_SHARE\_READ|FILE\_SHARE\_WRITE,NULL,OPEN\_EXISTING,0,NULL); if(hVolume==INVALID\_HANDLE\_VALUE){ ShowError(); exit(1); } if(DeviceIoControl(hVolume,IOCTL\_STORAGE\_EJECTION\_CONTROL,(LPVOID)&pmr,(DWORD)sizeof(pmr),NULL,0,&dwBytes,NULL)==FALSE){ ShowError(); } CloseHandle(hVolume); return 0;
}
Patcher32 wrote:
PREVENT_MEDIA_REMOVAL pmr;
Isn't this used with
IOCTL_STORAGE_MEDIA_REMOVAL
?"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Patcher32 wrote:
PREVENT_MEDIA_REMOVAL pmr;
Isn't this used with
IOCTL_STORAGE_MEDIA_REMOVAL
?"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Two MSDN sites that contradict each other. No surprise there!
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
I can't find an error, but maybe you open the device in a wrong way? MSDN says: "hDevice [in] A handle to the device. To obtain a device handle, call the CreateFile function. Files must be opened with the FILE_READ_ATTRIBUTES access right. For more information, see File Security and Access Rights." You try to open your device with generic read and write access. Sorry buts the only thing I found, what could maybe explain this error. Has hVolume the right value? (Your check is right, but i wonder what GetLastError() says.) Edit: Maybe you should also change your sharing parameter in your CreateFile call (for example try 0 -> no share).
-
Are you completely sure about F:?
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <
-
Okay I am trying to lock CD drive (F: here) with the code shown. But DeviceIoControl is returning "error 87: Parameter not correct". Please someone help me. I cannot find out which parameter is incorrcet.
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>void ShowError(void); //function to display message from GetLastError()
int main(){
char szVolume[] = "\\\\.\\F:";
HANDLE hVolume;
DWORD dwBytes = 0;
PREVENT_MEDIA_REMOVAL pmr;pmr.PreventMediaRemoval = TRUE; hVolume = CreateFile(szVolume,GENERIC\_READ|GENERIC\_WRITE,FILE\_SHARE\_READ|FILE\_SHARE\_WRITE,NULL,OPEN\_EXISTING,0,NULL); if(hVolume==INVALID\_HANDLE\_VALUE){ ShowError(); exit(1); } if(DeviceIoControl(hVolume,IOCTL\_STORAGE\_EJECTION\_CONTROL,(LPVOID)&pmr,(DWORD)sizeof(pmr),NULL,0,&dwBytes,NULL)==FALSE){ ShowError(); } CloseHandle(hVolume); return 0;
}
Okay I finally got it to work. YAY! :) :) :) I made this change :
pmr.PreventMediaRemoval = (BYTE)TRUE;
in the original code I posted. But MSDN says thePreventMediaRemoval
member isBOOLEAN
? Thanks for confusing me MSDN! :mad::mad: The value has to beBYTE
. So setting it to 1 locks drive from being ejected. Setting it to 0 unlocks the drive from ejecting. -
Okay I finally got it to work. YAY! :) :) :) I made this change :
pmr.PreventMediaRemoval = (BYTE)TRUE;
in the original code I posted. But MSDN says thePreventMediaRemoval
member isBOOLEAN
? Thanks for confusing me MSDN! :mad::mad: The value has to beBYTE
. So setting it to 1 locks drive from being ejected. Setting it to 0 unlocks the drive from ejecting."But MSDN says the PreventMediaRemoval member is BOOLEAN? Thanks for confusing me MSDN!" MSDN is right the problem is, that TRUE is a 32 Bit-Value (TRUE and true are not the same). So you send the value 0(FALSE) to your drive. And if I'm right, the function fails if you try to unlock drive you never locked before.
-
"But MSDN says the PreventMediaRemoval member is BOOLEAN? Thanks for confusing me MSDN!" MSDN is right the problem is, that TRUE is a 32 Bit-Value (TRUE and true are not the same). So you send the value 0(FALSE) to your drive. And if I'm right, the function fails if you try to unlock drive you never locked before.
If I lock with
(BYTE)TRUE
: works If I unlock a locked device using(BYTE)FALSE
: works If I try to unlock a device (without locking it first) using(BYTE)FALSE
: gives error #22, The device does not recognize command I think this is expected behavior. Please guide me in right direction if I am doing it wrong.