LoginSignup
12
12

More than 5 years have passed since last update.

[Windows] USBポート(その他デバイス)の有効/無効をコマンドラインプログラムから切り替える

Posted at

背景

USBドングルなどで制御されたアプリケーションにおいて、複数のドングルが刺さっているとアプリケーションが起動できない等の制限下において、ドングルを抜き差しするのではなくソフトウェアから有効/無効を切り替えたいという需要があった。(そのほうが物理的にも故障が減りそうだし)

準備

マイクロソフトから提供されているdevcon.exeをダウンロード。(結論から申しますと、罠がありました。)
exeを解凍し、好きなところへコピー。

実行

(使い方はヘルプ等、ごらんください)

64bitのかたは、ia64のほうで。
32bitのかたは、i386のほうで。

sample
C:\tool\devcon\ia64>devcon.exe /?

01.png

( ゚д゚)ホワッツ?

sample
C:\tool\devcon\ia64>devcon.exe /?
このバージョンの c:\tool\devcon\ia64\devcon.exe は、実行中の Windows のバージョンと互換性がありません。コンピューターのシステム情報を確認して、プログラムの x86
32 ビット) のバージョンと x64 (64 ビット) のバージョンのどちらが必要か確認してから、ソフトウェアの発行元に問い合わせてください。

( ゚д゚)ホワイ?

しかたがないのでi386のほうでやってみる。

sample
C:\tool\devcon\i386>devcon.exe /?
Device Console Help:
devcon.exe [-r] [-m:\\<machine>] <command> [<arg>...]
-r if specified will reboot machine after command is complete, if needed.
<machine> is name of target machine.
<command> is command to perform (see below).
<arg>... is one or more arguments if required by command.
For help on a specific command, type: devcon.exe help <command>
classfilter          Allows modification of class filters.
classes              List all device setup classes.
disable              Disable devices that match the specific hardware or instance ID.
driverfiles          List driver files installed for devices.
drivernodes          Lists all the driver nodes of devices.
enable               Enable devices that match the specific hardware or instance ID.
find                 Find devices that match the specific hardware or instance ID.
findall              Find devices including those that are not present.
help                 Display this information.
hwids                Lists hardware ID's of devices.
install              Manually install a device.
listclass            List all devices for a setup class.
reboot               Reboot local machine.
remove               Remove devices that match the specific hardware or instance ID.
rescan               Scan for new hardware.
resources            Lists hardware resources of devices.
restart              Restart devices that match the specific hardware or instance ID.
sethwid              Modify Hardware ID's of listed root-enumerated devices.
stack                Lists expected driver stack of devices.
status               List running status of devices.
update               Manually update a device.
updateni             Manually update a device (non interactive).

うごく。。。
気を取り直して、USBポートの無効化に挑戦

02.png

sample
c:\tool\devcon\i386>devcon.exe status "@USB\VID_058F&PID_6362*"
USB\VID_058F&PID_6362\058F63626420
    Name: USB     Driver is running.
1 matching device(s) found.

C:\tool\devcon\i386>devcon.exe disable "@USB\VID_058F&PID_6362\058F63626420"
アクセスが拒否されました。

03.png

詰んだ

orz

.
..
...
....
.....
......
.......
........

解決

devcon.exeはWindows Driver Kit (WDK)にふくまれているToolなので、先に紹介したダウンロードページからではなく、WDKをインストールすることでも、devcon.exeが入手できます。(32bit, 64bit両方とも)

なんと、こちらを利用することで、きちんと制御できました。(管理者権限でのみ検証)

sample
c:\Program Files (x86)\Windows Kits\8.1\Tools\x64>devcon.exe disable "@USB\VID_058F&PID_6362\058F63626420"
USB\VID_058F&PID_6362\058F63626420                          : Disabled
1 device(s) disabled.

c:\Program Files (x86)\Windows Kits\8.1\Tools\x64>devcon.exe enable "@USB\VID_058F&PID_6362\058F63626420"
USB\VID_058F&PID_6362\058F63626420                          : Enabled
1 device(s) are enabled.

マイクロソフトあるある。

12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12