简体   繁体   中英

Slashes in windows registry key

I'm trying to set full app path of test.exe in registry as name. But it gives me wrong result.

Expected Output:

在此处输入图像描述

Output:

在此处输入图像描述 This is the code I'm using

Dim WshShell, bKey
Set WshShell = WScript.CreateObject("WScript.Shell")


WshShell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers\D:\\\Program Files\\\test.exe", "RUNASADMIN", "REG_SZ"

Is there any workaround for this?

This MSDN KB article says:

Due to the limitations of the RegWrite method of Windows Script Host (WSH) it is not possible to write a "\\" (backslash) in a key name or value name.

This is by design and there is no workaround with WSH. The article goes on to suggest using alternative scripting objects (WMI, RegObj.dll) to set such key and value names.

Still using vbscript, try to create a .reg file and execute it.

Some code that does it in another path of the registry:

Set fs = CreateObject("Scripting.FileSystemObject")
Set shell = CreateObject("WScript.Shell")

'create .reg file:
Set reg_file = fs.CreateTextFile("slash.reg")
reg_file.WriteLine "Windows Registry Editor Version 5.00"
reg_file.WriteLine "[HKEY_CLASSES_ROOT\.txt]" 'put your path here

key_name = "D:\\Program Files\\test.exe" 'must be escaped inside the .reg file, so they enter as single slash in the registry
key_value = "RUNASADMIN"

reg_file.WriteLine """" & key_name & """=""" & key_value & """" 'escaping quotes inside vbscript string literal
reg_file.Close

'run it automatically to insert data (may ask for elevated privileges):
path = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
shell.run "regedit.exe /s """ & path & "slash.reg"""

Just click OK when asked for elevation. You may want to check the created file, so I am not deleting it in my code.

Another approach is to use WMI Registry provider

Const REG_HIVE_HKLM = &H80000002
Const ROOT = "Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"

Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set SWbemServicesReg = SWBemlocator.ConnectServer(".", "root\DEFAULT","","")
Set reg = SWbemServicesReg.Get("StdRegProv")

' if key is missing - create first, otherwise value won't be saved (without exception)
reg.CreateKey REG_HIVE_HKLM, ROOT

' set value
reg.SetStringValue REG_HIVE_HKLM, ROOT, "D:\Program Files\test.exe", "RUNASADMIN"

Try using slash ( / ) as your file system path separator. WSH will correctly write A *nix style path to a registry value while a Windows style path will write as a sequence of sub keys. However this depends on the software that is reading the registry value to grok the path correctly. Many components of Windows will now accept either path separator. Give it a try.

You can use .ShellExecute to edit the registry with reg.exe .

ShellExecute guide

Syntax:

CreateObject("Shell.Application").ShellExecute "application", "parameters", "dir", "verb", window
CreateObject("Shell.Application").ShellExecute 'some program.exe', '"some parameters with spaces"', , "runas", 1

Key:

Keyword Action
application The file to execute (required)
parameters Arguments for the executable
dir Working directory
verb The operation to execute (runas/open/edit/print)
window (1=normal, 0=hide, 2=Min, 3=max, 4=restore, 5=current, 7=min/inactive, 10=default) View mode application window

Example:

CreateObject("Shell.Application").ShellExecute "reg.exe", "add " & """HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers""" & " /v " & """" & Command & """" & " /t REG_SZ /d " & """~ DISABLEDXMAXIMIZEDWINDOWEDMODE RUNASADMIN HIGHDPIAWARE""" & " /f ", , , 0

Where the Command is a path with a backslash to a .exe (like D:\Path with backslash\some program.exe ) that passed to your application as command line parameters (like start "" "C:\Path\your application.exe" "D:\Path with backslash\some program.exe" .

I used MsgBox to ensure that is correct:

MsgBox "add " & """HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers""" & " /v " & """" & Command & """" & " /t REG_SZ /d " & """~ DISABLEDXMAXIMIZEDWINDOWEDMODE RUNASADMIN HIGHDPIAWARE""" & " /f "

You can also use CreateObject("WScript.Shell").Run as an alternative to run reg.exe and edit the registry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM