Error 87 (ERROR_INVALID_PARAMETER) Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Error 87 (ERROR_INVALID_PARAMETER)

Post by Ecimeric » 21 Jun 2021, 19:31

Code: Select all

ValueType := "REG_DWORD"
KeyName   := "HKLM\SOFTWARE\Policies\Google\Chrome"
ValueName := "IncognitoModeAvailability"

Run, % "AutoHotkeyU32.exe RegWrite.ahk " ValueType " " KeyName " " ValueName " " 0

; RegWrite.ahk
Loop, %0%
    params .= A_Space %A_Index%
    params := SubStr(params, 2)

; https://autohotkey.com/docs/Run#RunAs
full_command_line := DllCall("GetCommandLine", "str")
if !(A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)")) {
    try {
        if A_IsCompiled
            Run *RunAs "%A_ScriptFullPath%" "%params%" /restart
        else
            Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" "%params%"
    }
    ExitApp
}

RegWrite, % A_Args[1], % A_Args[2], % A_Args[3], % A_Args[4]
if ErrorLevel
    MsgBox, % A_LastError "`n`n" """" params """"
else
    MsgBox, % "No error"
How can I resolve error 87 (ERROR_INVALID_PARAMETER)?
Last edited by Ecimeric on 22 Jun 2021, 15:41, edited 1 time in total.
User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Error 87 (ERROR_INVALID_PARAMETER)

Post by mikeyww » 22 Jun 2021, 04:52

Hard to say, but you can display your command-line parameters to see what they are in the script.
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Error 87 (ERROR_INVALID_PARAMETER)

Post by Ecimeric » 22 Jun 2021, 15:42

I added a MsgBox to display the parameters; I even stripped an extra space from params.
User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Error 87 (ERROR_INVALID_PARAMETER)

Post by mikeyww » 22 Jun 2021, 17:26

The following worked for me.

Code: Select all

If !A_IsAdmin && !RegExMatch(DllCall("GetCommandLine", "str"), " /restart(?!\S)") {
 Try Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
 ExitApp
}
ValueType := "REG_DWORD"
KeyName   := "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome"
ValueName := "IncognitoModeAvailability"
Run, AutoHotkeyU32.exe RegWrite.ahk %ValueType% %KeyName% %ValueName% 0

; =====================================

; RegWrite.ahk
RegWrite, % A_Args[1], % A_Args[2], % A_Args[3], % A_Args[4]
MsgBox, 64, ErrorLevel, %ErrorLevel%
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Error 87 (ERROR_INVALID_PARAMETER)

Post by Ecimeric » 23 Jun 2021, 02:58

Thank you. A little disappointing having to elevate/restart the primary script, but at least it works; I may as well create a function in place of RegWrite.ahk.
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Error 87 (ERROR_INVALID_PARAMETER)

Post by just me » 23 Jun 2021, 03:20

Ecimeric:

Code: Select all

    try {
        if A_IsCompiled
            Run *RunAs "%A_ScriptFullPath%" "%params%" /restart
        else
            Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" "%params%"
Run as Administrator:

Code: Select all

    try
    {
        if A_IsCompiled
            Run *RunAs "%A_ScriptFullPath%" /restart
        else
            Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
    }
They are different.
User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Error 87 (ERROR_INVALID_PARAMETER)  Topic is solved

Post by mikeyww » 23 Jun 2021, 09:04

Alternative (& see info above about compiled versions):

Code: Select all

ValueType := "REG_DWORD"
KeyName   := "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome"
ValueName := "IncognitoModeAvailability"
Run, AutoHotkeyU32.exe RegWrite.ahk %ValueType% %KeyName% %ValueName% 0

; ==========================
; RegWrite.ahk
If !A_IsAdmin && !RegExMatch(cline := DllCall("GetCommandLine", "str"), " /restart(?!\S)") {
 Try Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" "%cline%"
 ExitApp
}
part := StrSplit(A_Args.1, " ")
RegWrite, % part.3, % part.4, % part.5, % part.6
MsgBox, 64, ErrorLevel, %ErrorLevel%
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: Error 87 (ERROR_INVALID_PARAMETER)

Post by Ecimeric » 23 Jun 2021, 15:40

Thank you both! :thumbup:
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Error 87 (ERROR_INVALID_PARAMETER)

Post by just me » 24 Jun 2021, 05:39

Sorry! After reading the docs about *RunAs silly me thought that parameters would be passed automatically; they are not, of course.

After re-reading i found two errors in the OP's script:

Code: Select all

Run *RunAs "%A_ScriptFullPath%" "%params%" /restart
Wrong parameter order, switches must precede script parameters.

Code: Select all

Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" "%params%"
You must not enclose the parameters in double-quotes. (That's also true for the first faulty line.) The script will only recognize one parameter then. But you should enclose every single parameter in double-quotes and separate them with a space.

So I would do something like this:

Code: Select all

#NoEnv ; RegWrite.ahk

; ------------------------------------------------------------------------------
; https://autohotkey.com/docs/Run#RunAs
full_command_line := DllCall("GetCommandLine", "str")
args := ""
Loop, % A_Args.Length()
   args .= """" . A_Args[A_Index] . """ "
if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
    try
    {
        if A_IsCompiled
            Run *RunAs "%A_ScriptFullPath%" /restart %args%
        else
            Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" %args%
    }
    ExitApp
}
; ------------------------------------------------------------------------------

MsgBox, 0, RegWrite, % A_Args.Length() . "`n" . A_Args[1] . "`n" . A_Args[2] . "`n" . A_Args[3] . "`n" . A_Args[4]
ExitApp
Edit: Typo
Last edited by just me on 24 Jun 2021, 06:53, edited 1 time in total.
User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Error 87 (ERROR_INVALID_PARAMETER)

Post by mikeyww » 24 Jun 2021, 06:37

I'm not sure that it matters for this particular script, but just me has a better overall approach to the problem.
Post Reply

Return to “Ask for Help (v1)”