VBS RegRead doesn't return value - vbscript

I've got the following code:
Dim objShell,failing_path,working_path
failing_path = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Test"
working_path = "HKEY_LOCAL_MACHINE\SOFTWARE\7-zip\Path"
Set objShell = WScript.CreateObject("WScript.Shell")
WScript.Echo "Working: " & objShell.RegRead(working_path)
WScript.Echo "Not Working: " & objShell.RegRead(failing_path)
When executing I will get the Path from the 7-zip Registrykey, but the Test Key returns following error:
Error says: Registry wasn't opened for reading.
Here the proof that the Test-Key exists:
What am I doing wrong? I also tried to read the key via oReg.GetStringValue but this always returned null.

It seems like your script is running in a 32-Bit compatible scripting host but in a 64-bit OS.
Since 32 bit applications are automatically redirected to the WOW6432Node areas of the registry in 64-Bit OSes, RegRead method tries to read 32-Bit equivalent path like
HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Test
instead
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Test
So, if the redirected path does not exist you receive that error as expected.
You may need to force run your scripts in 64-bit compatible scripting hosts to get rid of that kind of implicit registry redirects.

When I tested this, I was able to read from non-Wow6432Node paths just fine, even when using a 32-bit cscript.exe from a 32-bit cmd.exe (Process Explorer showed image type 32-bit for both processes).
Perhaps it's something really simple? I was able to reproduce the behavior you described when I created a registry value with a spurious trailing space:
>>> key = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
>>> WScript.Echo sh.RegRead(key & "Test")
Unable to open registry key "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\
Uninstall\Test" for reading. (0x80070002)
>>> WScript.Echo sh.RegRead(key & "Test ")
asd

Related

How to escape & ampersand in Custom protocol handler in Windows

I made a custom protocol handler following this link. The case is I need to open a link which can only be opened in IE and might contains several query parameters and should be opened in IE from our web app which is running on Chrome (this is really annoying). After many tries and fails, I managed to find the snippet to add entry to the windows registry hive and made .reg file and run:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Classes\ie]
"URL Protocol"="\"\""
#="\"URL:IE Protocol\""
[HKEY_CURRENT_USER\Software\Classes\ie\DefaultIcon]
#="\"explorer.exe,1\""
[HKEY_CURRENT_USER\Software\Classes\ie\shell]
[HKEY_CURRENT_USER\Software\Classes\ie\shell\open]
[HKEY_CURRENT_USER\Software\Classes\ie\shell\open\command]
#="cmd /k set myvar=%1 & call set myvar=%%myvar:ie:=%% & call \"C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe\" %%myvar%% & exit /B"
It worked but the problem is if a link contains more than 1 query params, all but the first are omitted, I am sure this because of character encoding in windows command line:
e.g. some_example_url?query1=value1&query2=value2&query3=value3 is becoming some_example_url?query1=value1
I find this solution but it did not work either. How can I properly escape & character, so that it can be opened on IE with all query parameters. (as before mentioned link is triggered by web app running on Chrome)
EDIT: The code which triggers:
fileClicked(url) {
// url should be escaped with ^
const escapedUrl = url.replace(/&/gi, '^&');
const ie = document.createElement('a');
// ie: scheme => custom protocol handler
// host computer (windows) should add custom protocol to windows registry
ie.href = `ie:${escapedUrl}`;
ie.click();
}
EDIT 2
#muzafako fixed the script, just last line should be replaced like below:
#="cmd /c set url=\"%1\" & call set url=%%url:ie:=%% & call start iexplore -nosessionmerging -noframemerging %%url%%"
You don't need to decode query parameters at all. I've tried to find solution for this issue and saw this answer. Its works for me. just change command line to:
#="cmd /c set url=\"%1\" & call set url=%%url:ie:=%% & call start iexplore -nosessionmerging -noframemerging %%url%%"

Code Error 800A0409 - Unterminated String Constant (Japanese Windows)

This one is kind of strange to me. Prior to posting I did research this error code and according to many articles out there it seems relatively a straight forward issue. The issue is generally related to either missing double quotes in a string or where a string spans across multiple lines.
However, in my case neither of the above seem to be the cause of the issue. The strange thing is my code works fine in English windows operating system but I encounter this error only in the Japanese version of windows.
The error is pointing to the following line of code:
WScript.Echo "PFXファイル " & pfxFileName & " は存在しません。"
Any ideas why this would only happen in the Japanese version of Windows?
Here is the block of surrounding code in case it gives any further clues:
' check if pfx file exists
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
pfxFileExists = 0
Do
If fso.FileExists(pfxFilePath & pfxFileName) Then
' it exists, continue
pfxFileExists = 1
Else
' The PFX file does not exist.
WScript.Echo "PFXファイル " & pfxFileName & " は存在しません。"
End If
Loop Until pfxFileExists = 1

Unable to Enumerate and Delete Registry Values Using VBScript

I need to iterate through the registry foe each application for which the following key exists:
HKCU\Software\Microsoft\Office\15.0\[NAME OF APPLICATION]\Resiliency\DisabledItems
So that I can delete any values stored here.
I wrote the below script, but all this seems to do is iterate through the keys until it reaches Outlook, at which point it assures me the key does not exist (even though it definitely does), and then the script stops running.
'***********************************************'
'------------------------------------Add-In Keys'
'***********************************************'
Sub AddInKeys()
On Error Resume Next
strOfficePath = "Software\Microsoft\Office\15.0\"
objReg.EnumKey conHKEY_CURRENT_USER, strOfficePath, arrOfficeSubKeys
for Each key in arrOfficeSubKeys
' Check if our DisabledItems key exists
If regExists("HKCU\Software\Microsoft\Office\15.0\" & key & "\Resiliency\DisabledItems\") Then
' If it does - enumerate the values under this key ...
objReg.EnumValues conHKEY_CURRENT_USER, strOfficePath & key & "\Resiliency\DisabledItems\", arrKeyValues
For Each value in arrKeyValues
' Delete key VALUE, but only IF it is not blank (this will be the default value)
If value <> "" Then
objShell.RegDelete "HKCU\Software\Microsoft\Office\15.0\" & key & "\Resiliency\DisabledItems\" & value
End If
Next
End If
Next
If Err <> 0 Then
strMessage = "ERROR: Sub - Add-In Keys"
End If
End Sub
'***********************************************'
'---Function to check existence of registry keys'
'***********************************************'
Function regExists(sKey)
ON ERROR RESUME NEXT
regExists = objShell.RegRead(sKey)
If Err.Number = 0 Then
regExists = True
Else
regExists = False
Err.Number = 0
End If
If Err <> 0 Then
strMessage = "ERROR: Sub - regExists"
End If
End Function
Some background: The script seems to work perfectly when I run it on my dev machine. It enumerates all keys and all values and deletes the ones which I need deleting. However, when I run this from a thin client (which is where the script will be deployed - in a log on script), I am seeing the behaviour described above.
When I load the registry hive from the test user (who is logged into the thin client), I can see that there are many more keys in addition to those being checked, but for some reason it stops checking after Outlook.
Am I missing some sort of error, or am I misunderstanding how the registry works?
This was due to the way our Citrix and Windows environment is set up.
The logon script is called from the server via Active Directory User -> Properties -> Profile
The script above where I was having the issue was executing locally on the machine, and so none of the application's registry keys were present, as these applications are installed on the server.
After adding my code to the server-hosted logon script, it was able to detect and delete all the required key values.

Error reading registry entry for Firefox

I have an AutoIt v3 script (copied from the author of FF.au3):
#Include <FF.au3>
_FFStart("http://ff-au3-example.thorsten-willert.de/")
If _FFIsConnected() Then
Sleep(2000)
_FFAction("presentationmode", True)
Sleep(2000)
_FFOpenURL("http://www.google.com")
Sleep(2000)
_FFAction("back")
_FFAction("presentationmode", False)
Sleep(2000)
_FFOpenURL("chrome:bookmarks")
Sleep(2000)
_FFAction("alert", "Bye bye ...")
_FFQuit()
EndIf
Exit
But when I run it, I get an error message:
__FFStartProcess ==> General Error: Error reading registry entry for FireFox.
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Mozilla\Mozilla Firefox\*CurrentVersion*\Main\PathToExe
Error from RegRead: 1
I have Firefox and AutoIt v3 installed, I downloaded the FF.au3 UDF to same directory as my script, and I have MozRepl Firefox plugin installed and activated (it's active at the menu->plug-ins, I don't see the "Activate on startup" option). I do have an entry at:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Mozilla\Mozilla Firefox\26.0 (pl)\Main
That points to the right destination. Why is there an "Error reading registry entry for FireFox."?
Here is the relevant piece of code from the Firefox library:
Local $sHKLM = 'HKEY_LOCAL_MACHINE\SOFTWARE\'
If #OSArch <> 'X86' Then $sHKLM &= 'Wow6432Node\'
$sHKLM &= 'Mozilla\Mozilla Firefox'
Local $sFFExe = RegRead($sHKLM & "" & RegRead($sHKLM, "CurrentVersion") & "\Main", "PathToExe")
If #error Then
SetError(__FFError($sFuncName, $_FF_ERROR_GeneralError, "Error reading registry entry for FireFox." & #CRLF & _
$sHKLM & "\*CurrentVersion*\Main\PathToExe" & #CRLF & _
"Error from RegRead: " & #error))
Return 0
EndIf
It reads the key CurrentVersion (probably of type REG_SZ) from the path HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Mozilla\Mozilla Firefox to get the current version of the application. Say for example that this returns the string "27.0".
Then it looks in HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Mozilla\Mozilla Firefox\27.0\Main (based on the string it just found for the current version) for the key PathToExe (REG_SZ also probably). This is the first attempt and if it does not fail it uses this path for the executable.
If this fails though, it checks the path HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Mozilla\Mozilla Firefox\*CurrentVersion*\Main (literally with the asterisks) for the key PathToExe. This fails as well and that's why you are getting the error.
Check all of the above registry paths for your system. It may be possible that the Firefox library needs to be updated for the later Firefox versions. Try an "as clean as possible" install of Firefox and see if that works. Also try repairing the installation via the setup/deinstaller if that is possible.
If nothing works and you are left to modifying your own system to get it to work, I will check and ask the author of the Firefox library to update it.
I think there's a missing backslash. In the FF.au3 UDF change the following:
Local $sFFExe = RegRead($sHKLM & "" & RegRead($sHKLM, "CurrentVersion") & "\Main", "PathToExe")
to:
Local $sFFExe = RegRead($sHKLM & "\" & RegRead($sHKLM, "CurrentVersion") & "\Main", "PathToExe")
Regards,
Gonnosuke
Most registry manipulation problems are about lack of permissions.
Add #RequireAdmin at the top of your code.
"When running on 64-bit Windows if you want to read a value specific to the 64-bit environment you have to suffix the HK... with 64 i.e. HKLM64."

Delete file in windows 7 using VB.NET

I have written following code in vb.net to delete the file.
If File.Exists(strPath & "\ReportEng.ini") = True Then
File.SetAttributes(strPath & "\ReportEng.ini", FileAttributes.Normal)
File.Delete(strPath & "\ReportEng.ini")
End If
File.Copy("\\192.168.0.1\SAP_Shared\AddonExtra\ReportEng.ini", strPath & "\ReportEng.ini")
This code works perfectly in windows xp. But in Windows 7,I can not delete it. This OS is hectic OS from developer's point of view. Some or other problem occurs and Microsoft has not considered the developer while building this OS.
How do I delete file in Windows 7 ?
It's so easy to do so;
If My.Computer.FileSystem.FileExists("C:\somefile.ext") Then 'Check whether file exists
My.Computer.FileSystem.DeleteFile("C:\somefile.ext") 'Delete the file!
End If
Have a nice day!
You don't need to delete the file: there is an overload File.Copy Method (String, String, Boolean) which allows overwriting.
You didn't say what error you get. I suspect it is because the user doesn't have write access to the directory. You should probably be using a subdirectory of the directory returned by Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) or maybe .LocalApplicationData, and definitely not the directory containing the program.
Also, using Path.Combine(strPath, "ReportEng.ini") is how you're meant to combine paths - it'll take care of, e.g., the trailing path separator for you.
The preferred method for interfacing with the Windows file system uses the following namespace:
Imports Microsoft.VisualBasic.FileIO.FileSystem
To delete a file:
Dim FileLocation As String = strPath & "\ReportEng.ini"
If Not GetDirectoryInfo(FileLocation).Exists Then
GetFileInfo(FileLocation).Delete()
End If

Resources