Desktop Refresh via dll - windows

My personal concern for this problem is because of a dynamic desktop program i am creating which the aim is for users to click a folder on a desktop and the contents of that folder becomes the new desktop. (I will post the code as an answer below as so to not convolute my actual question). However part of the code needs to kill and restart the explorer.exe process in order to reinitialize the desktop to display the new location.
Documentation of this problem is extremely difficult to find as its more technical than most people are willing to go for this particular field. This man is trying to do the exact same thing as me except using autoit, and here users looked more into doing it vbscript side but both came to the same result of killing and restarting explorer.exe in order to update the desktop.
My issue with killing the explorer.exe process in a forceful manner can result in unstable systems and the actual killing of the process takes a longer time to reboot the desktop than whatever action occurs when you simply move the desktop location. I want to know how i can update my desktop, by calling the dlls that update it, but from within a batch and vbscript hybrid.
EDIT:
Investigations to a command such as rundll32.exe user32.dll,LockWorkStation, and later to the user32.dll dependencies has uncovered multiple uses of desktop functions, in which i assume is used to update the desktop in some form. If you would like to view this, download dependency walker and open it to this folder from within the program. (C:\Windows\WinSxS\amd64_microsoft-windows-user32_31bf3856ad364e35_6.3.9600.18123_none_be367a2e4123fd9d\user32.dll)

Here is the manual changing of the desktop via batch and vbs hybrid. Its not perfect, but it provides a nice interface between moving in and out of directories to select the one you want to update to. This uses the taskkill which i want to depreciate with something else.
This is the initial batch script...
#echo off
setlocal enableextensions
::Below computes the desktop location that the program will reference
for /f %%a in ('cscript //nologo C:\HighestPrivelege\DesktopTools\findDesktop.vbs') do set "fold=%%a"
echo %fold%
::this loop will allow users to input new locations for the desktop by
moving through a terminal. Wildcards and autotab completion is usable are
and lack of input results in continuation of script
:loop
echo ################## %fold%
dir %fold% /A:D /B
echo _________________________
set loc=
set /p loc=[New Location?]
cd %fold%\%loc%
set fold=%cd%
IF [%loc%]==[] (goto cont) ELSE (goto loop)
:cont
::Below is the program that runs the regedit rewrite of the desktop variable
for the current user. It passes the decided folder value from earlier as the
new desktop.
cscript //nologo C:\HighestPrivelege\DesktopTools\setdesktop.vbs %fold%
::This restarts explorer.exe in order to reboot the newly created desktop
location. I wish i didnt have to kill explorer.exe forcefully in the process
but so far its the only way to make this program work.
taskkill /F /IM explorer.exe
explorer.exe
endlocal
pause
exit
and the VBS script that follows...
Option Explicit
'variable invocation
Dim objShell, strDesktop, strModify, strDelete, fso, f, File, content, parthe, args, arg1
Set args = WScript.Arguments
set objShell = CreateObject("WScript.Shell")
set fso = CreateObject("Scripting.FileSystemObject")
'this will take in the new desktop location.
set file= fso.OpenTextFile("C:\HighestPrivelege\DesktopTemporary.txt")
content = file.ReadAll
arg1 = args.Item(0)
parthe = ""&arg1&""
'The actual rewrite of the registry file containing the script lies below.
strDesktop = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop"
strModify = objShell.RegWrite(strDesktop,parthe,"REG_EXPAND_SZ")
Activating the batch script will prompt user for where they would like to relocate their desktop to, and pass that variable into the VBscript to be rewrited to the registry. Upon VBScript completion, windows explorer will restart itself in order to reinitialize the new desktop location.
Until i can get a working model of the one where all i have to do is click/interact with the folder to initialize the program, this manual one will have to do.

Related

Accidental VBscript fork bomb/infinite loop

This is the first VBscript I've ever written, so I could definitely use some guidance.
Through research and snippet browsing, I've generated the following vbscript to launch a batch file.
dim shell
set shell = CreateObject("WScript.Shell")
shell.Run "permissions.bat"
set shell=nothing
WScript.Quit
The contents of permissions.bat are:
icacls "%PROGRAMFILES(X86)%\programFolder\mySoftwarePackage" /grant "NT AUTHORITY\Authenticated Users":(OI)(CI)M /t
PAUSE
The problem I am having is that when I launch the vbscript, it never stops launching my bat file. It continuously launches instances just like a fork bomb and eventually crashes the windows kernel. Does anybody know what I did wrong here? I didn't put a while loop anywhere.

Windows CMD Start and wait for the default application in a batch file

I am trying to start the default application for a file, wait for it to complete, and then continue with my batch file. The problem is that start, when used below simply creates another command prompt window with the example.doc in the title bar. I can use call instead of start, but then call does not wait for the program to finish before going to the next line. It appears that start needs to have an executable name and will not work with the default application system in windows.
Any ideas how I can make this happen without having to hardcode the windows application in the batch file as well?
set filename=example.doc
start /wait %filename%
copy %filename% %filename%.bak
How do I start the default application for a file, wait for completion, then continue?
It appears that start needs to have an executable name and will not work with the default application system in windows.
start, when used below simply creates another command prompt window with the example.doc in the title bar
start /wait %filename%
The above command won't work because %filename% is used as the window title instead of a command to run.
Always include a TITLE this can be a simple string like "My Script" or just a pair of empty quotes ""
According to the Microsoft documentation, the title is optional, but depending on the other options chosen you can have problems if it is omitted.
Source start
Try the following command instead:
start "" /wait %filename%
Alternative solution using the default open command
Any ideas how I can make this happen without having to hardcode the
windows application in the batch file as well?
One way is to use assoc and ftype to get the default open command used for the file then execute that command.
The following batch file does that for you (so no hard coding of windows applications is needed).
Open.cmd:
#echo off
setlocal enabledelayedexpansion
set _file=example.doc
rem get the extension
for %%a in (%_file%) do (
set _ext=%%~xa
)
rem get the filetype associated with the extension
for /f "usebackq tokens=2 delims==" %%b in (`assoc %_ext%`) do (
set _assoc=%%b
)
rem get the open command used for files of type filetype
for /f "usebackq tokens=2 delims==" %%c in (`ftype %_assoc%`) do (
set _command=%%c
rem replace %1 in the open command with the filename
set _command=!_command:%%1=%_file%!
)
rem run the command and wait for it to finish.
start "" /wait %_command%
copy %_file% %_file%.bak 1>nul
endlocal
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
assoc - Display or change the association between a file extension and a fileType
enabledelayedexpansion - Delayed Expansion will cause variables to be expanded at execution time rather than at parse time.
for - Conditionally perform a command several times.
for /f - Loop command against the results of another command.
ftype - Display or change the link between a FileType and an executable program.
start - Start a program, command or batch script (opens in a new window).
variable edit/replace - Edit and replace the characters assigned to a string variable.
Simply use the filename directly as command, unless that filename is a batch file, in which case use call.
In a batch file invocation of a GUI subsystem executable is blocking, unlike for an interactive command.
Use the start command when you don't want blocking execution.
There is a subtle point about “default application”, namely that a file type can have a registered default application for the graphical shell, e.g. its “Open with…”, without having an assoc/ftype association, or different from that association.
I'm not entirely sure of which registry entries are used for this. I've always had to look it up and research it each time. As I recall it's not well-documented.
But hopefully you're OK with just the assoc/ftype scheme.
A further subtle point about “default application”: on the laptop I'm writing this on the ftype association for text files is to open them in Notepad:
[H:\forums\so]
> assoc .txt
.txt=txtfile
[H:\forums\so]
> ftype txtfile
txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1
[H:\forums\so]
> _
And this is what the graphical shell (Windows Explorer) will do.
But cmd.exe looks inside files, and if it finds an executable signature then it tries to run the text file as an executable, even in Windows 10:
[H:\forums\so]
> echo MZ bah! >oops.txt
[H:\forums\so]
> oops.txt
This version of H:\forums\so\oops.txt is not compatible with the version of Windows you're running. Check your computer's system information and then contact the software publisher.
[H:\forums\so]
> _

Simple shutdown script does not function properly after PC wipe

I had a simple VB Script that would let me enter in the amount of minutes before I wanted my PC to turn off by itself, and then it would auto-shutdown. That worked fine. After I wiped my PC, the script no longer functions as intended, instead showing a blank cmd window after I enter the number of minutes before shutdown, and displays the inputbox again (asking for # of minutes before shutdown).
Any ideas on why this won't function correctly, and why it worked before but not now? Do I need a certain package from Microsoft that maybe I didn't reinstall?
Code:
Dim a
Dim oShell
a=inputbox("After how many minutes would you like to shut down your PC? Enter cancel to cancel a previous shutdown")
Set oShell = WScript.CreateObject ("WScript.Shell")
if a = "cancel" then
oShell.run "cmd.exe /c shutdown /a"
elseif a = "" then
MsgBox"Please enter after how many minutes you would like to turn off this PC",0+16,"Enter a number"
elseif a = "0" then
b=msgbox("Are you sure you want to shut down this PC immediately?",4+32,"Shut down immediately?")
if b = "6" then
oShell.run "cmd.exe /c shutdown /s /f"
end if
else
oShell.run "cmd.exe /c shutdown /s /t " & (a * 60)
end if
EDIT: Running the script from its directory works as intended, but running the VBScript from a shortcut (as a I had been doing) doesn't work and yields the above results.
EDIT: Also the script itself won't run properly on my desktop, but runs fine in the folder I store my scripts.
You named the script shutdown.vbs and run it with the working directory set to the directory containing the script. By running oShell.Run "cmd.exe /c shutdown ..." your script is effectively calling itself.
If you call a command shutdown (without path and extension) the system is looking for a file with one of the extensions listed in %PATHEXT% in the directories listed in the %PATH% environment variable. The first match wins.
Since on Windows the current directory comes first in the %PATH% the file %CD%\shutdown.vbs is found before %windir%\system32\shutdown.exe.
Either rename your VBScript or change cmd.exe /c shutdown to cmd.exe /c shutdown.exe and the problem will disappear.

Batch file to start all programmes in the start folder of XP

I need start all folders in a the Windows "Start/Programs/Startup folder" of an XP machine, explorer is disabled to stop top people playing and remove the Start and Task-bar.
I can run a batch file at start-up but how do I write the batch to run ALL programs in the "Start/Programs/Startup folder" the programs in the folder may change but the batch needs to remain the same
I am able to open each file individually using the below code but I really need to be able to open everything in the folder to avoid problems in the future
start "" /b "C:\Documents and Settings\User\Start Menu\Programs\Startup\PROG.appref-ms"
I have tried the code below, that batch starts but nothing starts
%DIR%=C:\Documents and Settings\Pete\Start Menu\Programs\Startup
for %%a in (%DIR%\*) do "%%a"
Running the batch from the desktop also doesn't run the programs in the start folder, the DIR address is taken from windows explorer when I navigated to the folder with the short cuts in
This is an interesting request--one that I would question the motive behind, but since you asked, here's a way you could accomplish it:
#echo off
set DIR=C:\Your\Directory
for %%a in ("%DIR%\*") do "%%a"

Can I set an environment variable for an application using a shortcut in Windows?

I have a feeling I should be able add a directory to the PATH environment variable on an application-lifetime basis, but I can't find out how to do this. Is it possible to add a parameter to a Windows shortcut that appends a directory to the current value of PATH for use by the application being linked?
As explained here: http://www.labs64.com/blog/2012/06/set-environment-variables-in-windows-shortcut/
you can do it without a bat file too.
Set Target to e.g.:
C:\Windows\System32\cmd.exe /c "SET path=%path%&& START /D ^"C:\Program Files (x86)\Notepad++^" notepad++.exe"
To avoid see the command prompt for a split second before it close again, you should set
Run: Minimized
on the Shortcut tab
(Tested on Windows 7, Windows 10)
Let the shortcut execute a batch file (.cmd), that
Sets the environment variable
execute the app
You use "START" to execute the app, this will start the app in another process, but it will copy the environment. You do not wait for the app to finish.
Now you can exit the batch file.
Should look like this:
#echo off
set path=%path%;C:\My Folder
start "Window Title" "Path to my exe"
Linking directly to a batch file spawns an annoying console that you probably want to avoid. Here's a work-around. The simpler solution is to use the "Start Minimized" option in your link, but on Windows 7 you'll see a momentary console light up your task bar.
start.bat:
#echo off
IF "%1" == "" GOTO Error
IF "%2" == "" GOTO Error
IF NOT EXIST %2 GOTO Error
SET PATH=%1;%PATH%
start %2
GOTO End
:Error
echo Problem!
pause
:End
shortcut target:
MyPath = "C:\MyApp"
Set shell = WScript.CreateObject("WScript.Shell")
cmd = "start.bat " & MyPath & " MyApp.exe"
shell.Run cmd, 0, false
Set env = Nothing
Set shell = Nothing
You can do this with PowerShell easily. PowerShell exposes environment variables using the $env: prefix. For example, I wanted to launch TeamSQL with custom JAVA_HOME and PATH environment variables, so I could connect to a PostgreSQL database. TeamSQL depends on JDK / OpenJDK for this purpose.
First, I downloaded pre-built OpenJDK and extracted the ZIP archive with 7-Zip.
Next, in PowerShell, I ran the following:
$env:JAVA_HOME='C:\Users\TrevorSullivan\Downloads\openjdk\jdk-11.0.2\'
$env:PATH += ';%JAVA_HOME%\bin'
# Launch TeamSQL
& C:\Users\TrevorSullivan\AppData\Local\Programs\TeamSQL\TeamSQL.exe
Store that PowerShell code in a .ps1 file, and you can run it with PowerShell. Because child processes inherit the environment variables from the PowerShell session, your program is good to go.

Resources