CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2006
    Posts
    34

    Enable/Disable USB drive

    Hi,

    I want to Enable/Disable USB drive programatically.
    I dont to use devcon.exe.

    If anyone knows some API please tell me.

    Thanks in advance.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Enable/Disable USB drive

    What do you mean by Enable / Disable ¿

    Do you want to programmatically disconnect the USB device ¿

    If you want to determine if the USB device was inserted or taken out, you can use the [b]RegisterDeviceNotification{/b] API with the UnregisterDeviceNotification API. You'd also need to catch the WM_DEVICECHANGE event with either DBT_DEVICEARRIVAL or DBT_DEVICEREMOVECOMPLETE

    You could also programmatically show the "Safely Remove Hardware" box by using RunDLL32.exe and reference HotPlug.dll

    Is that what you mean ¿

  3. #3
    Join Date
    Sep 2006
    Posts
    34

    Re: Enable/Disable USB drive

    Disable means even if user plugs in the USB drive, the drive letter should not appear in my computer.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Enable/Disable USB drive

    Quote Originally Posted by bsharda
    Disable means even if user plugs in the USB drive, the drive letter should not appear in my computer.
    Honestly, why do you want to do that ¿

  5. #5
    Join Date
    Jan 2008
    Posts
    6

    Question Re: Enable/Disable USB drive

    hi,
    Have u found out how to enable and disable the usb port ?
    if so could u please help me?

  6. #6
    Join Date
    Oct 2007
    Posts
    4

    Wink Re: Enable/Disable USB drive

    Hi,
    If you are just trying to use it in Kernel level, i hope http://msdn2.microsoft.com/en-us/library/aa476213.aspx
    is useful, i have got it for good.
    If you are planning to write Assembly language for any other OS, then i think you need to control interrupt for the respective PORT.

    Have a great way.
    Cheers

    Marqu

  7. #7
    Join Date
    Jun 2013
    Posts
    1

    Exclamation Re: Enable/Disable USB drive

    Quote Originally Posted by Marqu View Post
    Hi,
    If you are just trying to use it in Kernel level, i hope http://msdn2.microsoft.com/en-us/library/aa476213.aspx
    is useful, i have got it for good.
    If you are planning to write Assembly language for any other OS, then i think you need to control interrupt for the respective PORT.

    Have a great way.
    Cheers

    Marqu
    Hi, The link provided is not working anymore and says content closed. Do you have the webpage ? or any kind of information on that. Can u please help me with the way I can disable/block usb ports and if needed then enable them again..

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Enable/Disable USB drive

    Quote Originally Posted by ajaytvish View Post
    Hi, The link provided is not working anymore and says content closed.
    No, it says:
    This content has been removed.
    Quote Originally Posted by ajaytvish View Post
    Do you have the webpage ? or any kind of information on that. Can u please help me with the way I can disable/block usb ports and if needed then enable them again..
    I'd just repeat Hannes's question (from the post#4):
    Honestly, why do you want to do that?
    Victor Nijegorodov

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Enable/Disable USB drive

    1) disable the driver (don't forget to reenable it, this is kinda drastic)
    -> ZwUnloadDriver/ZwLoadDriver (see MSDN)
    needs UAC elevation or to be called from kernel mode. (you'll need a driver for this)


    2) disable the USB port (again, don't forget to reenable it, still kinda drastic)
    SetupDiXxxx api's to set the state to enabled/disabled.
    iirc, you'll need SetupDiSetClassInstallParams and SetupDiCallClassInstaller at least, you'll probably also need a bunch of code to enumerate all the ports on the bus. (see MSDN)

    alternatively get the DDK, and use the DevCon tool (which you can call with any one of the CreateProcess() type functions).

    again, needs UAC elevation.

    3) 'eject' or 'disconnect' the device.
    You can do this via the PnP Configuration Manager.

    You'll also need to monitor changes when USB devices get plugged in/out that you want to disable/eject/disconnect. There's functions in WMI to do this.

    Not all devices will respond to 'eject' or 'disconnect' commands and continue working happily.

    See: DevEject (c++) or USBEject (C#)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured