1

how do we convert following vbscript to javascript?

<script type="text/vbscript">

  Function SayHello()
    MsgBox "Hello"

HKEY_LOCAL_MACHINE = "&H80000002"
uninstallRegKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
stdRegPro = "winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\default:StdRegProv"

Set objReg=GetObject(stdRegPro)

objReg.EnumKey HKEY_LOCAL_MACHINE, uninstallRegKey, arrSubKeys
MsgBox arrSubKeys

  End Function

</script>

Any help appreciated.

Thanks, Lok.

1
  • 3
    Do you mean JScript used in the Windows Script Host (cscript/wscript), or JavaScript run in browsers? Commented Nov 14, 2014 at 8:34

2 Answers 2

2

You can, if you use JScript (Microsoft's implementation of Javascript for Windows) and some information e.g.

  1. Calling WMI Methods with JScript
  2. Troubles with WMI in JScript

(found by googling "jscript wmi").

Evidence:

function showUnInstall() {
    var HKEY_LOCAL_MACHINE = 0x80000002;
    var uninstallRegKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
    var stdRegPro = "winmgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv";
    var objReg = GetObject(stdRegPro);

    var mEnumKey = objReg.Methods_.Item("EnumKey");
    var ipEnumKey = mEnumKey.InParameters.SpawnInstance_();
    ipEnumKey.hDefKey = HKEY_LOCAL_MACHINE;
    ipEnumKey.sSubKeyName = uninstallRegKey;

    var mGetStringValue = objReg.Methods_.Item("GetStringValue");
    var ipGetStringValue = mGetStringValue.InParameters.SpawnInstance_();
    ipGetStringValue.hDefKey = HKEY_LOCAL_MACHINE;
    ipGetStringValue.sValueName = "DisplayName";

    var opEnumKey = objReg.ExecMethod_(mEnumKey.name, ipEnumKey);
    if (0 === opEnumKey.ReturnValue) {
        var aNames = opEnumKey.sNames.toArray();
        for ( var i = 0; i < aNames.length; ++i) {
            ipGetStringValue.sSubKeyName = uninstallRegKey + "\\" + aNames[i];
            var opGetStringValue = objReg.ExecMethod_(mGetStringValue.name, ipGetStringValue);
            if (0 === opGetStringValue.ReturnValue) {
                WScript.Echo(opGetStringValue.sValue);
            } else {
                WScript.Echo("ERROR: GetStringValue.ReturnValue =", opGetStringValue.ReturnValue);
            }
        }
    } else {
        WScript.Echo("ERROR: EnumKey.ReturnValue =", opEnumKey.ReturnValue);
    }
}

output:

cscript 26907078.js
7-Zip 4.65
ActiveState ActiveTcl 8.5.2.0
ERROR: GetStringValue.ReturnValue = 1
Adobe Flash Player 15 Plugin
ERROR: GetStringValue.ReturnValue = 1
CMake 2.8, a cross-platform, open-source build system
Acrobat.com
...
Sign up to request clarification or add additional context in comments.

1 Comment

Working !! Just a little change. Instead of var objReg = GetObject(stdRegPro); I did var objReg = new ActiveXObject("WbemScripting.SWbemLocator"); Because it was throwing error "JavaScript runtime error: 'GetObject' is undefined"
1

You can't. Javascript does not have access to the registry.


This is not entirely accurate, I remember. A Node.js webserver running on Windows with NPM tools does have access, but only to that on the server it's running on. however, client-side javascript does not have access to the registry.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.