- CmdLine: To parse scripts own parameters, pass DllCall( "GetCommandLine", "Str" )
CmdLine can be anything, even imaginary parameters for mere test purposes. - Skip: The no. of parameters to skip ( from left ).
For compiled scripts, the first parameter will be the executable which can be skipped by passing 1
For uncompiled, it can be 2, that is A_AhkPath and A_ScriptFullpath will be skipped
Additionally, a 'reloaded script' would have /restart in its parameters.
argv[0] will contain the number of parameters retrieved. It will be a negative number if Skip > available
Code: Select all
Args( CmdLine := "", Skip := 0 ) { ; By SKAN, http://goo.gl/JfMNpN, CD:23/Aug/2014 | MD:24/Aug/2014
Local pArgs := 0, nArgs := 0, A := []
pArgs := DllCall( "Shell32\CommandLineToArgvW", "WStr",CmdLine, "PtrP",nArgs, "Ptr" )
Loop % ( nArgs )
If ( A_Index > Skip )
A[ A_Index - Skip ] := StrGet( NumGet( ( A_Index - 1 ) * A_PtrSize + pArgs ), "UTF-16" )
Return A, A[0] := nArgs - Skip, DllCall( "LocalFree", "Ptr",pArgs )
}
CmdLine := DllCall( "GetCommandLine", "Str" )
argv := Args( CmdLine )
; or simply
argv := Args( DllCall( "GetCommandLine", "Str" ) )
Test script:
Code: Select all
#Warn
#SingleInstance, Force
CmdLine := DllCall( "GetCommandLine", "Str" )
Skip := ( A_IsCompiled ? 1 : 2 )
argv := Args( CmdLine, Skip )
Msgbox % "Count = " argv[0] "`n1=" argv[1] "`n2=" argv[2] "`n3=" argv[3] "`n4=" argv[4] "`n5=" argv[5]