Jump to content

Error: cannot assign values to constants


Go to solution Solved by jdelaney,

Recommended Posts

Regarding to pass a parameter/argument to my compiled .au3 ->.exe, I read the help on Command Line Parameters zillions times, but I still don't get it right.

my sample code is: "sample.au3"

$CmdLine[0] = 1
MsgBox(0, "", "The argument is " &  $CmdLine[1])

I compile to sample.exe. When I run it in DOS command line:

sample.exe "123"

I always have Autoit error message popup:

AutoIt error:

Error: Cannot assign values to constants

I've been search out some similar questions in this forum, some answer like:  $CmdLine[1] is a constant, can not be used as by passing in a variable.

But hey, that is my purpose. I need to pass a variable into a AutoIT compiled .exe. I need to call autoit compiled .exe and passing in a variable in my C# code. However I failed to do it even in DOS command line.

Again, I already read the HELP regarding "Command Line Parameters", I already search the similar topic in this forum, please help to show me how to correct my sample code make it work.
 

Edited by llgg716
Link to comment
Share on other sites

The cmdline array is for reading, not setting...show us how you are calling the script.

For $i = 1 To UBound($CmdLine) - 1
    ConsoleWrite($CmdLine[$i] & @CRLF)
Next
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

You can copy the variable.

Local $aCmdLine = $CmdLine ; This is an internal AutoIt constant.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

I'm guessing you're confusing how a batch file process a command line parameter and how AutoIt does it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

 

You can copy the variable.

Local $aCmdLine = $CmdLine ; This is an internal AutoIt constant.

You are right. I tried this out and failed as well before I post my question here. It got the same error. After reading the help and other topics in this forum, I realize $CmdLine[x] is a constant and I can't pass in a variable to it through DOS command line. But how can I pass a variable into my autoit compiled .exe?

Link to comment
Share on other sites

Pass a variable or a command param?

Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • Solution

The $cmdline is constant durring the script run...it's values are determined by what you pass in...such as your exampl:

sample.exe "123" "addinganother" "andanother"

 

 

If your compiled script is ONLY what i have pasted above, then you will be able to read the param "123" (as well as the other ones)...change it to a messagebox to see it:

; this is the ONLY code in sample.exe:

For $i = 1 To UBound($CmdLine) - 1
    msgbox(1,1,$CmdLine[$i])
Next
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

 

After reading the help and other topics in this forum, I realize $CmdLine[x] is a constant and I can't pass in a variable to it through DOS command line. But how can I pass a variable into my autoit compiled .exe?

Yes you can, that is its only purpose.

 

Here's a very simple script to demonstrate it.

 

#include <Constants.au3>
If $Cmdline[0] > 0 Then
    For $I = 1 to $Cmdline[0]
        MsgBox($mb_systemmodal, "$Cmdline[" & $I & "] = ", $Cmdline[$I])
    Next
EndIf
Compile it, and send it some command line parameters.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

 

The cmdline array is for reading, not setting...show us how you are calling the script.

For $i = 1 To UBound($CmdLine) - 1
    ConsoleWrite($CmdLine[$i] & @CRLF)
Next

I agree with you after so many reading on help and discussion here. My question is how I can pass a variable into my autoit compiled .exe?

Due to the complexcity of my code, I am just using a similar sample code to brief my quesion.

I create a sample.au3 code:

1.Local $var
2.MsgBox(0, "", "The argument is " &  $var)

And then I compile it to .exe as sample.exe

Now, I like to run sample.exe in DOS command like this:

sample.exe "this is the variable I want o show and process"

Instead of poppng up  a message to show: this is the variable I want o show and process, I got the error message when I execute it in DOS command. But in the end, I like to call sample.exe in my C# code like this:

var proc = Process.Start("C:\\sample.exe "whatever variable I like to pass in" ");
proc.WaitForExit();
Link to comment
Share on other sites

 

The $cmdline is constant durring the script run...it's values are determined by what you pass in...such as your exampl:

sample.exe "123" "addinganother" "andanother"

 

 

If your compiled script is ONLY what i have pasted above, then you will be able to read the param "123" (as well as the other ones)...change it to a messagebox to see it:

; this is the ONLY code in sample.exe:

For $i = 1 To UBound($CmdLine) - 1
    msgbox(1,1,$CmdLine[$i])
Next

 

 

Yes you can, that is its only purpose.

 

Here's a very simple script to demonstrate it.

 

#include <Constants.au3>
If $Cmdline[0] > 0 Then
    For $I = 1 to $Cmdline[0]
        MsgBox($mb_systemmodal, "$Cmdline[" & $I & "] = ", $Cmdline[$I])
    Next
EndIf
Compile it, and send it some command line parameters.

 

 

The above two inputs are working perfectly when I copy and paste to my .au3 code. I compiled it to .exe and ran it in DOS command, it did pop up the message displaying the parameters/arguements correctly!

Until now, reading the correct code and executing it in front of me, I start to understand what you guys talking about! Thank you guys so much for the demo.

Link to comment
Share on other sites

Sometimes all it takes is to see it in action to understand how it all works together.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Before I lable it "resolved" on this topic, allow me the last question on C# part. I am alomost there.

Ok, I have the autoIT compiled sample.exe can accept multiple argument now. Then, I like to call this sample.exe in C# code.

public void tocall_Sample_with_args(string arg1, string arg2)
{
            var proc = Process.Start("C:\\sample.exe " + arg1 + " " + arg2);
            proc.WaitForExit();
 }

Build succeeded in C#. But I ran into error when I ran it. Looks like something wrong on the above piece of new code.

An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll

Additional information: The system cannot find the file specified

Can you please show me how to call autoIT compiled .exe with multiple arguments in C#?

Edited by llgg716
Link to comment
Share on other sites

do your args have spaces in them?  If so, you should wrap them:

...i'm not sure if you can escape??  Assumed the escape is (because of your file path escaping the )...so where you see ", it's actually including a " in the start()

var proc = Process.Start("C:sample.exe "" + arg1 + "" "" + arg2 + """);
 

Wish I knew more about C coding of any type, sorry :)

edit: answer 3: http://stackoverflow.com/questions/5168612/launch-program-with-parameters

edit2: this is an autoit forum :)

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

thank you for the link, jdelaney. With your link and another google search: http://stackoverflow.com/questions/12091152/run-application-from-c-sharp-code-with-multiple-arguments

 

 

public void to_call_sample(string arg1, string arg2)
{
            startInfo.FileName = @"C:\\sample.exe";
            startInfo.Arguments = arg1 + " " + arg2;
            var proc = Process.Start(startInfo);
            proc.WaitForExit();
}

I have the C# code working fine to call my auoit compiled exe with multiple arguments.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...