Create Registry Key (and Subkeys)?

[亡魂溺海] 提交于 2019-12-02 13:59:29

There are several issues with your code, aside from the fact that you posted an incomplete code sample.

  • "winmgmts:{impersonationLevel = impersonate}! \\" & strComputer & "\root\default:StdRegProv"
    The WMI moniker contains a spurious space between security settings and path (...! \\...). Remove it.
    As a side note, it's pointless to use a variable for the hostname if that hostname never changes.
  • strPath = strKeyPath & "\" & strSubPath
    You define strPath before you define the variables you build the path from. Also, your path components are defined as string literals, so you could drop the concatenation and the additional variables and simply define strPath as a string literal.
  • ObjRegistry.CreateKey (HKEY_LOCAL_MACHINE, strPath)
    You must not put argument lists in parentheses unless you're calling the function/method/procedure in a subexpression context. See here for more details. However, you may want to check the return value of your method calls to see if they were successful.

And FTR, hungarian notation is pointless code bloat. Don't use it.

Modified code:

Function SetEnterpriseMode(value)
    Const HKLM = &h80000002

    Set reg = GetObject("winmgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv")

    path = "Software\Policies\Microsoft\Internet Explorer\Main\EnterpriseMode"
    name = "Enabled"

    rc = reg.CreateKey(HKLM, path)
    If rc <> 0 Then
        MsgBox "Cannot create key (" & rc & ")."
        Exit Function
    End If

    rc = reg.SetStringValue(HKLM, path, name, value)
    If rc = 0 Then
        MsgBox "Successfully enabled Internet Explorer Enterprise Mode."
    Else
        MsgBox "Cannot set value (" & rc & ")."
    End If
End Function
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!