Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. DeviceIoControl to lock drive is not working

DeviceIoControl to lock drive is not working

Scheduled Pinned Locked Moved C / C++ / MFC
help
11 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Patcher32

    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;
    

    }

    C Offline
    C Offline
    Covean
    wrote on last edited by
    #2

    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).

    P 1 Reply Last reply
    0
    • P Patcher32

      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;
      

      }

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #3

      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. <

      P 1 Reply Last reply
      0
      • P Patcher32

        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;
        

        }

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #4

        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

        C 1 Reply Last reply
        0
        • D David Crow

          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

          C Offline
          C Offline
          Code o mat
          wrote on last edited by
          #5

          According to this[^] it is also used with IOCTL_STORAGE_EJECTION_CONTROL.

          > 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. <

          D 1 Reply Last reply
          0
          • C Code o mat

            According to this[^] it is also used with IOCTL_STORAGE_EJECTION_CONTROL.

            > 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. <

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #6

            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

            1 Reply Last reply
            0
            • C Covean

              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).

              P Offline
              P Offline
              Patcher32
              wrote on last edited by
              #7

              Yes hVolume is right value. GetLastError returns 0. GENERIC_READ already has FILE_READ_ATTRIBUTES Tried using 0 for File sharing but still DeviceIoControl gives error 87 parameter not correct Thanks for replying, I appreciate it.

              1 Reply Last reply
              0
              • C Code o mat

                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. <

                P Offline
                P Offline
                Patcher32
                wrote on last edited by
                #8

                Yes my DVD drive is F:. and handle returned by CreateFile is valid too. Thanks for replying

                1 Reply Last reply
                0
                • P Patcher32

                  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;
                  

                  }

                  P Offline
                  P Offline
                  Patcher32
                  wrote on last edited by
                  #9

                  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 the PreventMediaRemoval member is BOOLEAN? Thanks for confusing me MSDN! :mad::mad: The value has to be BYTE. So setting it to 1 locks drive from being ejected. Setting it to 0 unlocks the drive from ejecting.

                  C 1 Reply Last reply
                  0
                  • P Patcher32

                    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 the PreventMediaRemoval member is BOOLEAN? Thanks for confusing me MSDN! :mad::mad: The value has to be BYTE. So setting it to 1 locks drive from being ejected. Setting it to 0 unlocks the drive from ejecting.

                    C Offline
                    C Offline
                    Covean
                    wrote on last edited by
                    #10

                    "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.

                    P 1 Reply Last reply
                    0
                    • C Covean

                      "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.

                      P Offline
                      P Offline
                      Patcher32
                      wrote on last edited by
                      #11

                      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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups