Enabling/disabling a device in Windows 10 from command line [closed] - windows

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
I have a specific piece of hardware which I'd like to disable and re-enable each time my Windows restarts. I created a batch script which is supposed to do that, along with running my program afterwards:
cd %~dp0
devcon.exe disable "PCI\VEN_1002&DEV_687F"
timeout /t 3
devcon.exe enable "PCI\VEN_1002&DEV_687F"
runMyWindows.exe --totally-not-virus
I am not sure if devcon.exe is a proper application for this in the first place because I have no experience with writing Windows scripts at all.
However, I have noticed that those commands don't quite do the job because my runMyWindows.exe program doesn't work as it should until I go to Windows Device Manager and manually disable and re-enable this device.
I have only 1 user on this machine which is in "Administrator" group and I am not running this script in any special way except double-clicking the .bat file, or in case of the restart, it is run from the startup folder (C:\Users\oxxo\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup).
Is there a way to do this properly within my batch script which should be run automatically on Windows startup?

PnPUtil do this job also and no SDK or anything else related required to download.
Included in Windows since Vista:
https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/pnputil
Examples
Disables device specified by device instance ID:
pnputil /disable-device "USB\VID_045E&PID_00DB\6&870CE29&0&1"
Enables device specified by device instance ID:
pnputil /enable-device "USB\VID_045E&PID_00DB\6&870CE29&0&1"

Most people who'll be reading this thread won't find the other answer very useful, because it's mostly about how to run the script in the question with administrator privileges. I'll attempt to answer the implicit questions here:
Enable/disable a device via the command line
I found it easiest to use devcon.exe (6mb), like in the question:
set HARDWARE_ID="PCI\VEN_8086&DEV_4229&SUBSYS_11018086&REV_61"
devcon disable %HARDWARE_ID%
timeout /t 3
devcon enable %HARDWARE_ID%
devcon.exe requires administrator privileges.
Where to get devcon?
It's part of the Windows driver development toolkit. Unfortunately, the official resources ask you to download a 1gb SDK. I was able to get around that by following one of the answers here: https://superuser.com/questions/1002950/quick-method-to-install-devcon-exe
Once you have it, make sure devcon.exe is on your %PATH%. I put mine in C:\Windows\System32\.
Find the hardware ID of the device you want to manipulate
Open a Command Prompt with administrator privileges and do devcon hwids *, which will print all the devices and their corresponding IDs. That will produce a lot of output. Use Command Prompts search function to find what you need. Here's the section I was interested in:
PCI\VEN_8086&DEV_4229&SUBSYS_11018086&REV_61\4&6AB551C&0&00E1
Name: Intel(R) Wireless WiFi Link 4965AGN
Hardware IDs:
PCI\VEN_8086&DEV_4229&SUBSYS_11018086&REV_61
PCI\VEN_8086&DEV_4229&SUBSYS_11018086
PCI\VEN_8086&DEV_4229&CC_028000
PCI\VEN_8086&DEV_4229&CC_0280
Compatible IDs:
PCI\VEN_8086&DEV_4229&REV_61
PCI\VEN_8086&DEV_4229
PCI\VEN_8086&CC_028000
PCI\VEN_8086&CC_0280
PCI\VEN_8086
PCI\CC_028000
PCI\CC_0280
Pick a specific enough ID and check if it works by doing:
devcon find "PCI\VEN_8086&DEV_4229&SUBSYS_11018086&REV_61"
If that finds only 1 device, and it's the one you want, you're good. Notice that often you'll want to escape the hardware ID with quotes.
Bonus: running a .bat script at startup or power on
In my case, I also needed to run this script when computer has booted after shutdown or sleep. I gave the above script sensible permissions and used Task Scheduler to run it on login and on startup, in its terminology:
https://www.sevenforums.com/tutorials/67503-task-create-run-program-startup-log.html?ltr=T

Due to security 'improvements' in Windows 10 and certainly since Windows Vista and the introduction of User Account Control I assume you would need to Run as administrator, not just be a member of the Administrators group.
It should generally be read that Run as administrator means Run as the user with the account name Administrator not Run as any user who holds membership of the Administrators group.
To Run as administrator, right click on the batch file and select Run as administrator from the context menu.
There are other ways of running as Administrator too.
You can use a self-elevating batch file, which usually uses a PowerShell or WSH helper function.
You can use Task Scheduler and choose the appropriate triggers and account information, (possibly using the SYSTEM account).
Additionally you need to ensure that DevCon.exe is either:
Along side the batch file, "%~dp0DevCon.exe" Disable "PCI\VEN_1002&DEV_687F*"
At a location defined within %PATH%, DevCon Disable "PCI\VEN_1002&DEV_687F*"
Invoked using its full path, "C:\Tools\DevCon.exe" Disable "PCI\VEN_1002&DEV_687F*"
In all cases above please note the asterisk which is missing from your examples

Related

Get administrator privilegs in Qt during runtime [duplicate]

Is it possible to get a C++ application running in Windows to request administrator privileges from the operating system at run time?
I know it can be done at compile time, but can't seem to find anywhere whether it can be done at run time.
Thanks for your help!
EDIT: What if I want the current instance to have elevated privileges? For example, I might have data stored in memory which I want to keep.
If you want the application to always elevate, you can give it a manifest, either by building one in (not compiling technically) or by putting an external manifest in the same folder as the exe. If you want to decide, as a person, to run it elevated, you right click the exe or short cut and choose Run As Administrator. If you are launching it from code, then as #vcsjones comments, you use the runas verb when you launch that process. For example:
ShellExecute( NULL,
"runas",
"c:\\windows\\notepad.exe",
" c:\\temp\\report.txt",
NULL, // default dir
SW_SHOWNORMAL
);
You can elevate a process only during its creation. When a process already runs, there's no way to change its security token: it either runs elevated or not.
If your application needs to perform an administrative task, and it usually runs non-elevated, you have to create another .exe which will request elevation with its manifest. To start a process elevated, you have to use ShellExecute or ShellExecuteEx function. From your main process you will need a way to pass the commands to that new process that will run elevated.
For more information about UAC, read Designing UAC Applications for Windows Vista series.
Not quite, but you can do the opposite—you can drop privileges if you already have them. So, you can have your program start out running as an Administrator, using one of the methods listed by Kate Gregory. Then, drop your unneeded privileges; see Dropping privileges in C++ on Windows for how to do that.
Add a manifest file into your EXE as described here.
http://msdn.microsoft.com/en-us/library/bb756929.aspx
Your process (and threads) have a token assinged to them. That token already have all your groups set up. Under UAC, the Administrator group is disabled. UAC will remove that disabled group so you end up with a full administrator token.
To acheive the same, you must have the TCB priviledge. In other words, to elevate a process at runtime, you will need help from a process running under the SYSTEM account, and Microsoft isn't providing one, nor an API to control the current UAC implementation. Otherwise, it would defeat the purpose.
For the sake of completness, there is a whitelist of process that can perform some elevated operations without prompting. In short, your executable needs :
To be signed by Microsoft
To perform predefined operations, like with IFileOperation
The best explanation I found is this hack. It has been fixed since then, but is sheds some light on the whole thing.

Delphi all-in-one .exe storage [duplicate]

This question already has answers here:
How do I update the running EXE?
(2 answers)
Closed 8 years ago.
I write a very tiny TSR program (passwordmanager.exe) and have two very small files of records.
Actually i save all files (exe, data) to one USB-Stick the user always transports, wherat the data contains sensible data.
Is there any technique to modify the internal resource of the passwordmanager.exe itself that is currently running? As far as i know the exe is copied to the RAM, so the passwordmanager.exe may have no write lock and i am able to let the passwordmanager.exe grow if the user enters new Passwords.
Why do i think this may work? Years ago i had a MSDOS program who asks for the password but unfortunatelly the user has forgotten his password. As i printed the contents of the MSDOS-Program to the console the user luckily found his password in the machine-code!
Question: How can i store the two very small files of records into the exe?
In your comments you wrote "Yes the antiviral program will get upset, i dont care"
Well, if you don't care that your program might be blocked. and you have write permissions to that USB device, I can think of a pattern like this:
Copy your running.EXE (Application.ExeName) to a patch.EXE (via CopyFile) - or generate that patch.EXE from a Resource (Antivirus would NOT like this!).
running.EXE Execute/Create new process patch.EXE with parameters e.g.
patch.EXE /update /your_record_parameters /pid:running_process_id
patch.EXE will start and check for /update; Signal running.EXE to shutdown; Wait for it to shut down; patch running.EXE; Execute running.EXE /patch_done; Shut down itself.
Finally, running.EXE could clean up now by checking the /patch_done and Delete patch.EXE
On NTFS, you could use alternate file streams:
http://support.microsoft.com/kb/105763
(I remember round about when Win2K first came out, seeing a magazine article (UK PC Pro) which showed how to use Notepad to store a secondary stream in an .Exe and was astonished that any OS with pretensions of security would provide this facility, but there you go.)
A tool for viewing them:
http://www.nirsoft.net/utils/alternate_data_streams.html
Is there any technique to modify the internal resource of the passwordmanager.exe itself that is currently running?
No there is not. When a process is started its executable file is locked exclusively and so cannot be modified.

Run a bootable USB of command prompt compatible with multiboot

I need to know if a program exists ( or if I can create) that runs that computer's command prompt from a USB, without having to log in. I guess I don't really care if it is that computers cmd, but I need all of the modern functions of today's cmd. I also need to make sure that it has full administrative privileges. I know that it is possible, because just about every Linux system uses a similar system when it initially boots up, even when just downloaded to USB.
I guess I really need something that I can use with multiboot (a pure ISO file, not something like Rufus, which requires you to format usb). I don't know, but I don't think an ms dos thingy would have all of today's commands in cmd.
Any help much appreciated. Piece.
Edit: I just need the equvolent of single user mode in a Mac. Administrative access to the terminal without login info.
You can boot windows to safe mode limited command prompt, or you can launch a cmd window via the startup group (but you can't get a full screen after XP and the window can be closed with the latter method).
That is the first issue for you to solve, and then you can consider booting from USB.

How are windows programs installed? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
This looks like a common question. But I was not able to find answer for it. When we try to install windows programs, what exactly happens? What files are copied where? What is written in the registry?
Most programs come with an installation program named Setup.exe or Install.exe. When you install a program, the installation program usually does the following:
Looks for a previous version of the program on your hard disk. If it
finds a previous version, the program may ask whether you want to
replace the previous version.
Creates a folder in which to store the program files. Most
installation programs ask where you'd like this folder. Some
installation programs also create additional folders within this
folder. Windows creates a folder named Program Files, usually in C:\
(if Windows is stored in a partition or drive other than C, the
Program Files folder is usually in the same partition). We recommend
you install all your programs in folders within the Program Files
folder.
note Some software vendors have the bad habit of installing
application programs in locations other than your Program Files
folder. You can't do much about this; the additional folders may
clutter up your root folder, but they don't do any harm.
Copies the files onto your hard disk. If the program files are
compressed, the installation program uncompresses them. Usually, the
installation program copies most of the files into the program's
folder, but it may also put some files into your C:\Windows,
C:\Windows\System, or other folders.
Checks your system for the files and hardware it needs to run. For
example, an Internet connection program might check for a modem.
Adds entries to the Windows Registry to tell Windows which types of
files the program works with, which files the program is stored in,
and other information about the program.
Adds a command for the program to your Start | All Programs menu
(some programs add submenus to the Start | All Programs menu to
contain several commands). The installation program may also add a
shortcut to your Windows desktop to make running the program easy for
you. You can change the position on the Start menu of the command for
the program, get rid of the command, or create a command if the
installation program doesn't make one. You can also create a shortcut
icon on the desktop, if the installation program hasn't done so, or
move or delete the program's shortcut.
Asks you a series of questions to configure the program for your
system. The program may ask you to type additional information, like
Internet addresses, passwords, or software license numbers. It may
also ask which users should be able to run the program.
Every installation program is different, because it comes with the application program, not with Windows. If your computer is connected to a LAN or to the Internet, the installation program may configure your program to connect to other computers on the network.

Overcoming "It is being used by another person or program." [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Is there a way to unlock Windows files without downloading a utility?
I have a few files on my Windows XP C: drive that are very old and very useless. When I try to delete these files I get the following message:
Cannot delete FILENAME.zip: It is being used by another person or program
Close any programs that might be using the file and try again.
No one is accessing this file. No program is using it currently. Windows has screwed up the file locking mechanism.
Is there a way to delete this file without downloading someone's unlocking utility? I find the sites offering these programs to be a tad sketchy.
How could you force the file to unlock from within a program? I'm competent in Java, Perl, and Ruby, but I haven't seen anything among their libraries that would aid me here.
I've successfully used Process Explorer to find out which process has the file open. It saves a reboot that may not fix the problem anyway.
In process explorer: Find > Handle or DLL... then search for the name of the folder/file, then double click one of the search results. It'll select a handle in the main window, which you can right click and close.
Try downloading "Unlocker". Google it and take my words that it doesn't have any worm/spyware/virus. It is pretty cool utility and works great. Give it a try.
Did you try the commandline command OpenFiles
It is built in (XP and above I believe) and has several arguments that can be passed in.
Use msconfig and start up with everything turned off.
Then try to move / delete the file.
Or you can always boot up in safe mode and delete it.
You do that by hitting f8 when the machine boots up.
If you reboot and the files are still locked, then there is some process on your machine that is still using them. First you should figure out what that process is and determine if the files really aren't used any more or not.
Rebooting to Safe Mode is often a very easy way to do it. When you boot in safe mode, it won't load all the stuff set to run on startup. Press F8 while it's booting to access the boot menu, and choose "safe mode".
I had a .jpg pfile that hasd that issue and I couldn't delete. That brought me to this thread. When nothing else worked I renamed the file and left off the .jpg. THEN I could delete it easily. Not sure why, but worked for me
You don't need any utility.
Just use Win32 api to unlock them (simply close the handle)

Resources